关于这个主题已经有一些问题,但是没有任何响应可以提供参数来解释为什么我们不应该创建一个Spring MVC控制器Transactional.看到:
所以为什么?
背景:几年前,我在团队中使用C#/ NHibernate/Spring.Net实现了一个相当大的ERP软件.对服务器的往返实际上是这样实现的:事务在进入任何控制器逻辑之前打开,并在退出控制器后被提交或回滚.该交易在框架中进行管理,因此没有人必须关心它.这是一个出色的解决方案:稳定,简单,只有少数架构师不得不关心交易问题,团队的其他成员只是实现了功能.
从我的角度来看,这是我见过的最好的设计.当我尝试使用Spring MVC重现相同的设计时,我进入了一个带有延迟加载和事务问题的噩梦,每次都有相同的答案:不要让控制器进行事务处理,但为什么呢?
提前感谢您的答案!
在我的web.xml文件中,我有:
Run Code Online (Sandbox Code Playgroud)<!-- Creates the Spring Container shared by all Servlets and Filters --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>contextConfigLocation类路径:shared-context.xml
Run Code Online (Sandbox Code Playgroud)<!-- Main general mapping, i.e. context for the UI & version queries --> <servlet> <servlet-name>mainDispacherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:web-application-context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mainDispacherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- Processes application requests for version 1 --> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:version-api-contexts/application-context[v1.0].xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/api/v1.0/*</url-pattern> </servlet-mapping>
在父上下文中shared-context.xml,我有:
<aop:aspectj-autoproxy/>
<context:component-scan base-package="com.company.cse.support.audit"/>
Run Code Online (Sandbox Code Playgroud)
该包com.company.cse.support.audit包含一个标有@Component和@Aspect的类.此方面将消息记录到我的审核日志中.
当我在子上下文中定义的bean上调用一个方法时version-api-contexts/application-context[v1.0].xml,bean不会被spring AOP代理,并且不会调用该方面.如果我将方面定义行移动<context:component-scan base-package="com.company.cse.support.audit"/>
到子上下文XML,则方面可以正常工作. …