Spring Hibernate 延迟加载 - 无会话

Spring Hibernate Lazy loading - no session

我在服务调用时遇到了延迟加载问题,或者至少我认为我只是遇到了延迟加载问题...似乎是我遇到了会话问题 => 会话期间不可用孔服务调用。

这是我的服务来源:

@Service
public class MyServiceImpl implements MyService{

  @Autowired
  private MyRepository myRepository;

  @Override
  @Transactional
  public MyDTO find(String pk){
    MyObject o = myRepository.findByPK(pk);
    o.lazyCall();//Exception, but only at server call, tests runs without any problems
  }
}

我的测试-Class 注释为:

@Test
@ContextConfiguration (locations = { "classpath:spring-test-config.xml" })
@TransactionConfiguration (transactionManager="transactionManager", defaultRollback=true)
@Transactional
public class MyServiceTest
    extends AbstractTransactionalTestNGSpringContextTests{

并且测试运行良好。

但是如果我将我的东西部署到码头服务器中,我会收到此异常:

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.test.product.domain.ProductBase.productComponent, could not initialize proxy - no Session
 at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:575) ~[hibernate-core-4.3.9.Final.jar:4.3.9.Final]
 at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:214) ~[hibernate-core-4.3.9.Final.jar:4.3.9.Final]
 at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:554) ~[hibernate-core-4.3.9.Final.jar:4.3.9.Final]
 at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:142) ~[hibernate-core-4.3.9.Final.jar:4.3.9.Final]
 at org.hibernate.collection.internal.PersistentBag.iterator(PersistentBag.java:294) ~[hibernate-core-4.3.9.Final.jar:4.3.9.Final]
 at java.util.Collections$UnmodifiableCollection.<init>(Collections.java:1039) ~[na:1.8.0_25]
 at java.util.Collections$UnmodifiableCollection.iterator(Collections.java:1038) ~[na:1.8.0_25]
 at com.test.service.MyServiceImpl.find(MyServiceImpl.java:104) ~[My.service-0.0.1-SNAPSHOT.jar:na]
 at com.test.webservice.MyServiceController.create(MyServiceController.java:44) ~[classes/:na]
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_25]
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_25]
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_25]
 at java.lang.reflect.Method.invoke(Method.java:483) ~[na:1.8.0_25]
 at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221) ~[spring-web-4.1.6.RELEASE.jar:4.1.6.RELEASE]
 at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137) ~[spring-web-4.1.6.RELEASE.jar:4.1.6.RELEASE]
 at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110) ~[spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
 at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:776) ~[spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
 at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705) ~[spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
 at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
 at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959) ~[spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
 at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893) ~[spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
 at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966) [spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
 at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868) [spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:707) [javax.servlet-api-3.1.0.jar:3.1.0]
 at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842) [spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) [javax.servlet-api-3.1.0.jar:3.1.0]
 at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:816) [jetty-servlet-9.3.0.RC1.jar:9.3.0.RC1]
 at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1684) [jetty-servlet-9.3.0.RC1.jar:9.3.0.RC1]
 at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:316) [spring-security-web-4.0.1.RELEASE.jar:4.0.1.RELEASE]
 at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:126) [spring-security-web-4.0.1.RELEASE.jar:4.0.1.RELEASE]
 at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:90) [spring-security-web-4.0.1.RELEASE.jar:4.0.1.RELEASE]
 at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) [spring-security-web-4.0.1.RELEASE.jar:4.0.1.RELEASE]
 at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:114) [spring-security-web-4.0.1.RELEASE.jar:4.0.1.RELEASE]
 at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) [spring-security-web-4.0.1.RELEASE.jar:4.0.1.RELEASE]
 at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:122) [spring-security-web-4.0.1.RELEASE.jar:4.0.1.RELEASE]
 at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) [spring-security-web-4.0.1.RELEASE.jar:4.0.1.RELEASE]
 at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111) [spring-security-web-4.0.1.RELEASE.jar:4.0.1.RELEASE]
 at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) [spring-security-web-4.0.1.RELEASE.jar:4.0.1.RELEASE]
 at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:168) [spring-security-web-4.0.1.RELEASE.jar:4.0.1.RELEASE]
 at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) [spring-security-web-4.0.1.RELEASE.jar:4.0.1.RELEASE]
 at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:48) [spring-security-web-4.0.1.RELEASE.jar:4.0.1.RELEASE]
 at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) [spring-security-web-4.0.1.RELEASE.jar:4.0.1.RELEASE]
 at org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter.doFilter(DefaultLoginPageGeneratingFilter.java:162) [spring-security-web-4.0.1.RELEASE.jar:4.0.1.RELEASE]
 at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) [spring-security-web-4.0.1.RELEASE.jar:4.0.1.RELEASE]
 at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:205) [spring-security-web-4.0.1.RELEASE.jar:4.0.1.RELEASE]
 at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) [spring-security-web-4.0.1.RELEASE.jar:4.0.1.RELEASE]
 at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:120) [spring-security-web-4.0.1.RELEASE.jar:4.0.1.RELEASE]
 at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) [spring-security-web-4.0.1.RELEASE.jar:4.0.1.RELEASE]
 at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:64) [spring-security-web-4.0.1.RELEASE.jar:4.0.1.RELEASE]
 at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.1.6.RELEASE.jar:4.1.6.RELEASE]
 at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) [spring-security-web-4.0.1.RELEASE.jar:4.0.1.RELEASE]
 at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:53) [spring-security-web-4.0.1.RELEASE.jar:4.0.1.RELEASE]
 at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.1.6.RELEASE.jar:4.1.6.RELEASE]
 at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) [spring-security-web-4.0.1.RELEASE.jar:4.0.1.RELEASE]
 at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:91) [spring-security-web-4.0.1.RELEASE.jar:4.0.1.RELEASE]
 at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) [spring-security-web-4.0.1.RELEASE.jar:4.0.1.RELEASE]
 at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:213) [spring-security-web-4.0.1.RELEASE.jar:4.0.1.RELEASE]
 at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:176) [spring-security-web-4.0.1.RELEASE.jar:4.0.1.RELEASE]
 at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344) [spring-web-4.1.6.RELEASE.jar:4.1.6.RELEASE]
 at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261) [spring-web-4.1.6.RELEASE.jar:4.1.6.RELEASE]
 at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1667) [jetty-servlet-9.3.0.RC1.jar:9.3.0.RC1]
 at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:581) [jetty-servlet-9.3.0.RC1.jar:9.3.0.RC1]
 at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143) [jetty-server-9.3.0.RC1.jar:9.3.0.RC1]
 at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:548) [jetty-security-9.3.0.RC1.jar:9.3.0.RC1]
 at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:226) [jetty-server-9.3.0.RC1.jar:9.3.0.RC1]
 at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1121) [jetty-server-9.3.0.RC1.jar:9.3.0.RC1]
 at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:511) [jetty-servlet-9.3.0.RC1.jar:9.3.0.RC1]
 at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185) [jetty-server-9.3.0.RC1.jar:9.3.0.RC1]
 at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1055) [jetty-server-9.3.0.RC1.jar:9.3.0.RC1]
 at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) [jetty-server-9.3.0.RC1.jar:9.3.0.RC1]
 at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:213) [jetty-server-9.3.0.RC1.jar:9.3.0.RC1]
 at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:109) [jetty-server-9.3.0.RC1.jar:9.3.0.RC1]
 at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:118) [jetty-server-9.3.0.RC1.jar:9.3.0.RC1]
 at org.eclipse.jetty.server.Server.handle(Server.java:515) [jetty-server-9.3.0.RC1.jar:9.3.0.RC1]
 at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:291) [jetty-server-9.3.0.RC1.jar:9.3.0.RC1]
 at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:242) [jetty-server-9.3.0.RC1.jar:9.3.0.RC1]
 at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:238) [jetty-io-9.3.0.RC1.jar:9.3.0.RC1]
 at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:95) [jetty-io-9.3.0.RC1.jar:9.3.0.RC1]
 at org.eclipse.jetty.io.SelectChannelEndPoint.run(SelectChannelEndPoint.java:57) [jetty-io-9.3.0.RC1.jar:9.3.0.RC1]
 at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.produceAndRun(ExecuteProduceConsume.java:191) [jetty-util-9.3.0.RC1.jar:9.3.0.RC1]
 at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.run(ExecuteProduceConsume.java:126) [jetty-util-9.3.0.RC1.jar:9.3.0.RC1]
 at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:654) [jetty-util-9.3.0.RC1.jar:9.3.0.RC1]
 at org.eclipse.jetty.util.thread.QueuedThreadPool.run(QueuedThreadPool.java:572) [jetty-util-9.3.0.RC1.jar:9.3.0.RC1]
 at java.lang.Thread.run(Thread.java:745) [na:1.8.0_25]
