借助这些信息,我进行了一些调查,结果发现:Spring @Transaction无法启动事务。我放入
<tx:annotation-driven/>servlet-context.xml文件,然后突然启动了logDOI的事务,并且一切正常。我不再遇到LazyInitializationException。我不清楚为什么这样做有效。任何有关的信息将不胜感激。
更新:
我想到了。我的问题的关键在于servlet-context.xml文件。这是它的样子
<?xml version="1.0" encoding="UTF-8"?><beans:beans xmlns="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> <!-- Enables the Spring MVC @Controller programming model --> <annotation-driven /> <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> <resources mapping="/resources/**" location="/resources/" /> <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> <beans:bean > <beans:property name="prefix" value="/WEB-INF/views/" /> <beans:property name="suffix" value=".jsp" /> </beans:bean> <context:component-scan base-package="org.myapp.doi" /></beans:beans>主要的问题是在这种情况下:组件扫描线。Spring MVC创建两个实例化在其中实例化bean的上下文:使用web.xml文件中的contextConfigLocation参数定义的根应用程序上下文和在web.xml文件中的DispatcherServlet中定义的servlet上下文。Servlet上下文可以看到应用程序上下文,但反之则不行。现在,由于在servlet上下文中定义了context:component-scan并扫描了整个应用程序名称空间,因此我的DAO在servlet上下文中被实例化。但是,事务注释扫描是在应用程序上下文中完成的,因此无法从那里进行AOP代理的填充。只需修改servlet上下文中的context:component-scan即可仅扫描MVC控制器(
<context:component-scan base-package="org.myapp.doi.web" />固定一切;DAO是在应用程序上下文中创建的,并已正确设置事务。



