Spring Java 配置上下文的事务配置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4139438/
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-10-30 04:58:18  来源:igfitidea点击:

Transaction configuration for Spring Java configured context

javaspringtransactionsannotationsspring-aop

提问by Eugen

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.

我正在尝试将我的 Spring XML 配置迁移到基于 Java 的完整配置。

I'm using AnnotationConfigApplicationContextas a context implementation.

AnnotationConfigApplicationContext用作上下文实现。

I'm having trouble finding an Java equivalent for this line, from my old XML configuration:

我无法从旧的 XML 配置中找到该行的 Java 等效项:

<tx:annotation-driven transaction-manager="transactionManager" />

As a result, Spring doesn't manage the transactions.

因此,Spring 不管理事务。

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.

在我的 Java 配置中,我已经为事务初始化了相关的 bean:会话工厂、事务管理器等,但是没有那一行,就没有使用事务代理,所以实际上没有事务就位。

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.

所以我的问题是如何将该行转换为我的 Java 上下文配置,或者如何以另一种方式解决问题。

Any help is appreciated. Thanks.

任何帮助表示赞赏。谢谢。

采纳答案by AHungerArtist

You can now use @EnableTransactionManagement.

您现在可以使用@EnableTransactionManagement。

See: 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 skaffman

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.

根据我的经验,用@Bean-style config完全替换 XML 配置是不切实际的。有些东西在 java 中配置更有意义,特别是你自己的 bean 定义。但是当涉及到像 那样的基础设施类型声明时<tx:annotation-driven>,XML 语法要简洁得多。

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

可以在纯 Java 中重现相同的效果,但最终会变得繁琐且不直观,因为诸如此类<tx:annotation-driven>的事情通常是您真的不想触及的复杂低级 Spring 基础结构类的交互。

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 @Configurationclasses as beans in that XML context, alongside things like <tx:annotation-driven>.

我的建议 - 混合搭配,使用 Java 和 XML 各取所长。这很容易做到。我更喜欢保留普通的 XML ApplicationContext 类,然后将我的@Configuration类声明为该 XML 上下文中的 bean,以及诸如<tx:annotation-driven>.

回答by Chris Beams

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.

看看https://spring.io/blog/2011/02/17/spring-3-1-m1-introducing-featurespecification-support。Spring 3.1 的 FeatureSpecification 类(例如 TxAnnotationDriven)旨在解决上述问题。