14

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.

AHungerArtist
  • 9,332
  • 17
  • 73
  • 109
Eugen
  • 8,523
  • 8
  • 52
  • 74

3 Answers3

12

You can now use @EnableTransactionManagement.

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

AHungerArtist
  • 9,332
  • 17
  • 73
  • 109
2

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>.

skaffman
  • 398,947
  • 96
  • 818
  • 769
  • 1
    No, i don't think so. The annotation style is considerably richer in modern Spring versions than it was in Spring 3. – skaffman Dec 05 '14 at 01:31
  • Thanks for the update. That's what I suspected. We are attempting to update our legacy code to Spring 4, and as part of that process I would like to eliminate as much of our XML configuration as possible (as long as it makes sense, of course, but I feel that in most places the annotation-based approach is a better choice). – GaZ Dec 05 '14 at 09:00
0

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.

Chris Beams
  • 1,473
  • 10
  • 13