<?xml version="1.0" encoding="GBK"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-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">
<!-- 连接池 -->
<!-- <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> -->
<!-- <property name="jndiName"> -->
<!-- <value>java:jboss/datasources/ptDs</value> -->
<!-- </property> -->
<!-- </bean> -->
<!-- <bean id="dataSourceMer" class="org.springframework.jndi.JndiObjectFactoryBean"> -->
<!-- <property name="jndiName"> -->
<!-- <value>java:jboss/datasources/merDs</value> -->
<!-- </property> -->
<!-- </bean> -->
<!-- 数据库连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="maxPoolSize" value="10"></property>
<property name="minPoolSize" value="10"></property>
<property name="initialPoolSize" value="10"></property>
<property name="maxIdleTime" value="20"></property>
<property name="acquireIncrement" value="5"></property>
<property name="idleConnectionTestPeriod" value="60"></property>
<property name="preferredTestQuery" value="select 1"></property>
</bean>
<!-- Mybatis's sqlSessionFactory config -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
p:dataSource-ref="dataSource" p:configLocation="classpath:config/mybatis-config.xml" />
<!-- 缓存设置 -->
<bean id="distributeCache" class="com.kortide.ksfulife.distributecache.RedisCache" />
<!-- 启用注解式事务 -->
<!-- <tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager" /> -->
<bean name="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 启用声明式事务 -->
<!-- <tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager" /> -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="updateWelfare" propagation="REQUIRED" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config expose-proxy="true">
<!-- 只对业务逻辑层实施事务 -->
<aop:pointcut id="txPointcut" expression="execution(* com.kortide.serviceImpl.welfare..*.*(..))" />
<!-- Advisor定义,切入点和通知分别为txPointcut、txAdvice -->
<aop:advisor pointcut-ref="txPointcut" advice-ref="txAdvice"/>
</aop:config>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-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">
<!-- 连接池 -->
<!-- <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> -->
<!-- <property name="jndiName"> -->
<!-- <value>java:jboss/datasources/ptDs</value> -->
<!-- </property> -->
<!-- </bean> -->
<!-- <bean id="dataSourceMer" class="org.springframework.jndi.JndiObjectFactoryBean"> -->
<!-- <property name="jndiName"> -->
<!-- <value>java:jboss/datasources/merDs</value> -->
<!-- </property> -->
<!-- </bean> -->
<!-- 数据库连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="maxPoolSize" value="10"></property>
<property name="minPoolSize" value="10"></property>
<property name="initialPoolSize" value="10"></property>
<property name="maxIdleTime" value="20"></property>
<property name="acquireIncrement" value="5"></property>
<property name="idleConnectionTestPeriod" value="60"></property>
<property name="preferredTestQuery" value="select 1"></property>
</bean>
<!-- Mybatis's sqlSessionFactory config -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
p:dataSource-ref="dataSource" p:configLocation="classpath:config/mybatis-config.xml" />
<!-- 缓存设置 -->
<bean id="distributeCache" class="com.kortide.ksfulife.distributecache.RedisCache" />
<!-- 启用注解式事务 -->
<!-- <tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager" /> -->
<bean name="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 启用声明式事务 -->
<!-- <tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager" /> -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="updateWelfare" propagation="REQUIRED" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config expose-proxy="true">
<!-- 只对业务逻辑层实施事务 -->
<aop:pointcut id="txPointcut" expression="execution(* com.kortide.serviceImpl.welfare..*.*(..))" />
<!-- Advisor定义,切入点和通知分别为txPointcut、txAdvice -->
<aop:advisor pointcut-ref="txPointcut" advice-ref="txAdvice"/>
</aop:config>
</beans>
1.配置文件方式
其中主要配置中是tx:advice和aop:config两个配置节,以Spring AOP的方式实现事务管理。
tx:advice配置了事务的管理者是transactionManager,同时tx:method也规定了如果方法名匹配“add*”和“get*”方法时使用事务,propagation是设定事务的传播级别。除了updateWelfare方法,其他的方法的事务是只读的(典型地,对于只执行查询的事务你会将该属性设为true,如果出现了更新、插入或是删除语句时只读事务就会失败)
aop:config指定了一个aop:pointcut去引用上边的advice。