Spring事务在controller层中没有用的原因以及解决方法
我们在进行SSM整合的时候,会按照spring和springMVC的配置文件分别分开配置
对于SpringMVC的DispatcherServlet会比spring的ContextLoaderListen加载的要慢
并且在spring的xml配置文件中在进行包扫描的时候会排除掉controller层
<context:component-scan base-package="com.ssmcon">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>`
因此如果只是在spring中配置事务 如下
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
则该事务@Transactional只会在service层中启用,对于controller层,没有作用,如果要想要在controller层中开启事务
要在springMvc的xml配置文件中加上如下事务配置
<mvc:annotation-driven/>
才能够在springMvc的controller开启事务。