美文网首页
注解aop举例

注解aop举例

作者: 快点给我想个名 | 来源:发表于2020-02-26 19:44 被阅读0次
    加入maven依赖
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>4.3.12.RELEASE</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aspects</artifactId>
                <version>4.3.12.RELEASE</version>
            </dependency>
    
    环境搭建
    1. 随便定义个服务
    public class UserService {
        public int getUserAge(int i,int j){
            System.out.println("UserService...getUserAge...");
            return i/j; 
        }
    }
    
    1. 定义切面
    @Aspect
    public class LogAspects {
        
        //抽取公共的切入点表达式
        @Pointcut("execution(* com.simon.springdemo.aop.UserService.*(..))")
        public void pointCut(){};
        
        @Before("pointCut()")
        public void logStart(JoinPoint joinPoint){
            Object[] args = joinPoint.getArgs();
            System.out.println(""+joinPoint.getSignature().getName()+"--@Before:参数列表是:{"+Arrays.asList(args)+"}");
        }
        
        @After("pointCut()")
        public void logEnd(JoinPoint joinPoint){
            System.out.println(""+joinPoint.getSignature().getName()+"--@After");
        }
    
        /**
         * JoinPoint一定要出现在参数表的第一位
         * @param joinPoint
         * @param result
         */
        @AfterReturning(value="pointCut()",returning="result")
        public void logReturn(JoinPoint joinPoint,Object result){
            System.out.println(""+joinPoint.getSignature().getName()+"--@AfterReturning:运行结果:{"+result+"}");
        }
        
        @AfterThrowing(value="pointCut()",throwing="exception")
        public void logException(JoinPoint joinPoint,Exception exception){
            System.out.println(""+joinPoint.getSignature().getName()+"--@AfterThrowin异常信息:{"+exception+"}");
        }
    }
    
    1. 定义配置
    @EnableAspectJAutoProxy
    @Configuration
    public class AOPConfig{
         
        //业务逻辑类加入容器中
        @Bean
        public UserService userService(){
            return new UserService();
        }
    
        //切面类加入到容器中
        @Bean
        public LogAspects logAspects(){
            return new LogAspects();
        }
    }
    
    1. 测试
    public class Test_AOP {
        public static void main(String[] args) {
            AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AOPConfig.class);
            UserService mathCalculator = applicationContext.getBean(UserService.class);
            mathCalculator.getUserAge(1, 0);
            applicationContext.close();
        }
    }
    
    1. 测试结果
    正常执行完毕
      Before->目标方法->After->AfterReturning
    异常执行
      Before->目标方法->After->AfterThrowin异常信息
    

    相关文章

      网友评论

          本文标题:注解aop举例

          本文链接:https://www.haomeiwen.com/subject/hfnichtx.html