作者:小傅哥
博客:https://bugstack.cn
沉淀、分享、成长,让自己和他人都能有所收获!😄
一、前言
嘎小子,这片代码水太深你把握不住!
在电视剧《楚汉传奇》中有这么一段刘邦与韩信的饮酒对话,刘邦问韩信我那个曹参读过书见过世面能带多少兵,韩信说能带一万五,又补充说一万五都吃力。刘邦又一一说出樊哙、卢绾、周勃,韩信笑着说不足 2 万,脑子不行。这时候刘邦有点挂不住脸了,问:那我呢,我能带多少兵。韩信说,你能带十万。刘邦一看比他们都多,啊,还行。转头一想就问韩信那你呢,你能带多少兵。韩信喝多了,说啊,我,我多多益善。这时候刘邦恼了领导劲上来了,问:那我为什么能管着你,你给我说,说呀!
这像不像你领导问你,你能写多少代码、搭多少框架、接多少项目。可能很大一部分没经历太多的新人码农,仅仅是能完成一些简单的功能模块开发,而没有办法驾驭整个项目的涉及到的所有工程,也不能为项目提炼出一些可复用的通用性组件模块。在初级码农的心里,接一点需求还好,但没有人带的时候完全接一个较大型项目就会比较慌了,不知道这里有没有坑,自己也把握住不。这些代码一块块的带着能写,但是都弄到一块,就太难了!
在代码开发成长的这条路上,要经历 CRUD、ERP 查数据、接口包装、功能开发、服务整合、系统建设等,一直到独立带人承担较大型项目的搭建。这一过程需要你能有大量的编写代码经验积累和复杂问题的处理手段,之后才能一段段的把看似独立的模块后者代码片段组装成一个较大型能跑起来的项目。就像 Spring 的开发过程一样,我们总是不断在添加新的功能片段,最后又把技术实现与 Spring 容器整合,让使用方可以更简单的运用 Spring 提供的能力。
二、目标
在上一章节我们通过基于 Proxy.newProxyInstance 代理操作中处理方法匹配和方法拦截,对匹配的对象进行自定义的处理操作。并把这样的技术核心内容拆解到 Spring 中,用于实现 AOP 部分,通过拆分后基本可以明确各个类的职责,包括你的代理目标对象属性、拦截器属性、方法匹配属性,以及两种不同的代理操作 JDK 和 CGlib 的方式。
再有了一个 AOP 核心功能的实现后,我们可以通过单元测试的方式进行验证切面功能对方法进行拦截,但如果这是一个面向用户使用的功能,就不太可能让用户这么复杂且没有与 Spring 结合的方式单独使用 AOP,虽然可以满足需求,但使用上还是过去分散。
因此我们需要在本章节完成 AOP 核心功能与 Spring 框架的整合,最终能通过在 Spring 配置的方式完成切面的操作。
三、方案
从 BeanPostProcessor 开始,让 xml 中的配置加载到 DefaultAdvisorAutoProxyCreator 实现,
其实再有了核心功能的开发后,这事也不难。只不过我们要解决几个问题,包括:怎么串联到 Bean 的生命周期中、怎么组装各项功能、怎么适配代理,
借着 BeanPostProcessor,把动态代理融入到 Bean 的生命周期
其实在有了 AOP 的核心功能实现后,把这部分功能服务融入到 Spring 其实也不难,只不过要解决几个问题,包括:怎么借着 BeanPostProcessor 把动态代理融入到 Bean 的生命周期中,以及如何组装各项切点、拦截、前置的功能和适配对应的代理器。整体设计结构如下图:
为了可以让对象创建过程中,能把 xml 中配置的代理对象也就是切面的一些类对象实例化,就需要用到 BeanPostProcessor 提供的方法,因为这个类的中的方法可以分别作用与 Bean 对象执行初始化前后修改 Bean 的对象的扩展信息。但这里需要集合于 BeanPostProcessor 实现新的接口和实现类,这样才能定向获取对应的类信息。
但因为创建的是代理对象不是之前流程里的普通对象,所以我们需要前置于其他对象的创建,所以在实际开发的过程中,需要在 AbstractAutowireCapableBeanFactory#createBean 优先完成 Bean 对象的判断,是否需要代理,有则直接返回代理对象。在 Spring 的源码中会有 createBean 和 doCreateBean 的方法拆分
这里还包括要解决方法拦截器的具体功能,提供一些 BeforeAdvice、AfterAdvice 的实现,让用户可以更简化的使用切面功能。除此之外还包括需要包装切面表达式以及拦截方法的整合,以及提供不同类型的代理方式的代理工厂,来包装我们的切面服务。
四、实现
1. 工程结构
small-spring-step-12└── src ├── main │ └── java │ └── cn.bugstack.springframework │ ├── aop │ │ ├── aspectj │ │ │ └── AspectJExpressionPointcut.java │ │ │ └── AspectJExpressionPointcutAdvisor.java │ │ ├── framework │ │ │ ├── adapter │ │ │ │ └── MethodBeforeAdviceInterceptor.java │ │ │ ├── autoproxy │ │ │ │ └── MethodBeforeAdviceInterceptor.java │ │ │ ├── AopProxy.java │ │ │ ├── Cglib2AopProxy.java │ │ │ ├── JdkDynamicAopProxy.java │ │ │ ├── ProxyFactory.java │ │ │ └── ReflectiveMethodInvocation.java │ │ ├── AdvisedSupport.java │ │ ├── Advisor.java │ │ ├── BeforeAdvice.java │ │ ├── ClassFilter.java │ │ ├── MethodBeforeAdvice.java │ │ ├── MethodMatcher.java │ │ ├── Pointcut.java │ │ ├── PointcutAdvisor.java │ │ └── TargetSource.java │ ├── beans │ │ ├── factory │ │ │ ├── config │ │ │ │ ├── AutowireCapableBeanFactory.java │ │ │ │ ├── BeanDefinition.java │ │ │ │ ├── BeanFactoryPostProcessor.java │ │ │ │ ├── BeanPostProcessor.java │ │ │ │ ├── BeanReference.java │ │ │ │ ├── ConfigurableBeanFactory.java │ │ │ │ ├── InstantiationAwareBeanPostProcessor.java │ │ │ │ └── SingletonBeanRegistry.java │ │ │ ├── support │ │ │ │ ├── AbstractAutowireCapableBeanFactory.java │ │ │ │ ├── AbstractBeanDefinitionReader.java │ │ │ │ ├── AbstractBeanFactory.java │ │ │ │ ├── BeanDefinitionReader.java │ │ │ │ ├── BeanDefinitionRegistry.java │ │ │ │ ├── CglibSubclassingInstantiationStrategy.java │ │ │ │ ├── DefaultListableBeanFactory.java │ │ │ │ ├── DefaultSingletonBeanRegistry.java │ │ │ │ ├── DisposableBeanAdapter.java │ │ │ │ ├── FactoryBeanRegistrySupport.java │ │ │ │ ├── InstantiationStrategy.java │ │ │ │ └── SimpleInstantiationStrategy.java │ │ │ ├── support │ │ │ │ └── XmlBeanDefinitionReader.java │ │ │ ├── Aware.java │ │ │ ├── BeanClassLoaderAware.java │ │ │ ├── BeanFactory.java │ │ │ ├── BeanFactoryAware.java │ │ │ ├── BeanNameAware.java │ │ │ ├── ConfigurableListableBeanFactory.java │ │ │ ├── DisposableBean.java │ │ │ ├── FactoryBean.java │ │ │ ├── HierarchicalBeanFactory.java │ │ │ ├── InitializingBean.java │ │ │ └── ListableBeanFactory.java │ │ ├── BeansException.java │ │ ├── PropertyValue.java │ │ └── PropertyValues.java │ ├── context │ │ ├── event │ │ │ ├── AbstractApplicationEventMulticaster.java │ │ │ ├── ApplicationContextEvent.java │ │ │ ├── ApplicationEventMulticaster.java │ │ │ ├── ContextClosedEvent.java │ │ │ ├── ContextRefreshedEvent.java │ │ │ └── SimpleApplicationEventMulticaster.java │ │ ├── support │ │ │ ├── AbstractApplicationContext.java │ │ │ ├── AbstractRefreshableApplicationContext.java │ │ │ ├── AbstractXmlApplicationContext.java │ │ │ ├── ApplicationContextAwareProcessor.java │ │ │ └── ClassPathXmlApplicationContext.java │ │ ├── ApplicationContext.java │ │ ├── ApplicationContextAware.java │ │ ├── ApplicationEvent.java │ │ ├── ApplicationEventPublisher.java │ │ ├── ApplicationListener.java │ │ └── ConfigurableApplicationContext.java │ ├── core.io │ │ ├── ClassPathResource.java │ │ ├── DefaultResourceLoader.java │ │ ├── FileSystemResource.java │ │ ├── Resource.java │ │ ├── ResourceLoader.java │ │ └── UrlResource.java │ └── utils │ └── ClassUtils.java └── test └── java └── cn.bugstack.springframework.test ├── bean │ ├── IUserService.java │ ├── UserService.java │ └── UserServiceInterceptor.java └── ApiTest.java
复制代码
工程源码:公众号「bugstack虫洞栈」,回复:Spring 专栏,获取完整源码
AOP 动态代理融入到 Bean 的生命周期中类关系,如图 13-2
整个类关系图中可以看到,在以 BeanPostProcessor 接口实现继承的 InstantiationAwareBeanPostProcessor 接口后,做了一个自动代理创建的类 DefaultAdvisorAutoProxyCreator,这个类的就是用于处理整个 AOP 代理融入到 Bean 生命周期中的核心类。
DefaultAdvisorAutoProxyCreator 会依赖于拦截器、代理工厂和 Pointcut 与 Advisor 的包装服务 AspectJExpressionPointcutAdvisor,由它提供切面、拦截方法和表达式。
Spring 的 AOP 把 Advice 细化了 BeforeAdvice、AfterAdvice、AfterReturningAdvice、ThrowsAdvice,目前我们做的测试案例中只用到了 BeforeAdvice,这部分可以对照 Spring 的源码进行补充测试。
2. 定义 Advice 拦截器链
cn.bugstack.springframework.aop.BeforeAdvice
public interface BeforeAdvice extends Advice {
}
复制代码
cn.bugstack.springframework.aop.MethodBeforeAdvice
public interface MethodBeforeAdvice extends BeforeAdvice {
/** * Callback before a given method is invoked. * * @param method method being invoked * @param args arguments to the method * @param target target of the method invocation. May be <code>null</code>. * @throws Throwable if this object wishes to abort the call. * Any exception thrown will be returned to the caller if it's * allowed by the method signature. Otherwise the exception * will be wrapped as a runtime exception. */ void before(Method method, Object[] args, Object target) throws Throwable;
}
复制代码
3. 定义 Advisor 访问者
cn.bugstack.springframework.aop.Advisor
public interface Advisor {
/** * Return the advice part of this aspect. An advice may be an * interceptor, a before advice, a throws advice, etc. * @return the advice that should apply if the pointcut matches * @see org.aopalliance.intercept.MethodInterceptor * @see BeforeAdvice */ Advice getAdvice();
}
复制代码
cn.bugstack.springframework.aop.PointcutAdvisor
public interface PointcutAdvisor extends Advisor {
/** * Get the Pointcut that drives this advisor. */ Pointcut getPointcut();
}
复制代码
cn.bugstack.springframework.aop.aspectj.AspectJExpressionPointcutAdvisor
public class AspectJExpressionPointcutAdvisor implements PointcutAdvisor {
// 切面 private AspectJExpressionPointcut pointcut; // 具体的拦截方法 private Advice advice; // 表达式 private String expression;
public void setExpression(String expression){ this.expression = expression; }
@Override public Pointcut getPointcut() { if (null == pointcut) { pointcut = new AspectJExpressionPointcut(expression); } return pointcut; }
@Override public Advice getAdvice() { return advice; }
public void setAdvice(Advice advice){ this.advice = advice; }
}
复制代码
4. 方法拦截器
cn.bugstack.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor
public class MethodBeforeAdviceInterceptor implements MethodInterceptor {
private MethodBeforeAdvice advice;
public MethodBeforeAdviceInterceptor(MethodBeforeAdvice advice) { this.advice = advice; }
@Override public Object invoke(MethodInvocation methodInvocation) throws Throwable { this.advice.before(methodInvocation.getMethod(), methodInvocation.getArguments(), methodInvocation.getThis()); return methodInvocation.proceed(); }
}
复制代码
MethodBeforeAdviceInterceptor 实现了 MethodInterceptor 接口,在 invoke 方法中调用 advice 中的 before 方法,传入对应的参数信息。
而这个 advice.before 则是用于自己实现 MethodBeforeAdvice 接口后做的相应处理。其实可以看到具体的 MethodInterceptor 实现类,其实和我们之前做的测试是一样的,只不过现在交给了 Spring 来处理
5. 代理工厂
cn.bugstack.springframework.aop.framework.ProxyFactory
public class ProxyFactory {
private AdvisedSupport advisedSupport;
public ProxyFactory(AdvisedSupport advisedSupport) { this.advisedSupport = advisedSupport; }
public Object getProxy() { return createAopProxy().getProxy(); }
private AopProxy createAopProxy() { if (advisedSupport.isProxyTargetClass()) { return new Cglib2AopProxy(advisedSupport); }
return new JdkDynamicAopProxy(advisedSupport); }
}
复制代码
6. 融入 Bean 生命周期的自动代理创建者
cn.bugstack.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator
public class DefaultAdvisorAutoProxyCreator implements InstantiationAwareBeanPostProcessor, BeanFactoryAware {
private DefaultListableBeanFactory beanFactory;
@Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { this.beanFactory = (DefaultListableBeanFactory) beanFactory; }
@Override public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
if (isInfrastructureClass(beanClass)) return null;
Collection<AspectJExpressionPointcutAdvisor> advisors = beanFactory.getBeansOfType(AspectJExpressionPointcutAdvisor.class).values();
for (AspectJExpressionPointcutAdvisor advisor : advisors) { ClassFilter classFilter = advisor.getPointcut().getClassFilter(); if (!classFilter.matches(beanClass)) continue;
AdvisedSupport advisedSupport = new AdvisedSupport();
TargetSource targetSource = null; try { targetSource = new TargetSource(beanClass.getDeclaredConstructor().newInstance()); } catch (Exception e) { e.printStackTrace(); } advisedSupport.setTargetSource(targetSource); advisedSupport.setMethodInterceptor((MethodInterceptor) advisor.getAdvice()); advisedSupport.setMethodMatcher(advisor.getPointcut().getMethodMatcher()); advisedSupport.setProxyTargetClass(false);
return new ProxyFactory(advisedSupport).getProxy();
}
return null; } }
复制代码
这个 DefaultAdvisorAutoProxyCreator 类的主要核心实现在于 postProcessBeforeInstantiation 方法中,从通过 beanFactory.getBeansOfType 获取 AspectJExpressionPointcutAdvisor 开始。
获取了 advisors 以后就可以遍历相应的 AspectJExpressionPointcutAdvisor 填充对应的属性信息,包括:目标对象、拦截方法、匹配器,之后返回代理对象即可。
那么现在调用方获取到的这个 Bean 对象就是一个已经被切面注入的对象了,当调用方法的时候,则会被按需拦截,处理用户需要的信息。
五、测试
1. 事先准备
public class UserService implements IUserService {
public String queryUserInfo() { try { Thread.sleep(new Random(1).nextInt(100)); } catch (InterruptedException e) { e.printStackTrace(); } return "小傅哥,100001,深圳"; }
public String register(String userName) { try { Thread.sleep(new Random(1).nextInt(100)); } catch (InterruptedException e) { e.printStackTrace(); } return "注册用户:" + userName + " success!"; }
}
复制代码
2. 自定义拦截方法
public class UserServiceBeforeAdvice implements MethodBeforeAdvice {
@Override public void before(Method method, Object[] args, Object target) throws Throwable { System.out.println("拦截方法:" + method.getName()); }
}
复制代码
3. spring.xml 配置 AOP
<beans>
<bean id="userService" class="cn.bugstack.springframework.test.bean.UserService"/>
<bean class="cn.bugstack.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>
<bean id="beforeAdvice" class="cn.bugstack.springframework.test.bean.UserServiceBeforeAdvice"/>
<bean id="methodInterceptor" class="cn.bugstack.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor"> <property name="advice" ref="beforeAdvice"/> </bean>
<bean id="pointcutAdvisor" class="cn.bugstack.springframework.aop.aspectj.AspectJExpressionPointcutAdvisor"> <property name="expression" value="execution(* cn.bugstack.springframework.test.bean.IUserService.*(..))"/> <property name="advice" ref="methodInterceptor"/> </bean>
</beans>
复制代码
4. 单元测试
@Testpublic void test_aop() { ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring.xml"); IUserService userService = applicationContext.getBean("userService", IUserService.class); System.out.println("测试结果:" + userService.queryUserInfo());}
复制代码
测试结果
拦截方法:queryUserInfo测试结果:小傅哥,100001,深圳
Process finished with exit code 0
复制代码
六、总结
本章节实现 AOP 功能的外在体现主要是把以前自己在单元测试中的切面拦截,交给 Spring 的 xml 配置了,也就不需要自己手动处理了。那么这里有一个非常重要的知识点,就是把相应的功能如何与 Spring 的 Bean 生命周期结合起来,本章节用到的 BeanPostProcessor,因为它可以解决在 Bean 对象执行初始化方法之前,用于修改新实例化 Bean 对象的扩展点,所以我们也就可以处理自己的 AOP 代理对象逻辑了。
一个功能的实现往往包括核心部分、组装部分、链接部分,为了这些各自职责的分工,则需要创建接口和类,由不同关系的继承、实现进行组装。只有明确了各个职责分工,才好灵活的扩展相应的功能逻辑,否则很难驾驭大型系统的开发和建设,也就是那种不好把握的感觉。
目前我们实现的 AOP 与 Spring 源码中的核心逻辑是类似的,但更会偏简单一些,也不会考虑更多的复杂场景遇到的问题,包括是否有构造函数、是否为代理中的切面等。其实也可以看出只要是 Java 中的一些特性,都需要在真实使用的 Spring 中进行完整的实现,否则在使用这些功能的时候就会遇到各种问题。
七、系列推荐
评论