Java SpringWeb-关于启用AspectJ&;的循环参考;事务管理
在以下条件下,弹簧腹板会产生循环参考Java SpringWeb-关于启用AspectJ&;的循环参考;事务管理,java,spring,spring-transactions,autoproxy,Java,Spring,Spring Transactions,Autoproxy,在以下条件下,弹簧腹板会产生循环参考 启用AspectJ AutoProxy和事务管理 创建一个简单的bean,B1应该首先加载 创建依赖于B1的ProxyFactoryBean B2 下面是我的分析 Spring试图创建Bean B1。在这一点上,, 注释:反转录酶抑制剂BPP生效 AutoProxyCreator打算创建TransactionAdvicer&尝试查找TransactionManagementConfigurer类型的所有bean B2是工厂bean(检查工厂bean类型的快捷
@Configuration
@DependsOn("testBean2")
@EnableTransactionManagement
public class TestConfig
{
@Bean
public PlatformTransactionManager transactionManager()
{
DriverManagerDataSource dataSource = new DriverManagerDataSource();
// MySQL database we are using
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/db");// change url
dataSource.setUsername("username");// change userid
dataSource.setPassword("password");// change pwd
PlatformTransactionManager transactionManager = new DataSourceTransactionManager(dataSource);
return transactionManager;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<aop:aspectj-autoproxy />
<context:component-scan base-package="test.config" />
<bean id="testBean2" class="test.beans.TestBean2" />
<bean id="testTransactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributes">
<props>
<prop key="audit">PROPAGATION_REQUIRES_NEW</prop>
</props>
</property>
</bean>
<bean id="testBean1" class="org.springframework.aop.framework.ProxyFactoryBean"
depends-on="testBean2">
<property name="target">
<bean class="test.beans.TestBean1" />
</property>
<property name="interceptorNames">
<list>
<value>testTransactionInterceptor</value>
</list>
</property>
</bean>
</beans>
XML配置:
@Configuration
@DependsOn("testBean2")
@EnableTransactionManagement
public class TestConfig
{
@Bean
public PlatformTransactionManager transactionManager()
{
DriverManagerDataSource dataSource = new DriverManagerDataSource();
// MySQL database we are using
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/db");// change url
dataSource.setUsername("username");// change userid
dataSource.setPassword("password");// change pwd
PlatformTransactionManager transactionManager = new DataSourceTransactionManager(dataSource);
return transactionManager;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<aop:aspectj-autoproxy />
<context:component-scan base-package="test.config" />
<bean id="testBean2" class="test.beans.TestBean2" />
<bean id="testTransactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributes">
<props>
<prop key="audit">PROPAGATION_REQUIRES_NEW</prop>
</props>
</property>
</bean>
<bean id="testBean1" class="org.springframework.aop.framework.ProxyFactoryBean"
depends-on="testBean2">
<property name="target">
<bean class="test.beans.TestBean1" />
</property>
<property name="interceptorNames">
<list>
<value>testTransactionInterceptor</value>
</list>
</property>
</bean>
</beans>
传播需要新的
testTransactionInterceptor
将@Bean
声明移动到@Configuration
类
它将阻止一个bean方法调用两次@Manh,我不确定这是否是问题所在。目前,这两个bean都是在XML中定义的。这很奇怪,您是否可以共享XML和您的配置类(如果有的话)?