So in the latest version of Spring we are able to use the @Configuration
annotation to setup our configurations for Spring. Now in JavaConfig it is possible to use the @AnnotationDrivenTx
(@AnnotationDrivenTx Reference Link) annotation to setup transactions in our Config class. But since JavaConfig has been decommissioned I was wondering if anyone knew how to setup something similar without JavaConfig and without needing to add anything to the application-context.xml
. Here is what I basically have for my Config class
@Configuration
@ImportResource("config/application-context.xml")
public class Config {
public @Bean DataSource dataSource() {
//get and return datasource
}
public @Bean Service1 getService1() {
//return service1Impl
}
}
And I'd like to make Service1
transactional. If anyone has any ideas on how to do this or if this is just not possible please let me know.
Thanks!
One of the most important annotations in spring is @Configuration annotation which indicates that the class has @Bean definition methods. So Spring container can process the class and generate Spring Beans to be used in the application. This annotation is part of the spring core framework.
@Configuration annotation indicates that a class declares one or more @Bean methods and may be processed by the Spring container to generate bean definitions and service requests for those beans at runtime.
Annotation Type EnableTransactionManagement. Enables Spring's annotation-driven transaction management capability, similar to the support found in Spring's <tx:*> XML namespace. To be used on @Configuration classes to configure traditional, imperative transaction management or reactive transaction management.
The @Transactional annotation makes use of the attributes rollbackFor or rollbackForClassName to rollback the transactions, and the attributes noRollbackFor or noRollbackForClassName to avoid rollback on listed exceptions. The default rollback behavior in the declarative approach will rollback on runtime exceptions.
You can now use @EnableTransactionManagement
.
See this post for more details: http://blog.springsource.com/2011/06/10/spring-3-1-m2-configuration-enhancements/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With