2015-07-15 00:00:28,383 | DEBUG | qtp1971855969-26 | o.s.s.w.c.Sec

最后我的服务器数据源/tx 配置如下所示:

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${db.driver}" />
        <property name="url" value="${db.url}" />
        <property name="username" value="${db.username}" />
        <property name="password" value="${db.password}" />
    </bean>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">

        <property name="dataSource" ref="dataSource" />

        <property name="packagesToScan" >
            <list>
                <value>com.test</value>
            </list>
        </property>

        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="true" />
                <property name="generateDdl" value="true" />
            </bean>
        </property>

    </bean>

    <!-- Transaction Manager configuration -->
    <tx:annotation-driven transaction-manager="transactionManager"/>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" >
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

那有什么区别呢?为什么测试和服务器之间的行为不同?为什么我的服务方法中没有数据库会话,但是当我从数据库加载某些内容时却有一个会话?我阅读了一些有关 OpenSessionInViewFilter 的内容,但这似乎是前端应用程序的解决方案 => 但我想实现一个由前端调用的休息服务器。

有什么想法吗?请帮忙。

韩国

---更新--- 完整的配置文件:

root-context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jms="http://www.springframework.org/schema/jms"
    xmlns:amq="http://activemq.apache.org/schema/core"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/data/jpa
        http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util-3.0.xsd
        http://www.springframework.org/schema/jms 
        http://www.springframework.org/schema/jms/spring-jms.xsd
        http://activemq.apache.org/schema/core 
        http://activemq.apache.org/schema/core/activemq-core.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd"
    default-autowire="byName">

    <!-- Root Context: defines shared resources visible to all other web components -->

    <!-- only components from this package can be wired by spring --> 
    <context:component-scan base-package="com.test.product.*" />
    <context:component-scan base-package="com.test.person.*" />

    <!-- Directory to scan for repository classes -->
    <jpa:repositories base-package="com.test.*.domain.repository" />

    <!-- jdbc.properties => used to put db-connection data to an own property-file -->
    <bean id="domainPropertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
        <property name="locations">
            <list>
                <value>/WEB-INF/jdbc.properties</value>
            </list>
        </property>
    </bean>
    <!--  Datasource configuration - which database we use, look for jdbc.properties to set this data -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${db.driver}" />
        <property name="url" value="${db.url}" />
        <property name="username" value="${db.username}" />
        <property name="password" value="${db.password}" />
    </bean>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">

        <property name="dataSource" ref="dataSource" />

        <property name="packagesToScan" >
            <list>
                <value>com.test.product.domain</value>
                <value>com.test.person.domain</value>
            </list>
        </property>

        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="true" />
                <property name="generateDdl" value="true" />
            </bean>
        </property>

    </bean>

    <!-- Transaction Manager configuration -->
    <tx:annotation-driven transaction-manager="transactionManager"/>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" >
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <!-- Couch DB Configuration -->

    <!-- production and configuration or if you want to do a test to a real couch db (for example at continues integration) -->

    <util:properties id="couchdbProperties" location="/WEB-INF/couchdb.properties"/>

    <bean id="documentArchiveDatabase" class="org.ektorp.impl.StdCouchDbConnector">
        <constructor-arg value="documentArchive" />
        <constructor-arg>
            <bean id="couchDbInstance" class="org.ektorp.impl.StdCouchDbInstance">
                <constructor-arg>
                    <bean class="org.ektorp.spring.HttpClientFactoryBean" />
                </constructor-arg>
            </bean>
        </constructor-arg>
    </bean>

    <!-- START JMS Config -->

    <jms:annotation-driven/>

    <!-- Possible productive configuration -->


    <amq:broker id="broker" useJmx="false" persistent="false" >
        <amq:transportConnectors>
            <amq:transportConnector uri="tcp://localhost:61616"/>
        </amq:transportConnectors>
    </amq:broker>

    <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> 
        <property name="brokerURL" value="tcp://localhost:61616"/>
    </bean> 

    <bean id="cachingConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
        <property name="targetConnectionFactory" ref="connectionFactory"/>
    </bean> 

    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="cachingConnectionFactory"/>
    </bean>

    <bean id = "destinationResolver" class = "org.springframework.jms.support.destination.DynamicDestinationResolver"></bean>

    <bean id="jmsListenerContainerFactory" class="org.springframework.jms.config.DefaultJmsListenerContainerFactory">
        <property name="connectionFactory" ref="connectionFactory"/>
        <property name="destinationResolver" ref="destinationResolver"/>
        <property name="concurrency" value="1-10"/>
    </bean>

    <!-- END JMS Config -->


