SpringCloud开发学习总结(六)—— 结合注解的AOP示例

  面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

  我们现在做的一些非业务,如:日志、事务、安全等都会写在业务代码中(也即是说,这些非业务类横切于业务类),但这些代码往往是重复,复制——粘贴式的代码会给程序的维护带来不便,AOP就实现了把这些业务需求与系统需求分开来做。这种解决的方式也称代理机制。

  先来了解一下AOP的相关概念,《Spring参考手册》中定义了以下几个AOP的重要概念,结合下面示例分析如下:

  • 切面(Aspect):官方的抽象定义为“一个关注点的模块化,这个关注点可能会横切多个对象”,在本例中,“切面”就是类LogAspect所关注的具体行为,例如,TestServiceImp.update()的调用就是切面LogAspect所关注的行为之一。“切面”在ApplicationContext中<aop:aspect>来配置,此项目中spring-boot-starter-aop已包含配置
  • 连接点(Joinpoint) :程序执行过程中的某一行为,例如,ILogService.insert的调用或者ILogService.delete抛出异常等行为。
  • 通知(Advice) :“切面”对于某个“连接点”所产生的动作,例如,TestAspect中对com.spring.service包下所有类的方法进行日志新增的动作就是一个Advice。其中,一个“切面”可以包含多个“Advice”,新增,修改,删除等。
  • 切入点(Pointcut) :匹配连接点的断言,在AOP中通知和一个切入点表达式关联。大部分做法都由切入点表达式execution(* com.spring.service.*.*(..))来决定,本例是通过@Pointcut("@annotation(com.didispace.web.aspect.ServiceLog) ")注解的方式。
  • 目标对象(Target Object) :被一个或者多个切面所通知的对象。例如,AServcieImpl和BServiceImpl,当然在实际运行时,Spring AOP采用代理实现,实际AOP操作的是TargetObject的代理对象。
  • AOP代理(AOP Proxy) :在Spring AOP中有两种代理方式,JDK动态代理和CGLIB代理。默认情况下,TargetObject实现了接口时,则采用JDK动态代理,例如,AServiceImpl;反之,采用CGLIB代理,例如,BServiceImpl。强制使用CGLIB代理需要将 <aop:config>的 proxy-target-class属性设为true。

通知(Advice)类型:

  • 前置通知(Before advice):在某连接点(JoinPoint)之前执行的通知,但这个通知不能阻止连接点前的执行。ApplicationContext中在<aop:aspect>里面使用<aop:before>元素进行声明。例如,LogAspect中的before方法。
  • 后置通知(After advice):当某连接点退出的时候执行的通知(不论是正常返回还是异常退出)。ApplicationContext中在<aop:aspect>里面使用<aop:after>元素进行声明。例如,LogAspect中的after方法,所以调用doError抛出异常时,after方法仍然执行。
  • 返回后通知(After return advice):在某连接点正常完成后执行的通知,不包括抛出异常的情况。ApplicationContext中在<aop:aspect>里面使用<after-returning>元素进行声明。
  • 环绕通知(Around advice):包围一个连接点的通知,类似Web中Servlet规范中的Filter的doFilter方法。可以在方法的调用前后完成自定义的行为,也可以选择不执行。ApplicationContext中在<aop:aspect>里面使用<aop:around>元素进行声明。例如,LogAspect中的handleAround方法。
  • 抛出异常后通知(After throwing advice):在方法抛出异常退出时执行的通知。ApplicationContext中在<aop:aspect>里面使用<aop:after-throwing>元素进行声明。例如,LogAspect中的doAfterThrowing方法。

接下来通过一下例子来演示SpringCloud+AOP

  • 在pom.xml文件中引入(starter中默认添加了@EnableAspectJAutoProxy)

 

1         <!--引用AOP注解功能开始-->
2         <dependency>
3             <groupId>org.springframework.boot</groupId>
4             <artifactId>spring-boot-starter-aop</artifactId>
5         </dependency>
6         <!--引用AOP注解功能结束-->

 

 

  •  自定义一个注解,用于注解式AOP 
public enum LogType {
    INFO, WARN, ERROR
}
 1 /**
 2  * 系统日志记录
 3  * 
 4  * @author cjg
 5  *
 6  */
 7 @Target({ ElementType.METHOD })
 8 @Retention(RetentionPolicy.RUNTIME)
 9 @Documented
