Mittwoch, 3. Dezember 2008

Annotation-driven transactions

Instead of programmatically implementing code or configuring Spring applicaton context xml files to manage transactions, it can simply be done by using Spring's transaction annotations. There's really not much to it. All you need to do is add the following to your Spring context:

<tx:annotation-driven />

Additionally ensure that the tx namespace is defined as per the xsi:schemaLocation

<beans xmlns="" xmlns:xsi="" xmlns:aop="" xmlns:tx="" xmlns:context=""
default-lazy-init="true" />

And now to the fun part! Add annotations to your code. I prefer to apply annotations to interfaces. That way all implementations of the interface are transactional.

...and Bob's your uncle!

1 Kommentar:

Karsten Gresch hat gesagt…


the Spring team recommends *not* using @Transactional in interfaces but only in concrete classes.

Reason is that you are bound to interface-based proxies. As annotations are not inherited you wouldn't be able to use cglib proxies or weaving-based aspects. Check the reference, and rock on, star :-)

Regards from Berlin,