Spring-servlet.xml和application-context.xml中的@Transactional

时间:2020-12-31 21:03:55

In our web application we have many contextes. In one of them tx:annotation-driven is enabled. As result I have possibility to use @Transactional annotation around the project, except beans created and scanned (@Controller, and related @Service, @Component, etc..) in spring-servlet.xml (for one servlet's app context as was disscussed in Difference between applicationContext.xml and spring-servlet.xml in Spring Framework).

在我们的Web应用程序中,我们有许多上下文。在其中一个中,tx:启用了注释驱动。因此,我可以在项目周围使用@Transactional注释,除了在spring-servlet.xml中创建和扫描的bean(@ Controller,以及相关的@ Service,@ Component等等)(对于一个servlet的应用程序上下文,如同讨论的那样)在Spring Framework中的applicationContext.xml和spring-servlet.xml之间的区别。

My goals is to additionally enable this feature for beans related to spring-servlet.xml. I can try to do this with adding tx:annotation-driven, but based on this topic spring multiple transaction manager issue such configuration considered not valid.

我的目标是为与spring-servlet.xml相关的bean另外启用此功能。我可以尝试通过添加tx:annotation-driven来做到这一点,但基于这个主题,spring多个事务管理器发出这样的配置被认为是无效的。

Could someone suggest in which way I can enable @Transactional for beans from spring-servlet.xml

有人建议我可以通过哪种方式从spring-servlet.xml为bean启用@Transactional

1 个解决方案

#1


I'm not sure whether I properly understand your setup but I assume that you have have a spring web application with multiple servlets; each servlet has it's own spring context and they all have the same parent spring context in common: application-context.xml.

我不确定我是否正确理解你的设置,但我认为你有一个带有多个servlet的spring web应用程序;每个servlet都有自己的spring上下文,它们都有相同的父Spring语境:application-context.xml。

If this is your setup, you should define your PlatformTransactionManager once in application-context.xml; as it's defined in the parent context it's automatically available in every servlet spring context. Then you should be able to use tx:annotation-driven in every servlet spring context and in the parent context as well without any problems as long as they use the same transaction manager.

如果这是您的设置,您应该在application-context.xml中定义一次PlatformTransactionManager;因为它在父上下文中定义,它在每个servlet spring上下文中自动可用。然后你应该能够在每个servlet spring上下文和父上下文中使用tx:annotation-driven,只要它们使用相同的事务管理器就没有任何问题。

It's not required to define a transaction manager in every context where you want to use tx:annotation-driven.

在您要使用tx:annotation-driven的每个上下文中都不需要定义事务管理器。

#1


I'm not sure whether I properly understand your setup but I assume that you have have a spring web application with multiple servlets; each servlet has it's own spring context and they all have the same parent spring context in common: application-context.xml.

我不确定我是否正确理解你的设置,但我认为你有一个带有多个servlet的spring web应用程序;每个servlet都有自己的spring上下文,它们都有相同的父Spring语境:application-context.xml。

If this is your setup, you should define your PlatformTransactionManager once in application-context.xml; as it's defined in the parent context it's automatically available in every servlet spring context. Then you should be able to use tx:annotation-driven in every servlet spring context and in the parent context as well without any problems as long as they use the same transaction manager.

如果这是您的设置,您应该在application-context.xml中定义一次PlatformTransactionManager;因为它在父上下文中定义,它在每个servlet spring上下文中自动可用。然后你应该能够在每个servlet spring上下文和父上下文中使用tx:annotation-driven,只要它们使用相同的事务管理器就没有任何问题。

It's not required to define a transaction manager in every context where you want to use tx:annotation-driven.

在您要使用tx:annotation-driven的每个上下文中都不需要定义事务管理器。