作者:lovely月夜静悄悄知_302 | 来源:互联网 | 2023-10-12 08:32
在aop或者事务配置中都会使用AopConfigUtils,static{APC_PRIORITY_LIST.add(InfrastructureAdvisorAutoProxyCreator
在aop或者事务配置中都会使用AopConfigUtils,
static {
APC_PRIORITY_LIST.add(InfrastructureAdvisorAutoProxyCreator.class);
APC_PRIORITY_LIST.add(AspectJAwareAdvisorAutoProxyCreator.class);
APC_PRIORITY_LIST.add(AnnotationAwareAspectJAutoProxyCreator.class);
}
<tx:annotation-driven transaction-manager="sqlTransactionManager" proxy-target-class="true" />
这个事务配置会注册InfrastructureAdvisorAutoProxyCreator
<aop:aspectj-autoproxy proxy-target-class="true" expose-proxy="true"/>
aop配置会注册AnnotationAwareAspectJAutoProxyCreator
但最终使用的是AnnotationAwareAspectJAutoProxyCreator,
注册的时候回进行优先级判断,优先级大的替换小的
private static BeanDefinition registerOrEscalateApcAsRequired(Class> cls, BeanDefinitionRegistry registry, Object source) {
Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
if (registry.containsBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME)) {
BeanDefinition apcDefinition = registry.getBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME);
if (!cls.getName().equals(apcDefinition.getBeanClassName())) {
int currentPriority = findPriorityForClass(apcDefinition.getBeanClassName());
int requiredPriority = findPriorityForClass(cls);
if (currentPriority apcDefinition.setBeanClassName(cls.getName());
}
}
return null;
}
RootBeanDefinition beanDefinition = new RootBeanDefinition(cls);
beanDefinition.setSource(source);
beanDefinition.getPropertyValues().add("order", Ordered.HIGHEST_PRECEDENCE);
beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
registry.registerBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME, beanDefinition);
return beanDefinition;
}
而优先级就是所在list的下标
private static int findPriorityForClass(String className) {
for (int i = 0; i <APC_PRIORITY_LIST.size(); i++) {
Class> clazz = APC_PRIORITY_LIST.get(i);
if (clazz.getName().equals(className)) {
return i;
}
}
throw new IllegalArgumentException(
"Class name [" + className + "] is not a known auto-proxy creator class");
}
所以AnnotationAwareAspectJAutoProxyCreator优先级会比InfrastructureAdvisorAutoProxyCreator大,使用的是AnnotationAwareAspectJAutoProxyCreator。