<?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:util="http://www.springframework.org/schema/util" 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-2.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd" default-autowire="byName"> <!-- ================================================================ --> <!-- 加载属性文件 --> <!-- ================================================================ --> <bean id="jdbcConfiguration" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:jdbc-config.properties</value> </list> </property> </bean> <!-- ================================================================ --> <!-- 配置数据源 --> <!-- ================================================================ --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" p:driverClassName="${jdbc_driver}" p:url="${jdbc_url}" p:username="${username}" p:password="${password}" /> <!-- ================================================================ --> <!-- 事务配置 --> <!-- ================================================================ --> <!-- Transaction manager for a single JDBC DataSource --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" /> <!-- 如果采用XML配置则用下面的配置 --> <aop:config proxy-target-class="true"> <aop:advisor pointcut="execution(* com.epeer.service.*Impl.*(..))" advice-ref="txAdvice" /> </aop:config> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="insert*" no-rollback-for="java.lang.Throwable" /> <tx:method name="remove*" /> <tx:method name="add*" no-rollback-for="java.lang.Throwable" /> <tx:method name="*" read-only="true" /> </tx:attributes> </tx:advice> <!-- 如果采用XML配置则用上面的配置 --> <!-- 如果采用标注则去掉上面的配置,加入下面一行 --> <!--<tx:annotation-driven transaction-manager="transactionManager"/> --> <!-- ================================================================ --> <!-- 事务配置 --> <!-- ================================================================ --> <!-- Transaction manager for a single JDBC DataSource --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" /> <tx:annotation-driven transaction-manager="transactionManager" /> <!-- ================================================================ --> <!-- DAO配置 --> <!-- ================================================================ --> <bean id="userDao" class="com.epeer.dao.UserDaoImpl" /> <!-- ================================================================ --> <!-- Service配置 --> <!-- ================================================================ --> <bean id="userService" class="com.epeer.service.UserServiceImpl" /> </beans>
采用标注的话UserServiceImpl如下:
package com.epeer.service; import org.springframework.transaction.annotation.Transactional; import com.epeer.dao.UserDao; @Transactional public class UserServiceImpl implements UserService { private UserDao userDao; public UserDao getUserDao() { return userDao; } public void setUserDao(UserDao userDao) { this.userDao = userDao; } @Transactional(noRollbackForClassName = "java.lang.Throwable") public void insertUserIp(String startip, String endip) { userDao.insertUserIp("192.168.0.3", "0.0.0.0"); } }