</beans>

Spring-Security.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:sec="http://www.springframework.org/schema/security"
    xsi:schemaLocation="
      http://www.springframework.org/schema/security
      http://www.springframework.org/schema/security/spring-security-4.0.xsd
      http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">

    <http entry-point-ref="restAuthenticationEntryPoint">
        <intercept-url pattern="/rest/**" access="hasRole('ROLE_USER')" />

        <form-login authentication-success-handler-ref="mySuccessHandler"
            authentication-failure-handler-ref="myFailureHandler" />
        <logout />
        <csrf disabled="true"/>
    </http>

    <beans:bean id="restAuthenticationEntryPoint" class="com.test.webservice.security.RestAuthenticationEntryPoint" />

    <beans:bean id="mySuccessHandler"
        class="com.test.webservice.security.RestAuthenticationSuccessHandler" />
    <beans:bean id="myFailureHandler"
        class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler" />

    <authentication-manager alias="authenticationManager">
        <authentication-provider>
            <user-service>
                <user name="temporary" password="temporary" authorities="ROLE_ADMIN" />
                <user name="user" password="user" authorities="ROLE_USER" />
            </user-service>
        </authentication-provider>
    </authentication-manager>

</beans:beans>

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"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

     <!-- Configure to plugin JSON as request and response in method handler -->
    <annotation-driven>
        <message-converters>
             <beans:bean class="org.springframework.http.converter.StringHttpMessageConverter">
             </beans:bean>
             <beans:bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <beans:property name="objectMapper">
                    <beans:bean class="com.test.webservice.MyObjectMapper" />
                </beans:property>
            </beans:bean>
        </message-converters>
    </annotation-driven>

    <!-- only components from this package can be wired by spring --> 
    <context:component-scan base-package="com.test.product.*" />
    <context:component-scan base-package="com.test.person.*" />


</beans:beans>

Web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app 
   xmlns="http://java.sun.com/xml/ns/javaee" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
   version="2.5"> 

   <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml, /WEB-INF/spring/spring-security.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <!-- Security Stuff -->
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

jdbc.properties

db.driver=org.h2.Driver
db.url=jdbc:h2:mem:test;DB_CLOSE_DELAY=-1
db.username=sa
db.password=

db.memurl=jdbc:h2:mem:test

您的堆栈跟踪可疑,对于企业应用程序来说太小了;) 但严重的是,我在堆栈跟踪中没有看到任何事务拦截器。 您使用的是正确的@Transactional 注释吗?你在为你的豆子使用组件扫描吗? (AFAIK packagesToScan 仅用于搜索实体,而不是 spring beans)它也可能是 Spring @Transactional not working.

中的问题

您的会话将在 @Transactional class 出现后被 spring TransactionManager 关闭。您正在 "productComponent" 中使用您的惰性方法。在出现 @Transactional calss 之前,您已经检索了所有数据。