10 public @interface ServiceLog {
11     /**
12      * 操作类型,新增用户?删除用户 ?调用xx服务?使用接口?...
13      * 
14      * @return
15      */
16     public String operation();
17 
18     /**
19      * 日志级别
20      * 
21      * @return
22      */
23     public LogType level() default LogType.INFO;
24 
25 }
  • 使用@Aspect注解将一个java类定义为切面类
  1 @Component
  2 @Aspec
  3 public class LogAspect {
  4 
  5     private static final Logger log = LoggerFactory.getLogger(LogAspect.class);
  6 
  7     /**
  8      * 切入点
  9      */
 10     @Pointcut("@annotation(com.didispace.web.aspect.ServiceLog) ")
 11     public void entryPoint() {
 12         // 无需内容
 13     }
 14 
 15     @Before("entryPoint()")
 16     public void before(JoinPoint joinPoint) {
 17 
 18         log.info("=====================开始执行前置通知==================");
 19         try {
 20             String targetName = joinPoint.getTarget().getClass().getName();
 21             String methodName = joinPoint.getSignature().getName();
 22             Object[] arguments = joinPoint.getArgs();
 23             Class<?> targetClass = Class.forName(targetName);
 24             Method[] methods = targetClass.getMethods();
 25             String operation = "";
 26             for (Method method : methods) {
 27                 if (method.getName().equals(methodName)) {
 28                     Class<?>[] clazzs = method.getParameterTypes();
 29                     if (clazzs.length == arguments.length) {
 30                         operation = method.getAnnotation(ServiceLog.class).operation();// 操作人
 31                         break;
 32                     }
 33                 }
 34             }
 35             StringBuilder paramsBuf = new StringBuilder();
 36             for (Object arg : arguments) {
 37                 paramsBuf.append(arg);
 38                 paramsBuf.append("&");
 39             }
 40 
 41             // *========控制台输出=========*//
 42             log.info("[X用户]执行了[" + operation + "],类:" + targetName + ",方法名:" + methodName + ",参数:"
 43                     + paramsBuf.toString());
 44             log.info("=====================执行前置通知结束==================");
 45         } catch (Throwable e) {
 46             log.info("around " + joinPoint + " with exception : " + e.getMessage());
 47         }
 48 
 49     }
 50     
 51     @After("entryPoint()")
 52     public void after(JoinPoint joinPoint) {
 53 
 54         log.info("=====================开始执行后置通知==================");
 55         try {
 56             String targetName = joinPoint.getTarget().getClass().getName();
 57             String methodName = joinPoint.getSignature().getName();
 58             Object[] arguments = joinPoint.getArgs();
 59             Class<?> targetClass = Class.forName(targetName);
 60             Method[] methods = targetClass.getMethods();
 61             String operation = "";
 62             for (Method method : methods) {
 63                 if (method.getName().equals(methodName)) {
 64                     Class<?>[] clazzs = method.getParameterTypes();
 65                     if (clazzs.length == arguments.length) {
 66                         operation = method.getAnnotation(ServiceLog.class).operation();// 操作人
 67                         break;
 68                     }
 69                 }
 70             }
 71             StringBuilder paramsBuf = new StringBuilder();
 72             for (Object arg : arguments) {
 73                 paramsBuf.append(arg);
 74                 paramsBuf.append("&");
 75             }
 76 
 77             // *========控制台输出=========*//
 78             log.info("[X用户]执行了[" + operation + "],类:" + targetName + ",方法名:" + methodName + ",参数:"
 79                     + paramsBuf.toString());
 80             log.info("=====================执行后置通知结束==================");
 81         } catch (Throwable e) {
 82             log.info("around " + joinPoint + " with exception : " + e.getMessage());
 83         }
 84 
 85     }
 86     /**
 87      * 环绕通知处理处理
 88      * 
 89      * @param joinPoint
 90      * @throws Throwable
 91      */
 92     @Around("entryPoint()")
 93     public Object around(ProceedingJoinPoint point) throws Throwable {
 94         // 先执行业务,注意:业务这样写业务发生异常不会拦截日志。
 95         Object result = point.proceed();
 96         try {
 97             handleAround(point);// 处理日志
 98         } catch (Exception e) {
 99             log.error("日志记录异常", e);
100         }
101         return result;
102     }
103 
104     /**
105      * around日志记录
106      * 
107      * @param point
108      * @throws SecurityException
109      * @throws NoSuchMethodException
110      */
111     public void handleAround(ProceedingJoinPoint point) throws Exception {
112         Signature sig = point.getSignature();
113         MethodSignature msig = null;
114         if (!(sig instanceof MethodSignature)) {
115             throw new IllegalArgumentException("该注解只能用于方法");
116         }
117         msig = (MethodSignature) sig;
118         Object target = point.getTarget();
119         Method currentMethod = target.getClass().getMethod(msig.getName(), msig.getParameterTypes());
120         // 方法名称
121         String methodName = currentMethod.getName();
122         // 获取注解对象
123         ServiceLog aLog = currentMethod.getAnnotation(ServiceLog.class);
124         // 类名
125         String className = point.getTarget().getClass().getName();
126         // 方法的参数
127         Object[] params = point.getArgs();
128 
129         StringBuilder paramsBuf = new StringBuilder();
130         for (Object arg : params) {
131             paramsBuf.append(arg);
132             paramsBuf.append("&");
133         }
134         // 处理log。。。。
135         log.info("[X用户]执行了[" + aLog.operation() + "],类:" + className + ",方法名:" + methodName + ",参数:"
136                 + paramsBuf.toString());
137 
138     }
139 
140     @AfterThrowing(pointcut = "entryPoint()", throwing = "e")
141     public void doAfterThrowing(JoinPoint joinPoint, Throwable e) {
142         // 通过request获取登陆用户信息
143         // HttpServletRequest request = ((ServletRequestAttributes)
144         // RequestContextHolder.getRequestAttributes()).getRequest();
145         try {
146             String targetName = joinPoint.getTarget().getClass().getName();
147             String className = joinPoint.getTarget().getClass().getName();
148             String methodName = joinPoint.getSignature().getName();
149             Object[] arguments = joinPoint.getArgs();
150             Class<?> targetClass = Class.forName(targetName);
151             Method[] methods = targetClass.getMethods();
152             String operation = "";
153             for (Method method : methods) {
154                 if (method.getName().equals(methodName)) {
155                     Class<?>[] clazzs = method.getParameterTypes();
156                     if (clazzs.length == arguments.length) {
157                         operation = method.getAnnotation(ServiceLog.class).operation();
158                         break;
159                     }
160                 }
161             }
162 
163             StringBuilder paramsBuf = new StringBuilder();
164             for (Object arg : arguments) {
165                 paramsBuf.append(arg);
166                 paramsBuf.append("&");
167             }
168 
169             log.info("异常方法:" + className + "." + methodName + "();参数:" + paramsBuf.toString() + ",处理了:" + operation);
170             log.info("异常信息:" + e.getMessage());
171         } catch (Exception ex) {
172             log.error("异常信息:{}", ex.getMessage());
173         }
174     }
175 }

 

  • 写AOP测试功能
 1 public interface ILogService {
 2 
 3     public boolean insert(Map<String, Object> params, String id);
 4 
 5     public boolean update(String name, String id);
 6 
 7     public boolean delete(String id);
 8 
 9     public boolean doError(String id);
10 }
 1 @Service
 2 public class TestServiceImp implements ILogService {
 3 
 4     @ServiceLog(operation = "新增用户信息测试操作。。。。。")
 5     @Override
 6     public boolean insert(Map<String, Object> params, String id) {
 7         return false;
 8     }
 9 
10     @ServiceLog(operation = "更新用户信息操作....")
11     @Override
12     public boolean update(String name, String id) {
13         return false;
14     }
15 
16     @ServiceLog(operation = "删除操作。。。。")
17     @Override
18     public boolean delete(String id) {
19         return false;
20     }
21 
22     @ServiceLog(operation = "异常操作测试", level = LogType.ERROR)
23     @Override
24     public boolean doError(String id) {
25         try {
26             @SuppressWarnings("unused")
27             int i = 1 / 0;
28         } catch (Exception e) {
29             throw new RuntimeException(e.getMessage());
30         }
31         return false;
32     }
33 
34 }
  • 编写SpringBoot测试类,并展示结果
 1 @RunWith(SpringRunner.class)
 2 @SpringBootTest
 3 public class DemoSpringbootAopLogApplicationTests {
 4 
 5     @Autowired
 6     ILogService logService;
 7 
 8     @Test
 9     public void contextLoads() {
10         Map<String, Object> params = new HashMap<>();
11         params.put("key1", "v1");
12         params.put("key2", "v2");
13 
14         logService.insert(params, "111");
15         logService.update("king", "kang");
16         logService.delete("111");
17         logService.doError("111");
18     }
19 
20 }

至此,SpringCloud+AOP搭建成功!

项目完整代码见https://github.com/Adosker/hello


 

 

注释一:JDK实现动态代理需要实现类通过接口定义业务方法,对于没有接口的类则需要CGLib。CGLib采用了非常底层的字节码技术,其原理是通过字节码技术为一个类创建子类,并在子类中采用方法拦截的技术拦截所有父类方法的调用,顺势织入横切逻辑。

JDK动态代理与CGLib动态代理均是实现Spring AOP的基础。

两者比较:CGLib创建的动态代理对象性能比JDK创建的动态代理对象的性能高不少,但是CGLib在创建代理对象时所花费的时间却比JDK多得多,所以对于单例的对象,因为无需频繁创建对象,用CGLib合适,反之,使用JDK方式要更为合适一些。同时,由于CGLib由于是采用动态创建子类的方法,对于final方法,无法进行代理!

 

posted @ 2018-08-20 15:56  用心记录每一天  阅读(6377)  评论(2编辑  收藏  举报