简体   繁体   English

在@Configuration类中设置Spring中的注释驱动事务

[英]Setting Up Annotation Driven Transactions in Spring in @Configuration Class

So in the latest version of Spring we are able to use the @Configuration annotation to setup our configurations for Spring. 因此,在最新版本的Spring中,我们可以使用@Configuration注释来设置Spring的配置。 Now in JavaConfig it is possible to use the @AnnotationDrivenTx ( @AnnotationDrivenTx Reference Link) annotation to setup transactions in our Config class. 现在在JavaConfig中,可以使用@AnnotationDrivenTx@AnnotationDrivenTx Reference Link)注释在Config类中设置事务。 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 . 但是由于JavaConfig已经退役,我想知道是否有人知道如何在没有JavaConfig的情况下设置类似的东西,而无需向application-context.xml添加任何东西。 Here is what I basically have for my Config class 这是我基本上用于Config类的内容

@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. 我想使Service1事务性的。 If anyone has any ideas on how to do this or if this is just not possible please let me know. 如果有人对如何做到这一点有任何想法,或者如果这是不可能的,请告诉我。

Thanks! 谢谢!

You can now use @EnableTransactionManagement . 您现在可以使用@EnableTransactionManagement

See this post for more details: http://blog.springsource.com/2011/06/10/spring-3-1-m2-configuration-enhancements/ 有关更多详细信息,请参阅此帖子: http//blog.springsource.com/2011/06/10/spring-3-1-m2-configuration-enhancements/

It seems like it isn't possible according to this forum post : 根据这个论坛帖子似乎不可能:

there may be a more first-class mechanism for enabling annotation-driven TX in @Configuration classes in Spring 3.1, but in the meantime, the recommended approach is to use @ImportResource to include a snippet of XML that declares <tx:annotation-driven/> 可能有更多的第一类机制在Spring 3.1中的@Configuration类中启用注释驱动的TX,但与此同时,推荐的方法是使用@ImportResource包含一段声明<tx:annotation-driven/>的XML片段<tx:annotation-driven/>

Wait: but you seem to have an XML context anyway. 等等:但你似乎还有一个XML上下文。 Why not add <tx:annotation-driven/> to it and use @Transactional ? 为什么不向它添加<tx:annotation-driven/>并使用@Transactional

Take a look at http://blog.springsource.com/2011/02/17/spring-3-1-m1-featurespec . 请查看http://blog.springsource.com/2011/02/17/spring-3-1-m1-featurespec Spring 3.1's FeatureSpecification classes such as TxAnnotationDriven are designed to solve exactly the problem described above. Spring 3.1的FeatureSpecification类(如TxAnnotationDriven)旨在解决上述问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM