今天在联系项目的时候,测试事务的时候,发现Spring管理的事务不起作用,百度了一大圈,终于发现了问题所在.
项目结构图:
修改前的application.xml
<?xml version = "1.0" encoding = "utf-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd ">
<!-- 加载数据库文件 -->
<context:property-placeholder location="classpath:db.properties" />
<!--注入Druid连接池数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<property name="url" value="${jdbc_url}" />
<property name="username" value="${jdbc_user}" />
<property name="password" value="${jdbc_password}" />
<property name="driverClassName" value="${jdbc_driver}" />
<property name="filters" value="stat" />
<property name="maxActive" value="20" />
<property name="initialSize" value="1" />
<property name="maxWait" value="60000" />
<property name="minIdle" value="1" />
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="validationQuery" value="SELECT 'x'" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="poolPreparedStatements" value="true" />
<property name="maxPoolPreparedStatementPerConnectionSize"
value="20" />
</bean>
<!-- 注入SqlSessionFactory -->
<bean name="sqlsessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入sqlMapConfig.xml -->
<property name="configLocation" value="classpath:sqlMapConfig.xml"></property>
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- Mapper动态扫面注入对象 -->
<bean class = "org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.tangbaobao.crm.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlsessionFactory"></property>
</bean>
<!-- 配置事务 -->
<bean name="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 开启注解事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
修改前的spmvc-servlet.xml
<?xml version = "1.0" encoding = "utf-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd ">
<!-- 扫描包,可以识别注解 -->
<context:component-scan base-package="com.tangbaobaor"></context:component-scan>
<!-- 配置处理器映射器 -->
<!-- 配置处理器适配器 -->
<!-- 用注解驱动 -->
<mvc:annotation-driven />
<!-- 配置视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/files/" />
<property name="suffix" value=".jsp"></property>
</bean>
<!--拦截器 -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/customer/**"/>
<mvc:mapping path="/message/**"/>
<bean class="com.tangbaobao.crm.interceptor.LoginInceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
</beans>
修改后:
application.xml
<?xml version = "1.0" encoding = "utf-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd ">
<!-- 加载数据库文件 -->
<context:property-placeholder location="classpath:db.properties" />
<!--注入Druid连接池数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<property name="url" value="${jdbc_url}" />
<property name="username" value="${jdbc_user}" />
<property name="password" value="${jdbc_password}" />
<property name="driverClassName" value="${jdbc_driver}" />
<property name="filters" value="stat" />
<property name="maxActive" value="20" />
<property name="initialSize" value="1" />
<property name="maxWait" value="60000" />
<property name="minIdle" value="1" />
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="validationQuery" value="SELECT 'x'" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="poolPreparedStatements" value="true" />
<property name="maxPoolPreparedStatementPerConnectionSize"
value="20" />
</bean>
<!-- 注入SqlSessionFactory -->
<bean name="sqlsessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入sqlMapConfig.xml -->
<property name="configLocation" value="classpath:sqlMapConfig.xml"></property>
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- Mapper动态扫面注入对象 -->
<bean class = "org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.tangbaobao.crm.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlsessionFactory"></property>
</bean>
<!-- 配置Spring管理容器 -->
<context:component-scan base-package="com.tangbaobao"></context:component-scan>
<!-- 配置事务 -->
<bean name="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 开启注解事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
修改后spv-servlet.xml
<?xml version = "1.0" encoding = "utf-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd ">
<!-- 扫描包,可以识别注解 -->
<context:component-scan base-package="com.tangbaobao.crm.controller"></context:component-scan>
<context:component-scan base-package="com.tangbaobao.common.utils"></context:component-scan>
<!-- 配置处理器映射器 -->
<!-- 配置处理器适配器 -->
<!-- 用注解驱动 -->
<mvc:annotation-driven />
<!-- 配置视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/files/" />
<property name="suffix" value=".jsp"></property>
</bean>
<!--拦截器 -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/customer/**"/>
<mvc:mapping path="/message/**"/>
<bean class="com.tangbaobao.crm.interceptor.LoginInceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
</beans>
总结
SpringMVC的配置文件中,配置的扫描包值用来扫描Controller层的,所以官方建议,只在springMVC的配置文件中配置视图层的
application.xml配置全局的包,这样就可以啦