Spring AOP应用主要有四种途径:1、利用ProxyFactory编程式应用;2、直接配置ProxyFactoryBean;3、配置AbstractAutoProxyCreator子类实现auto-proxy自动代理;4、通过<aop:config>配置Spring AOP。其中2,3,4都属于声明式Spring AOP的应用。
一、ProxyFactoryBean应用举例
ProxyFactoryBean
<bean id="personTarget" class="com.mycompany.PersonImpl">
<property name="name" value="Tony"/>
<property name="age" value="51"/>
</bean>
<bean id="myAdvisor" class="com.mycompany.MyAdvisor">
<property name="someProperty" value="Custom string property value"/>
</bean>
<bean id="debugInterceptor" class="org.springframework.aop.interceptor.DebugInterceptor">
</bean>
<bean id="person"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces" value="com.mycompany.Person"/>
<property name="target" ref="personTarget"/>
<property name="interceptorNames">
<list>
<value>myAdvisor</value>
<value>debugInterceptor</value>
</list>
</property>
</bean>
Spring通过IOC容器获取到ProxyFactoryBean对象,ProxyFactoryBean对象调用ProxyFactoryBean的getObject()方法取到配置的target的代理对象。
ProxyFactoryBeangetObject()方法如下:
public Object getObject() throws BeansException {
//初始化通知器链
initializeAdvisorChain();
//如果是单例,则调用getSingletonInstance返回单例代理对象
if (isSingleton()) {
return getSingletonInstance();
}
else {
<span style="white-space:pre"> </span>.....
//非单例,每次重新实例化一个代理对象
return newPrototypeInstance();
}
}
二、auto-proxy自动代理使用和源码分析,以BeanNameAutoProxyCreator为例
<beanclass="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<propertyname="beanNames" value="jdk*,onlyJdk"/>
<propertyname="interceptorNames">
<list>
<value>myInterceptor</value>
</list>
</property>
</bean>
AbstractAutoProxyCreator类继承了SmartInstantiationAwareBeanPostProcessor,IOC容器调用getBean()创建实例时,依赖BeanPostProcessor特性调用postProcess**()方法,AbstractAutoProxyCreator重写这些方法判断目标类是否走Spring AOP代理。SpringBeanFactory调用getBean()并且触发BeanPostProcessor截图如下:
AbstractAutoProxyCreator.postProcessBeforeInstantiation()源码
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
Object cacheKey = getCacheKey(beanClass, beanName);
if (beanName == null || !this.targetSourcedBeans.contains(beanName)) {
if (this.advisedBeans.containsKey(cacheKey)) {
return null;
}
if (isInfrastructureClass(beanClass) || shouldSkip(beanClass, beanName)) {
this.advisedBeans.put(cacheKey, Boolean.FALSE);
return null;
}
}
// Create proxy here if we have a custom TargetSource.
// Suppresses unnecessary default instantiation of the target bean:
// The TargetSource will handle target instances in a custom fashion.
if (beanName != null) {
TargetSource targetSource = getCustomTargetSource(beanClass, beanName);
if (targetSource != null) {
this.targetSourcedBeans.add(beanName);
Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(beanClass, beanName, targetSource);
Object proxy = createProxy(beanClass, beanName, specificInterceptors, targetSource);
this.proxyTypes.put(cacheKey, proxy.getClass());
return proxy;
}
}
return null;
}
AbstractAutoProxyCreator.postProcessAfterInitialization()源码
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean != null) {
Object cacheKey = getCacheKey(bean.getClass(), beanName);
if (!this.earlyProxyReferences.contains(cacheKey)) {
return wrapIfNecessary(bean, beanName, cacheKey);
}
}
return bean;
}
protected Object wrapIfNecessary(Object bean, String beanName, Object cacheKey) {
if (beanName != null && this.targetSourcedBeans.contains(beanName)) {
return bean;
}
if (Boolean.FALSE.equals(this.advisedBeans.get(cacheKey))) {
return bean;
}
if (isInfrastructureClass(bean.getClass()) || shouldSkip(bean.getClass(), beanName)) {
this.advisedBeans.put(cacheKey, Boolean.FALSE);
return bean;
}
// getAdvicesAndAdvisorsForBean返回bean对应的Advisor,如果不为空则创建代理对象并返回
Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(bean.getClass(), beanName, null);
if (specificInterceptors != DO_NOT_PROXY) {
this.advisedBeans.put(cacheKey, Boolean.TRUE);
Object proxy = createProxy(bean.getClass(), beanName, specificInterceptors, new SingletonTargetSource(bean));
this.proxyTypes.put(cacheKey, proxy.getClass());
return proxy;
}
this.advisedBeans.put(cacheKey, Boolean.FALSE);
return bean;
}
三、<aop:config>进行aspect配置实现Spring AOP
Spring提供对应的AopNamespaceHandler,DefinitionParser对aop:config进行解析,解析时首先向Bean容器中注册一个AspectJAwareAdvisorAutoProxyCreator(继承AbstractAutoProxyCreator完成auto-proxy功能),然后解析对应的<aop:config>子元素。<aop:config>其实就是在第三种Spring AOP应用方式的基础上进行扩展。
1、AopNamespaceHandler源码
public class AopNamespaceHandler extends NamespaceHandlerSupport {
/**
* Register the {@link BeanDefinitionParser BeanDefinitionParsers} for the
* '{@code config}', '{@code spring-configured}', '{@code aspectj-autoproxy}'
* and '{@code scoped-proxy}' tags.
*/
@Override
public void init() {
//初始化,注册对应的BeanDefinitionParser
registerBeanDefinitionParser("config", new ConfigBeanDefinitionParser());
registerBeanDefinitionParser("aspectj-autoproxy", new AspectJAutoProxyBeanDefinitionParser());
registerBeanDefinitionDecorator("scoped-proxy", new ScopedProxyBeanDefinitionDecorator());
registerBeanDefinitionParser("spring-configured", new SpringConfiguredBeanDefinitionParser());
}
}
2、ConfigBeanDefinitionParser解析器源码,
public BeanDefinition parse(Element element, ParserContext parserContext) {
CompositeComponentDefinition compositeDef =
new CompositeComponentDefinition(element.getTagName(), parserContext.extractSource(element));
parserContext.pushContainingComponent(compositeDef);
//注册AutoProxyCreator
configureAutoProxyCreator(parserContext, element);
//解析对应的子元素
List<Element> childElts = DomUtils.getChildElements(element);
for (Element elt: childElts) {
String localName = parserContext.getDelegate().getLocalName(elt);
if (POINTCUT.equals(localName)) {
parsePointcut(elt, parserContext);
}
else if (ADVISOR.equals(localName)) {
parseAdvisor(elt, parserContext);
}
else if (ASPECT.equals(localName)) {
parseAspect(elt, parserContext);
}
}
parserContext.popAndRegisterContainingComponent();
return null;
}
3、第二步注册了AspectJAwareAdvisorAutoProxyCreator,AspectJAwareAdvisorAutoProxyCreator继承AbstractAutoProxyCreator用于依赖BeanPostProcessor特性在对象实例化前后实现目标对象实例化是否返回代理对象。