最近項目用到了Spring、Tomcat7自帶的數據庫連接池tomcat jdbc pool 、mybatis,現將三者集成的配置記錄一下,以備不時之需。
所需的jar包:
spring框架需要的jar包(這裏就不詳細說了)
mybatis-3.1.1.jar
mybatis-spring-1.1.1.jar
mysql-connector-java-5.1.15-bin.jar
tomcat-jdbc.jar
tomcat-juli.jar
配置文件如下:
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!--tomcat jdbc pool數據源配置-->
<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
<property name="poolProperties">
<bean class="org.apache.tomcat.jdbc.pool.PoolProperties">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value=""/>
<property name="jmxEnabled" value="true"/>
<property name="testWhileIdle" value="true"/>
<property name="testOnBorrow" value="true"/>
<property name="testOnReturn" value="false"/>
<property name="validationInterval" value="30000"/>
<property name="validationQuery" value="SELECT 1"/>
<property name="timeBetweenEvictionRunsMillis" value="30000"/>
<property name="maxActive" value="100"/>
<property name="initialSize" value="10"/>
<property name="maxWait" value="10000"/>
<property name="minEvictableIdleTimeMillis" value="30000"/>
<property name="minIdle" value="10"/>
<property name="logAbandoned" value="false"/>
<property name="removeAbandoned" value="true"/>
<property name="removeAbandonedTimeout" value="60"/>
<property name="jdbcInterceptors" value="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer"/>
</bean>
</property>
</bean>
<!-- 配置事務管理器,注意這裏的dataSource和SqlSessionFactoryBean的dataSource要一致,不然事務就沒有作用了 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 定義攔截器,用來指定事務屬性、級別和異常處理 -->
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager"></property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="delete*"> PROPAGATION_REQUIRED,-Exception</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<!-- 用來定義哪些類需要事務管理 spring事務動態代理類 BeanNameAutoProxyCreator 根據類名自動代理,接受表達式 -->
<bean id="BeanProxy" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<!-- 對類名以Service結尾的類進行代理 -->
<value>*Service</value>
</property>
<!-- 對代理類進行加載攔截器(實現通知的過程) -->
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis-config.xml" />
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="authorityDao" class="com.hongdian.ithings.authoritymanagement.dao.AuthorityDaoImpl">
<property name="sessionFactory" ref="sqlSessionFactory"/>
</bean>
<bean id="authorityService" class="com.hongdian.ithings.authoritymanagement.service.AuthorityServiceImpl">
<property name="authorityDao" ref="authorityDao"/>
</bean>
<bean class="com.hongdian.ithings.authoritymanagement.common.SpringContext"></bean>
<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<!--線程池活躍的線程數-->
<property name="corePoolSize" value="5" />
<!--線程池最大活躍的線程數-->
<property name="maxPoolSize" value="50" />
<!--任務隊列的最大容量-->
<property name="queueCapacity" value="5" />
</bean>
<bean id="authorityComponent" class="com.hongdian.ithings.authoritymanagement.communication.AuthorityComponent">
<property name="taskExecutor" ref="taskExecutor"/>
</bean>
</beans>