zoukankan      html  css  js  c++  java
  • (修改完成)spring 梳理11--注解实现AOP

    方法增强=加上日志 

    一、使用AOP织入,需要导入一个依赖包

    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.9.4</version>
    </dependency>

    二、application.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:aop="http://www.springframework.org/schema/aop"
    
           xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    https://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    ">
    
        <!--扫包,com.xinzhi底下都扫扫看-->
        <context:component-scan base-package="com.xinzhi"/>
    
        <!--第三种方式:注解实现-->
        <aop:aspectj-autoproxy/>
    
    
    
    
    </beans>
    application.xml

    1. 头文件添加aop相关,保证我们在配置文件中可以写相应的东西 

    2. 注解实现aop必须添加

    <!--第三种方式:注解实现-->
        <aop:aspectj-autoproxy/>

    3. 记得开扫包

    <!--扫包,com.xinzhi底下都扫扫看-->
        <context:component-scan base-package="com.xinzhi"/>

    三、接口 和 类

    package com.xinzhi.service;
    
    /**
     * @author sr
     * @date 2021/1/25
     */
    public interface IAdminService {
        /**
         * 添加管理员
         * @param name
         */
        void saveAdmin(String name);
    }
    IAdminService.java
    package com.xinzhi.service;
    
    import com.xinzhi.entity.User;
    
    /**
     * @author sr
     * @date 2021/1/24
     */
    public interface IUserService {
        /**
         * 获取用户信息
         * @param id
         * @return
         */
        void getUserInfo(int id);
    }
    IUserService.java
    package com.xinzhi.service.impl;
    
    import com.xinzhi.service.IAdminService;
    import org.springframework.stereotype.Service;
    
    /**
     * @author sr
     * @date 2021/1/25
     */
    @Service
    public class AdminServiceImpl implements IAdminService {
    
        @Override
        public void saveAdmin(String name) {
            System.out.println("这是Admin的添加方法");
        }
    }
    AdminServiceImpl.java
    package com.xinzhi.service.impl;
    
    import com.xinzhi.dao.IUserDao;
    import com.xinzhi.entity.User;
    import com.xinzhi.service.IUserService;
    import org.springframework.stereotype.Service;
    
    import javax.annotation.Resource;
    
    /**
     * @author sr
     * @date 2021/1/24
     */
    @Service
    public class UserServiceImpl implements IUserService {
    
    
        @Override
        public void getUserInfo(int id) {
            System.out.println("这是userService");
        }
    }
    UserServiceImpl.java

    四、LogAdivice

    package com.xinzhi.aop;
    
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.*;
    import org.springframework.stereotype.Component;
    
    import java.util.Date;
    
    /**
     * @author sr
     * @date 2021/1/25
     */
    @Component
    @Aspect
    public class LogAdvice {
        //springaop自动的5种aop这里全部列出
    
    
        /**
         *前置通知
         */
        @Before("execution(* com.xinzhi.service.impl.*.*(..))")
        public void before(){
            System.out.println("---------方法执行前before()---------");
        }
    
    
        /**
         * 后置通知
         */
        @After("execution(* com.xinzhi.service.impl.*.*(..))")
        public void after(){
            System.out.println("---------方法执行后after()---------");
        }
    
    
    
        /**
         * 返回后的通知
         */
        @AfterReturning("execution(* com.xinzhi.service.impl.*.*(..))")
        public void afterReturning(){
            System.out.println("---------方法返回后afterReturning()---------");
        }
    
        /**
         * 环绕通知
         * @param jp             切点
         * @throws Throwable
         */
        @Around("execution(* com.xinzhi.service.impl.*.*(..))")
        public void around(ProceedingJoinPoint jp) throws Throwable {
            System.out.println("-------环绕前-------");
            System.out.println("签名(拿到方法名):"+jp.getSignature());
    
            //执行目标方法proceed
            Object proceed = jp.proceed();
    
            System.out.println("-------环绕后------");
            System.out.println(proceed);
        }
    
        /**
         * 异常发生时通知
         */
        @AfterThrowing("execution(* com.xinzhi.service.impl.*.*(..))")
        public void afterThrow() {
            System.out.println("--------------有异常发生-----------------" + new Date());
        }
    
    }

    五、测试

     @Autowired
        private IUserService userService;
        @Autowired
        private IAdminService adminService;
    
        @Test
        public void testCreateBean(){
            //加载配置文件
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
            userService = applicationContext.getBean(IUserService.class);
            adminService = applicationContext.getBean(IAdminService.class);
    
            userService.getUserInfo(3);
            System.out.println("------------分割线-------------");
            adminService.saveAdmin("孙锐");
        }

    输出结果:

    目录

  • 相关阅读:
    如何测试一个纸杯?
    你对测试最大的兴趣在哪里?为什么
    您认为在测试人员同开发人员的沟通过程中,如何提高沟通的效率和改善沟通的效果?维持测试人员同开发团队中其他成员良好的人际关系的关键是什么?
    BUG管理工具的跟踪过程(用BugZilla为例子
    说说你对集成测试中自顶向下集成和自底向上集成两个策略的理解,要谈出它们各自的优缺点和主要适应于哪种类型测试
    你认为做好测试计划工作的关键是什么
    单元测试、集成测试、系统测试的侧重点是什么?
    黑盒测试和白盒测试是软件测试的两种基本方法,请分别说明各自的优点和缺点!
    Python 运算符
    Python 基础数据类型
  • 原文地址:https://www.cnblogs.com/Master-Sun/p/14320576.html
Copyright © 2011-2022 走看看