<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 声明通知,它必须要引用一个txManager
transaction-manager:默认值为transactionManager
-->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="update" propagation="REQUIRED"/>
<tx:method name="save" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- 声明切面与切点的整合体
-->
<aop:config>
<!-- 切面加到接口上 -->
<aop:pointcut expression="execution(* cn.itcast..service.*Service.*(..))" id="cut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="cut"/>
</aop:config>
附:spring完整配置文件
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- 引用外部的配置文件 -->
<context:property-placeholder
location="jdbc.properties"/>
<!-- 声明dataSource,建议使用c3p0 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${d}"></property>
<property name="url" value="${r}"></property>
<property name="password" value="${p}"></property>
<property name="username" value="${u}"></property>
</bean>
<!-- 必须要配置事务的管理者 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 声明通知,它必须要引用一个txManager
transaction-manager:默认值为transactionManager
-->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="update" propagation="REQUIRED"/>
<tx:method name="save" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- 声明切面与切点的整合体
-->
<aop:config>
<!-- 切面加到接口上 -->
<aop:pointcut expression="execution(* cn.itcast..service.*Service.*(..))" id="cut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="cut"/>
</aop:config>
<!-- 以下是用户的配置。。。。。。 -->
<!-- 配置一个分层的数据操作以下是Service层,里面直接声明Bean -->
<bean id="studService" class="cn.itcast.stud.service.StudServiceImpl">
<property name="dao1">
<bean class="cn.itcast.stud.dao.Stud1DaoJdbc">
<property name="dataSource" ref="dataSource"></property>
</bean>
</property>
<property name="dao2">
<bean class="cn.itcast.stud.dao.Stud2DaoJdbc">
<property name="dataSource" ref="dataSource"></property>
</bean>
</property>
</bean>
<!-- 以下配置Action -->
<bean id="studAction" class="cn.itcast.stud.StudAction">
<property name="service" ref="studService"></property>
</bean>
</beans>