applicationContext,xml
<!-- 导入资源文件 --> <context:property-placeholder location="classpath:db.properties"/> <!-- 配置 C3P0 数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property> <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property> </bean> <!-- 1. 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 2. 配置事务属性 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!--使用 propagation 指定事务的传播行为,当前事务方法被另一个事务方法调用时如何使用事务,默认值 REQUIRED,使用调用方法的事务 REQUIRES_NEW:事务自己的事务,调用的事务方法的事务挂起--> <tx:method name="purchase" propagation="REQUIRES_NEW"></tx:method> <!-- 方法名 get,find 开始的方法 --> <tx:method name="get*" read-only="true"></tx:method> <tx:method name="find*" read-only="true"></tx:method> <tx:method name="*"/> </tx:attributes> </tx:advice> <!-- 3. 配置切入点,以及把事务切入点和事务属性关联起来 --> <aop:config> <aop:pointcut id="txPointCut" expression="execution(* demo.tx.BookShopService.*(..))"></aop:pointcut> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"></aop:advisor> </aop:config>