spring注解版aop

  1. 创建maven项目,然后导入spring依赖,这些就不细说了
  2. 既然这是注解版,那就先创建spring配置类(配置类代替了xml文件)
     1 package crm.mytest.aoptest;
     2 
     3 import org.springframework.context.annotation.ComponentScan;
     4 import org.springframework.context.annotation.Configuration;
     5 import org.springframework.context.annotation.EnableAspectJAutoProxy;
     6 
     7 @Configuration
     8 @EnableAspectJAutoProxy//给配置类中添加注解功能,必须要加不能忘!
     9 @ComponentScan("crm.mytest.aoptest")
    10 public class MyConfig {
    11 /*    @Bean
    12 public Mathcaculator mathcaculator(){
    13     return new Mathcaculator();
    14 }
    15     @Bean
    16     public LogAspects logAspects(){
    17         return new LogAspects();
    18     }*/
    19 }
    //配置类可以不添加bean,因为可以使用包扫描配合上@Component就好了

    3. 创建目标类Mathcaculator,添加到容器中

     1 package crm.mytest.aoptest;
     2 
     3 import org.springframework.stereotype.Component;
     4 @Component
     5 public class Mathcaculator {
     6 
     7     public int div(int i, int j) {
     8         return i / j;
     9     }
    10 }

    4.创建切面类LogAspectsClass

     1 package crm.mytest.aoptest;
     2 
     3 import org.aspectj.lang.JoinPoint;
     4 import org.aspectj.lang.annotation.*;
     5 import org.springframework.stereotype.Component;
     6 
     7 import java.util.Arrays;
     8 
     9 /**
    10  * LogAspectsClass
    11  */
    12 @Aspect//告诉spring这是一个切面类
    13 @Component
    14 public class LogAspects {
    15 
    16     //抽取出公共的切入点
    17     @Pointcut("execution(* crm.mytest.aoptest.Mathcaculator.*(..))")
    18     public void pointCut(){};
    19 
    20     //@Before在目标方法之前切入;切入点表达式(指定在哪个方法切入)
    21     @Before("pointCut()")
    22     public void logStart(JoinPoint joinPoint){
    23         Object[] args = joinPoint.getArgs();
    24         System.out.println(""+joinPoint.getSignature().getName()+"运行。。。@Before:参数列表是:{"+Arrays.asList(args)+"}");
    25     }
    26 
    27     @After("pointCut()")
    28     public void logEnd(JoinPoint joinPoint){
    29         System.out.println(""+joinPoint.getSignature().getName()+"结束。。。@After");
    30     }
    31 
    32     //JoinPoint一定要出现在参数表的第一位
    33     @AfterReturning(value="pointCut()",returning="result")
    34     public void logReturn(JoinPoint joinPoint,Object result){
    35         System.out.println(""+joinPoint.getSignature().getName()+"正常返回。。。@AfterReturning:运行结果:{"+result+"}");
    36     }
    37 
    38     @AfterThrowing(value="pointCut()",throwing="exception")
    39     public void logException(JoinPoint joinPoint, Exception exception){
    40         System.out.println(""+joinPoint.getSignature().getName()+"异常。。。异常信息:{"+exception+"}");
    41     }
    42 }

    5.测试代码(利用了junit和spring整合,也就是利用了注解的方式启动容器)

     1 package crm.mytest.aoptest;
     2 
     3 import org.junit.Test;
     4 import org.junit.runner.RunWith;
     5 import org.springframework.beans.factory.annotation.Autowired;
     6 import org.springframework.test.context.ContextConfiguration;
     7 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
     8 
     9 
    10 @RunWith(SpringJUnit4ClassRunner.class)
    11 @ContextConfiguration(classes = crm.mytest.aoptest.MyConfig.class)
    12 public class Demo {
    13     @Autowired
    14     private Mathcaculator mathcaculator;
    15     @Test
    16     public void test01(){
    17         mathcaculator.div(1,1);
    18     }
    19 }

    总结:感觉注解版方便,开发效率更高,不过缺点就是不利维护。不过本人偏爱注解。注解和xml开发方式应该相辅相成,利用他们的优点,规避他们的缺点。比较喜欢的方式,才想用来发表上去,所以xml版本的aop开发,我就不再写啦。感觉记忆能力有限,我只要求自己掌握住效率最高的,综合最好的一种方法,了解其他的方法。

posted @ 2018-06-23 19:33  AnyProgrammer  阅读(231)  评论(0编辑  收藏  举报