从 TransactionProxyFactoryBean 到声明式事务管理?
From TransactionProxyFactoryBean to a Declarative transaction management?
我想从使用 TransactionProxyFactoryBean 的旧式事务管理迁移到 spring 推荐的声明式事务管理。
这样就可以避免时不时出现的交易异常。
这是我的配置xml文件:
<beans xmlns=...>
<context:annotation-config/>
<context:component-scan base-package="prof" />
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>WEB-INF/classes/hibernate.cfg.xml</value>
</property>
</bean>
<import resource="prof-dao-spring.xml" />
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
...
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<bean id="ProfileService" parent="baseTransactionProxy">
<property name="target">
<bean class="tv.clever.hibernate.service.ProfileService"></bean>
</property>
</bean>
</beans>
ProfileService
看起来像:
@Component
public class ProfileService {
@Autowired
@Qualifier("baseDAO")
protected BaseDAO baseDAO;
private static ProfileService profileService;
public ProfileService() {
setProfileService(this);
}
public void setProfileService(ProfileService ps) {
profileService = ps;
}
public void save(final Collection transientObjects) {
baseDAO.save(transientObjects);
}
...
}
我需要从哪里开始?
假设你想在你的服务 class 上使用注解打一个 @Transactional
,添加 <tx:annotation-driven />
到你的配置并删除 TransactionalProxyFactoryBean
声明和所有使用它的 bean作为 parent。
额外pro-tips:
- 将
@Service
用于服务 classes,将 @Repository
用于 daos
<context:annotation-config />
隐含于 <context:component-scan />
您的服务
@Service
@Transactional
public class ProfileService { ... }
配置
<beans xmlns=...>
<context:component-scan base-package="prof" />
<import resource="prof-dao-spring.xml" />
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>WEB-INF/classes/hibernate.cfg.xml</value>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven />
</beans>
重新启动应用程序。
我想从使用 TransactionProxyFactoryBean 的旧式事务管理迁移到 spring 推荐的声明式事务管理。 这样就可以避免时不时出现的交易异常。
这是我的配置xml文件:
<beans xmlns=...>
<context:annotation-config/>
<context:component-scan base-package="prof" />
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>WEB-INF/classes/hibernate.cfg.xml</value>
</property>
</bean>
<import resource="prof-dao-spring.xml" />
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
...
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<bean id="ProfileService" parent="baseTransactionProxy">
<property name="target">
<bean class="tv.clever.hibernate.service.ProfileService"></bean>
</property>
</bean>
</beans>
ProfileService
看起来像:
@Component
public class ProfileService {
@Autowired
@Qualifier("baseDAO")
protected BaseDAO baseDAO;
private static ProfileService profileService;
public ProfileService() {
setProfileService(this);
}
public void setProfileService(ProfileService ps) {
profileService = ps;
}
public void save(final Collection transientObjects) {
baseDAO.save(transientObjects);
}
...
}
我需要从哪里开始?
假设你想在你的服务 class 上使用注解打一个 @Transactional
,添加 <tx:annotation-driven />
到你的配置并删除 TransactionalProxyFactoryBean
声明和所有使用它的 bean作为 parent。
额外pro-tips:
- 将
@Service
用于服务 classes,将@Repository
用于 daos <context:annotation-config />
隐含于<context:component-scan />
您的服务
@Service
@Transactional
public class ProfileService { ... }
配置
<beans xmlns=...>
<context:component-scan base-package="prof" />
<import resource="prof-dao-spring.xml" />
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>WEB-INF/classes/hibernate.cfg.xml</value>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven />
</beans>
重新启动应用程序。