导读 | 谈起AOP,大家都知道是面向切面编程,但你真的了解Spring中的AOP吗?Spring AOP、JDK动态代理、CGLIB、AspectJ之间又有什么关联和区别? |
谈起AOP,大家都知道是面向切面编程,但你真的了解Spring中的AOP吗?Spring AOP、JDK动态代理、CGLIB、AspectJ之间又有什么关联和区别?
在Spring中AOP包含两个概念,一是Spring官方基于JDK动态代理和CGLIB实现的Spring AOP;二是集成面向切面编程神器AspectJ。Spring AOP和AspectJ不是竞争关系,基于代理的框架的Spring AOP和成熟框架AspectJ都是有价值的,它们是互补的。
Spring无缝地将Spring AOP、IoC与AspectJ集成在一起,从而达到AOP的所有能力。Spring AOP默认将标准JDK动态代理用于AOP代理,可以代理任何接口。但如果没有面向接口编程,只有业务类,则使用CGLIB。当然也可以全部强制使用CGLIB,只要设置proxy-target-class=”true”。
AOP中的术语
通知(Advice)
Spring切面可以应用5种类型的通知:
前置通知(Before):在目标方法被调用之前调用通知功能;
后置通知(After):在目标方法完成之后调用通知,此时不会关心方法的输出是什么;
返回通知(After-returning):在目标方法成功执行之后调用通知;
异常通知(After-throwing):在目标方法抛出异常后调用通知;
环绕通知(Around):通知包裹了被通知的方法,在被通知的方法调用之前和调用之后执行自定义的行为。
连接点(Join point)
切点(Poincut)
切面(Aspect)
引入(Introduction)
织入(Weaving)
这些术语的解释,其他博文中很多,这里就不再赘述。
现在有这样一个场景,页面传入参数当前页page和每页展示多少条数据rows,我们需要写个拦截器将page、limit参数转换成MySQL的分页语句offset、rows。
1、实现MethodInterceptor,拦截方法
public class MethodParamInterceptor implements MethodInterceptor { @Override @SuppressWarnings("unchecked") public Object invoke(MethodInvocation invocation) throws Throwable { Object[] params = invocation.getArguments(); if (ArrayUtils.isEmpty(params)) { return invocation.proceed(); } for (Object param : params) { //如果参数类型是Map if (param instanceof Map) { Map paramMap = (Map) param; processPage(paramMap); break; } } return invocation.proceed(); } /** * * @param paramMap */ private void processPage(Map paramMap) { if (!paramMap.containsKey("page") && !paramMap.containsKey("limit")) { return; } int page = 1; int rows = 10; for (Map.Entry entry : paramMap.entrySet()) { String key = entry.getKey(); String value = entry.getValue().toString(); if ("page".equals(key)) { page = NumberUtils.toInt(value, page); } else if ("limit".equals(key)) { rows = NumberUtils.toInt(value, rows); }else { //TODO } } int offset = (page - 1) * rows; paramMap.put("offset", offset); paramMap.put("rows", rows); } }
public class RequestParamPostProcessor extends AbstractBeanFactoryAwareAdvisingPostProcessor implements InitializingBean { private Class validatedAnnotationType = Controller.class; @Override public void afterPropertiesSet() throws Exception { Pointcut pointcut = new AnnotationMatchingPointcut(this.validatedAnnotationType, true); this.advisor = new DefaultPointcutAdvisor(pointcut, new MethodParamInterceptor()); } }
@Configuration public class MethodInterceptorConfig { @Bean public RequestParamPostProcessor converter() { return new RequestParamPostProcessor(); } }`
@Configuration public class MethodInterceptorConfig { @Autowired private UserService userService; @Bean public RequestParamPostProcessor converter() { return new RequestParamPostProcessor(); } }
启动时,会出现:
2019-11-08 14:55:50.954 INFO 51396 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'sqlSessionFactory' of type [org.apache.ibatis.session.defaults.DefaultSqlSessionFactory] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2019-11-08 14:55:50.960 INFO 51396 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'sqlSessionTemplate' of type [org.mybatis.spring.SqlSessionTemplate] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2019-11-08 14:55:51.109 INFO 51396 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'rememberMapper' of type [com.sun.proxy.$Proxy84] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2019-11-08 14:55:53.406 INFO 51396 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
很多切面失效,如事务切面。这是因为注入了自定义的Bean,自定义的Bean优先级最低,由最低优先级的BeanPostProcessor来加载并完成初始化的。但为了加载其中的RequestParamPostProcessor,导致不得不优先装载低优先级Bean,此时事务处理器的AOP等都还没完成加载,注解事务初始化都失败了。但Spring就提示了一个INFO级别的提示,然后剩下的Bean由最低优先级的BeanPostProcessor正常处理。
@Component@Aspect@Slf4jpublic class MethodParamInterceptor { @Pointcut("@annotation(org.springframework.web.bind.annotation.PostMapping)") public void paramAspect() { } @Before("paramAspect()") public void beforeDataSource(JoinPoint joinPoint) { Arrays.stream(joinPoint.getArgs()).forEach(paramObject -> { if (paramObject instanceof Map) { Map parameter = (Map) paramObject; processPage(parameter); } }); } private void processPage(Map<String, Object> paramMap) { if (null == paramMap) { return; } if (!paramMap.containsKey("page") && !paramMap.containsKey("limit")) { return; } int page = 1; int rows = 10; for (Map.Entry<String, Object> entry : paramMap.entrySet()) { String key = entry.getKey(); String value = entry.getValue().toString(); if ("page".equals(key)) { page = NumberUtils.toInt(value, page); } else if ("limit".equals(key)) { rows = NumberUtils.toInt(value, rows); } } int offset = (page - 1) * rows; paramMap.put("offset", offset); paramMap.put("rows", rows); } @After("paramAspect()") public void afterDataSource(JoinPoint joinPoint) { } }
从上面两个例子可以对比出SpringAOP和AspectJ的两种不同用法,但达到的能力是一样的。
Sping AOP在组织、抽象代码场景中更加适合,AspectJ用于单纯的切面来实现某项功能更加简洁。
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/121928.html