以前做項目都是拿同事寫好的spring配置文件直接使用,這次自己配置spring遇到了一些問題:在對spring進行聲明式事務驅動配置時,報“the prefix "tx" for element "tx:annotation-driven" is not bound“。明明已引入spring 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"
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/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 引入jdbc配置文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:jdbc.properties</value>
</list>
</property>
</bean>
<!-- dataSource 配置 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="driverClass" value="${jdbc.driverClass}" />
<property name="user" value="${jdbc.userName}" />
<property name="password" value="${jdbc.passWord}" />
<!--連接池中保留的最大連接數。Default: 10 -->
<property name="maxPoolSize" value="${jdbc.maxPoolSize}" />
<!--連接池中保留的最小連接數。 -->
<property name="minPoolSize" value="${jdbc.minPoolSize}" />
<!--初始化時獲取的連接數,取值應在minPoolSize與maxPoolSize之間。Default: 3 -->
<property name="initialPoolSize" value="${jdbc.initialPoolSize}" />
<!--最大空閒時間,60秒內未使用則連接被丟棄。若爲0則永不丟棄。Default: 0 -->
<property name="maxIdleTime" value="${jdbc.maxIdleTime}" />
<!--當連接池中的連接耗盡的時候c3p0一次同時獲取的連接數。Default: 3 -->
<property name="acquireIncrement" value="${jdbc.acquireIncrement}" />
<!--JDBC的標準參數,用以控制數據源內加載的PreparedStatements數量。但由於預緩存的statements 屬於單個connection而不是整個連接池。所以設置這個參數需要考慮到多方面的因素。
如果maxStatements與maxStatementsPerConnection均爲0,則緩存被關閉。Default: 0 -->
<property name="maxStatements" value="${jdbc.maxStatements}" />
<!--每60秒檢查所有連接池中的空閒連接。Default: 0 -->
<property name="idleConnectionTestPeriod" value="${jdbc.idleConnectionTestPeriod}" />
<!--定義在從數據庫獲取新連接失敗後重復嘗試的次數。Default: 30 -->
<property name="acquireRetryAttempts" value="${jdbc.acquireRetryAttempts}" />
<!--獲取連接失敗將會引起所有等待連接池來獲取連接的線程拋出異常。但是數據源仍有效 保留,並在下次調用getConnection()的時候繼續嘗試獲取連接。如果設爲true,那麼在嘗試
獲取連接失敗後該數據源將申明已斷開並永久關閉。Default: false -->
<property name="breakAfterAcquireFailure" value="${jdbc.breakAfterAcquireFailure}" />
<!--因性能消耗大請只在需要的時候使用它。如果設爲true那麼在每個connection提交的 時候都將校驗其有效性。建議使用idleConnectionTestPeriod或automaticTestTable 等方法來提升連接測試的性能。Default: false -->
<property name="testConnectionOnCheckout" value="${jdbc.testConnectionOnCheckou}" />
</bean>
select * from user where birth_date = ? and username = ? and age > ? 轉換後sql:
select * from user where birth_date = to_date('2010-08-13','yyyy-mm-dd')
and username = 'badqiu' and age > 20 -->
<bean id="log4jdbcInterceptor" class="net.sf.log4jdbc.DataSourceSpyInterceptor" />
<bean id="dataSourceLog4jdbcAutoProxyCreator"
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="interceptorNames">
<list>
<value>log4jdbcInterceptor</value>
</list>
</property>
<property name="beanNames">
<list>
<value>mysql_dataSource</value>
</list>
</property>
</bean>
<!-- myBatis配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="mysql_dataSource" />
<property name="mapperLocations">
<list>
<value>classpath:/cn/ctyun/portal/iam/dao/IamUserCredentialMapper.xml
</value>
</list>
</property>
</bean>
<!-- 事務管理器配置, 使用jdbc事務 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="mysql_dataSource" />
</bean>
<!-- 使用annotation定義事務 -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
</beans>麻利的上百度一陣搜索,無外乎是一堆copy來copy去的解決方法:定義aop的時候沒有加載schema(我明明加載了好嗎)。好歹哥們是過了四級的,找到個老外的網站,大概意思是未引入tx標籤定義,感覺這個靠譜,在文件頭加上:
xmlns:tx="http://www.springframework.org/schema/tx"
OK,問題解決。