如果我对方法使用事务性注释,同时也使用Aspect,那么spring会如何表现?它会在事务代理对象上创建方面代理吗? 或者spring是否能够混淆代理对象的逻辑?
如果我的理解完全错误,请纠正我。
答案 0 :(得分:2)
AOP Proxies是由BeanPostProcessor
创建的,AbstractAutoProxyCreator
在AopUtils.findAdvisorsThatCanApply()
的雇佣关系中最具体的一个,具有以下步骤
OrderComparator
。AbstractAdvisorAutoProxyCreator.sortAdvisors()
对顾问进行排序,请参阅<bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="target" />
<property name="proxyTargetClass" value="true" />
<property name="interceptorNames" value="tracer" />
</bean>
<bean id="target" class="test.SomeBean" />
<bean id="tracer" class="test.Tracer" />
<aop:config proxy-target-class="true">
<aop:advisor id="traceAdvisor" advice-ref="tracer" pointcut="execution (public * *(..))" />
</aop:config>
。通常,只涉及代理。
然而,正如Marten所说,如果您通过AutorProxyCreator未知的其他方式创建代理,您可以轻松获得代理的代理。
例如:
public class SomeBean {
public void someMethod() {
System.out.println("In someMethod");
}
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/context.xml");
SomeBean bean = (SomeBean) ctx.getBean("proxy");
bean.someMethod();
}
}
public class Tracer implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] args, Object target)
throws Throwable {
System.out.println("About to execute [" + method.getName() + "]" +
" on target [" + target.getClass().getName() + "]");
}
}
使用
About to execute [someMethod] on target [test.SomeBean$$EnhancerByCGLIB$$428125af]
About to execute [someMethod] on target [test.SomeBean$$EnhancerByCGLIB$$ee348b75]
About to execute [someMethod] on target [test.SomeBean]
In someMethod
将输出:
{{1}}