方法一:通过切面配置
1、配置数据源
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://192.168.0.88:3306/product123"></property>
<property name="username" value="root"></property>
<property name="defaultAutoCommit" value="false"/>
<property name="password" value="x5"></property>
</bean>
2、配置事物管理器
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
事物管理器的 dataSource属性引用了数据源。
3、配置事物的通知
<tx:advice id="txAdvice" transaction-manager="transactionManager" >
<tx:attributes>
<tx:method name="*" isolation="READ_COMMITTED" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
通知的transactionManager属性引用了事物管理器,在<tx:attributes/>标签下可以定义传播行为隔离级别等信息。
4、把通知和切点关联
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.service..*(..))"/>
</aop:aspect>
通过标签<aop:advisor/>的pointcut属性指明事物要用在哪些类的哪些方法上。
方法二:通过注解配置
第一步和第二步同方法一
第三步:启动注解驱动
<tx:annotation-driven transaction-manager="transactionManager" />
第四补:给服务类加上事物注解@Transactional
@Transactional(isolation=Isolation.READ_COMMITTED)
@Service
@Scope(ConfigurableBeanFactory .SCOPE_PROTOTYPE)
public class EmployeeServiceImpl implements EmployeeService{
@Autowired
EmployeeDao employeeDao;
public void selectEmployee(Employee employee) {
employeeDao.selectEmployee(employee);
System.out.println("服务调用。。。");
}
}
@Transactional可以配置隔离级别,传播行为,回滚等属性