spring mvc @Transactional 註解 配置 步驟 及 @Transactional 事物無效 注意事項
阿新 • • 發佈:2018-12-10
- spring-context.xml檔案配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd" default-lazy-init="true"> <!-- 使用Annotation自動註冊Bean,不掃描@Controller註解--> <context:component-scan base-package="com.test.project"><!-- base-package 如果多個,用“,”分隔 --> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- 定義事務 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 配置 Annotation 驅動,掃描@Transactional註解的類定義事務 --> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/> </beans>
- spring-mvc.xml 配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> <!-- 自動掃描使用Annotation自動註冊Bean,只掃描@Controller --> <context:component-scan base-package="com.test.myproject" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> </beans>
use-default-filters="false" 一定要設定為false,否則事物無效
use-default-filters 屬性的預設值為 true,即使用預設的 Filter 進行包掃描,而預設的 Filter 對標有 @Service,@Controller和@Repository 的註解的類進行掃描,我們希望 SpringMVC 只來控制網站的跳轉邏輯,所以我們只希望 SpringMVC 的配置掃描 @Controllerce 註解標註的類,不希望它掃描其餘註解標註的類,所以設定了 use-default-filters 為 false,並使用 context:include-filter 子標籤設定其只掃描帶有 @Controller 註解標註的類。 而 Spring 就不同了,我們希望 Spring 只不掃描帶有 @Controller 註解標註的類,而掃描其他註解標註的類,而這時建立在使用預設的 Filter 進行掃描的基礎上,設定了 context:exclude-filter 標籤排除Controller