开发者

Transaction configuration for Spring Java configured context

开发者 https://www.devze.com 2023-01-24 20:35 出处:网络
I\'ve been struggling with this for a few hours now. I\'m trying to migrate my Spring XML configuration to a full Java based configuration.

I've been struggling with this for a few hours now.

I'm trying to migrate my Spring XML configuration to a full Java based configuration.

I'm using AnnotationConfigApplicationContext as a context implementation.

I'm having trouble finding an Java equivalent for this line, from my old XML configuration:

<tx:annotation-driven transaction-manager="transactionManager" />

As a result, Spring doesn't manage the transactions.

In my Java configuration I have initialized the relevant beans for transactions: the session factory, the transactional manager, etc, but without that line, no transaction proxy is used, so no transactions are actually in place.

So开发者_开发问答 my question is how do I either translate that line to my Java context configuration or how to I go about solving the problem in another way.

Any help is appreciated. Thanks.


You can now use @EnableTransactionManagement.

See: http://blog.springsource.com/2011/06/10/spring-3-1-m2-configuration-enhancements/


In my experience, it's not practical to entirely replace the XML config with @Bean-style config. Some things do make more sense configured in java, specifically your own bean definitions. But when it comes to infrastructural-type declarations like <tx:annotation-driven>, the XML syntax is a lot more concise.

You can reproduce the same effect in pure java, but it ends up being cumbersome and unintuitive, since things like <tx:annotation-driven> are typically interactions of complex low-level Spring infrastructure classes that you really don't want to touch.

My advice - mix and match, using each of Java and XML for their own strengths. This is quite easy to do. I prefer to keep the normal XML ApplicationContext classes, and then declare my @Configuration classes as beans in that XML context, alongside things like <tx:annotation-driven>.


Take a look at https://spring.io/blog/2011/02/17/spring-3-1-m1-introducing-featurespecification-support. Spring 3.1's FeatureSpecification classes such as TxAnnotationDriven are designed to solve exactly the problem described above.

0

精彩评论

暂无评论...
验证码 换一张
取 消