package com.lovo.advice; import java.sql.Connection; import java.util.Collection; import java.util.HashMap; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; import com.lovo.dao.BaseDAO; import com.lovo.jdbc.DBUtil; import com.lovo.service.BaseService; public class TransactionAdvice implements MethodInterceptor { public Object invoke(MethodInvocation arg0) throws Throwable { Object returnValue = null; Connection con = DBUtil.getConnection(); BaseService bs = (BaseService) arg0.getThis(); con.setAutoCommit(false); HashMap<String,BaseDAO> allDaos = bs.getAllDaos(); Collection<BaseDAO> allValues = allDaos.values(); for(BaseDAO bdao : allValues){ bdao.setCon(con); } try { returnValue = arg0.proceed(); con.commit(); } catch (Exception ex) { con.rollback(); }finally{ con.close(); } return returnValue; } } package com.lovo.dao; import java.sql.Connection; public class BaseDAO { private Connection con; public Connection getCon() { return con; } public void setCon(Connection con) { this.con = con; } } package com.lovo.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import com.lovo.jdbc.DBUtil; public class FirstDAO extends BaseDAO { public void addUser(String name, String password) throws SQLException { String sql = "insert into t_user values (?,?)"; PreparedStatement ps = this.getCon().prepareStatement(sql); ps.setString(1, name); ps.setString(2, password); ps.executeUpdate(); } } package com.lovo.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import com.lovo.jdbc.DBUtil; public class SecondDAO extends BaseDAO { public void modUser(int id, String name) throws SQLException { PreparedStatement pst = null; String sql = "update t_user set name=? where id=?"; pst = this.getCon().prepareStatement(sql); pst.setString(1, name); pst.setInt(2, id); pst.executeUpdate(); } } package com.lovo.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class DBUtil { public static Connection getConnection(){ Connection conn = null; try { Class.forName("net.sourceforge.jtds.jdbc.Driver"); conn=DriverManager.getConnection("jdbc:jtds:sqlserver://localhost:1433/test", "sa", "sa"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; } } package com.lovo.service; import java.util.ArrayList; import java.util.HashMap; import com.lovo.dao.BaseDAO; import com.lovo.dao.FirstDAO; import com.lovo.dao.SecondDAO; public class BaseService { private HashMap<String,BaseDAO> allDaos; public HashMap<String, BaseDAO> getAllDaos() { return allDaos; } public void setAllDaos(HashMap<String, BaseDAO> allDaos) { this.allDaos = allDaos; } } package com.lovo.service; import java.sql.SQLException; import com.lovo.dao.FirstDAO; import com.lovo.dao.SecondDAO; public class UserService extends BaseService{ public void execute1() throws SQLException{ ((FirstDAO)this.getAllDaos().get("fdao")).addUser("frjj", "frjf"); ((SecondDAO)this.getAllDaos().get("sdao")).modUser(1, "HHGL"); } } package com.lovo.test; import java.sql.SQLException; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.lovo.service.UserService; public class TestMain { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext contxt = new ClassPathXmlApplicationContext("applicationContext.xml"); UserService us = (UserService)contxt.getBean("usProxy"); try { us.execute1(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } XML <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="fdao" class="com.lovo.dao.FirstDAO"> </bean> <bean id="sdao" class="com.lovo.dao.SecondDAO"> </bean> <bean id="us" class="com.lovo.service.UserService"> <property name="allDaos"> <map> <entry key="fdao"> <ref bean="fdao"/> </entry> <entry key="sdao"> <ref bean="sdao"/> </entry> </map> </property> </bean> <bean id="transAdvice" class="com.lovo.advice.TransactionAdvice"> </bean> <bean id="transInterceptor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="advice" ref="transAdvice"></property> <property name="pattern" value="com.lovo.service.UserService.execute[0-9]"></property> </bean> <bean id="transProxyTemplate" abstract="true" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="interceptorNames"> <list> <value>transInterceptor</value> </list> </property> </bean> <bean id="usProxy" parent="transProxyTemplate"> <property name="target" ref="us"></property> </bean> </beans>