在Spring中AOP有几种配置方式,根据我对spring源码的浏览,发现几种实现方式原理如下:
1. ProxyFactoryBean
<bean name="myController" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="interceptorNames">
<list>
<value>pointcut.advisor2</value>
<value>pointcut.advisor1</value>
<value>myRawController</value>
</list>
</property>
</bean>
这个属于最费力不讨好类型的,配置起来很麻烦。原理是根据spring的获取bean的方式,继承了FactoryBean接口的bean在取bean的时候会调用对应的bean class的getObject方法。下面是ProxyFactoryBean的getObject方法:
public Object getObject()
throws BeansException
{
initializeAdvisorChain();
if(isSingleton())
return getSingletonInstance();
if(targetName == null)
logger.warn("Using non-singleton proxies with singleton targets is often undesirable. Enable prototype proxies by setting the 'targetName' property.");
return newPrototypeInstance();
}
private synchronized Object newPrototypeInstance()
{
if(logger.isTraceEnabled())
logger.trace((new StringBuilder("Creating copy of prototype ProxyFactoryBean config: ")).append(this).toString());
ProxyCreatorSupport copy = new ProxyCreatorSupport(getAopProxyFactory());
TargetSource targetSource = freshTargetSource();
copy.copyConfigurationFrom(this, targetSource, freshAdvisorChain());
if(autodetectInterfaces && getProxiedInterfaces().length == 0 && !isProxyTargetClass())
copy.setInterfaces(ClassUtils.getAllInterfacesForClass(targetSource.getTargetClass(), proxyClassLoader));
copy.setFrozen(freezeProxy);
if(logger.isTraceEnabled())
logger.trace((new StringBuilder("Using ProxyCreatorSupport copy: ")).append(copy).toString());
return getProxy(copy.createAopProxy());
}
于是,通过这种方式实现了bean的代理。不过这种方式的缺点也是显而易见的,那就是配置起来相当麻烦。
2. BeanNameAutoProxyCreator
配置方式如下:
<bean id="userService" class="com.aop.service.UserService"/>
<bean id="beforeAdvice" class="com.aop.advice.BeforeAdvice"/>
<bean id="xxxxxx" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property value="beanNames">
<list>
<value>*service</value>
</list>
</property>
<property value="interceptorNames">
<value>beforeAdvice</value>
</property>
</bean>
这个类实现了BeanPostProcessor接口的子接口:SmartInstantiationAwareBeanPostProcessor,
每个被这个类care的类在取得bean实例前,会调用以下方法:
public Object postProcessBeforeInstantiation(Class beanClass, String beanName)
throws BeansException
{
Object cacheKey = getCacheKey(beanClass, beanName);
if(!targetSourcedBeans.contains(cacheKey))
{
if(advisedBeans.contains(cacheKey) || nonAdvisedBeans.contains(cacheKey))
return null;
if(isInfrastructureClass(beanClass) || shouldSkip(beanClass, beanName))
{
nonAdvisedBeans.add(cacheKey);
return null;
}
}
TargetSource targetSource = getCustomTargetSource(beanClass, beanName);
if(targetSource != null)
{
targetSourcedBeans.add(beanName);
Object specificInterceptors[] = getAdvicesAndAdvisorsForBean(beanClass, beanName, targetSource);
Object proxy = createProxy(beanClass, beanName, specificInterceptors, targetSource);
proxyTypes.put(cacheKey, proxy.getClass());
return proxy;
} else
{
return null;
}
}
3. <aop:config>标签
通过aop namespace下的一个标签aop:config来实现aop代理,这个也是用起来相当方便的一种配置方式
<bean id="fooService" class="DefaultFooService"/>
<!-- this is the actual advice itself -->
<bean id="profiler" class="SimpleProfiler"/>
<aop:config>
<aop:aspect ref="profiler">
<aop:pointcut id="aopafterMethod"
expression="execution(* FooService.*(..))"/>
<aop:after pointcut-ref="aopafterMethod"
method="afterMethod"/>
<aop:pointcut id="aopBefore"
expression="execution(* FooService.getBefore(String)) and args(myName)"/>
<aop:before pointcut-ref="aopBefore"
method="beforeMethod"/>
</aop:aspect>
</aop:config>
配置很简短,功能很全面。
这种配置方式的原理则是在进行配置文件解析的时候,由AopNameSpaceHandler对此标签进行解析,然后
注册一个“org.springframework.aop.config.internalAutoProxyCreator” bean,这个bean的实现类是:
org/springframework/aop/aspectj/autoproxy/AspectJAwareAdvisorAutoProxyCreator,此类也实现了
BeanPostProcessor接口。
至此,把大致原理分析了一下。当然,分析的不是很详细,有兴趣的朋友可以跟我联系大家一起交流一下。