No transactional EntityManager available

在项目中遇到一个这样的问题,在service层加了注解:@Transactional,仍然出现错误,依然会提示No transactional EntityManager available 。

在网上查了好久才找到原因。

原本在applicationContext.xml 配置文件中扫描包这样配置的:

 <context:component-scan base-package="com.test" >

在spring-mvc.xml 配置文件中扫描包这样配置的:

<context:component-scan base-package="com.test" >

改成如下即可。

applicationContext.xml 配置文件中排除Control层:

<context:component-scan base-package="com.test" >

    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />   </context:component-scan>

spring-mvc.xml 配置文件中,加入Control层,排除Service层:

<context:component-scan base-package="com.test">

      <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />   

    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" /> 

    </context:component-scan>

再经测试没问题

猜你喜欢

转载自1960370817.iteye.com/blog/2350741
今日推荐