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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3722310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 03:55:16  来源:igfitidea点击:

Setting Up Annotation Driven Transactions in Spring in @Configuration Class

javaspringconfigurationtransactionsspring-transactions

提问by Ian Dallas

So in the latest version of Spring we are able to use the @Configurationannotation 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

因此,在最新版本的 Spring 中,我们可以使用@Configuration注释来设置 Spring 的配置。现在在 JavaConfig 中,可以使用@AnnotationDrivenTx@AnnotationDrivenTx 参考链接)注释在我们的 Config 类中设置事务。但是由于 JavaConfig 已经退役,我想知道是否有人知道如何在没有 JavaConfig 的情况下设置类似的东西并且不需要在application-context.xml. 这是我的 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 Service1transactional. If anyone has any ideas on how to do this or if this is just not possible please let me know.

我想进行Service1交易。如果有人对如何做到这一点有任何想法,或者如果这根本不可能,请告诉我。

Thanks!

谢谢!

采纳答案by AHungerArtist

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/

回答by Sean Patrick Floyd

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 @Configurationclasses in Spring 3.1, but in the meantime, the recommended approach is to use @ImportResourceto include a snippet of XML that declares <tx:annotation-driven/>

@Configuration在 Spring 3.1 的类中可能有更一流的机制来启用注解驱动的 TX ,但同时,推荐的方法是使用@ImportResource包含声明的 XML 片段 <tx:annotation-driven/>

Wait: but you seem to have an XML context anyway. Why not add <tx:annotation-driven/>to it and use @Transactional?

等等:但你似乎有一个 XML 上下文。为什么不添加<tx:annotation-driven/>并使用@Transactional

回答by Chris Beams

Take a look at 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.

看看http://blog.springsource.com/2011/02/17/spring-3-1-m1-featurespec。Spring 3.1 的 FeatureSpecification 类(例如 TxAnnotationDriven)旨在解决上述问题。