Spring Java 配置上下文的事务配置
我已经为此苦苦挣扎了几个小时。
我正在尝试将 Spring XML 配置迁移到完整的基于 Java 的配置。
我使用 AnnotationConfigApplicationContext 作为上下文实现。
我无法从旧的 XML 配置中找到该行的 Java 等效项:
<tx:annotation-driven transaction-manager="transactionManager" />
因此,Spring 不管理事务。
在我的 Java 配置中,我已经初始化了事务的相关 bean:会话工厂、事务管理器等,但如果没有该行,就不会使用事务代理,因此实际上没有事务。
所以我的问题是如何将该行转换为我的 Java 上下文配置或如何以另一种方式解决问题。
任何帮助表示赞赏。 谢谢。
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.
I'm using AnnotationConfigApplicationContext
as a context implementation.
I'm having trouble finding an Java equivalent for this line, from my old XML configuration:
<tx:annotation-driven transaction-manager="transactionManager" />
As a result, Spring doesn't manage the transactions.
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.
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.
Any help is appreciated.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您现在可以使用@EnableTransactionManagement。
请参阅:http://blog.springsource.com/2011/06 /10/spring-3-1-m2-配置增强/
You can now use @EnableTransactionManagement.
See: http://blog.springsource.com/2011/06/10/spring-3-1-m2-configuration-enhancements/
根据我的经验,用
@Bean
样式的配置完全替换 XML 配置是不切实际的。有些东西在 java 中配置确实更有意义,特别是您自己的 bean 定义。但是,当涉及像
这样的基础结构类型声明时,XML 语法要简洁得多。您可以在纯java中重现相同的效果,但最终会变得麻烦且不直观,因为像
这样的东西通常是复杂的低层交互你真的不想接触的 Spring 基础设施类。我的建议 - 混合搭配,利用 Java 和 XML 各自的优势。这很容易做到。我更喜欢保留普通的 XML ApplicationContext 类,然后将我的
@Configuration
类声明为该 XML 上下文中的 bean,以及
等内容。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.You can reproduce 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.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
@Configuration
classes as beans in that XML context, alongside things like<tx:annotation-driven>
.看看 https://spring .io/blog/2011/02/17/spring-3-1-m1-introducing-featurespecation-support。 Spring 3.1的FeatureSpecification类(例如TxAnnotationDriven)旨在准确解决上述问题。
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.