静态代理和动态代理的例子
1.静态代理
静态代理 时序图:
[img]
[/img]
静态的代理
存在的问题:
1.需要建立大量的代理类
2.重复的代码会出现各个角落里。
接口
package com.test.dao;public interface UserDao {public void addUser(String userId,String userName);public String findById(String id);public void modifyUser(String userId,String userName);}
package com.test.dao;public class userDaoImpl implements UserDao {public void addUser(String userId, String userName) {System.out.println("userDaoImpl----->addUser()"+userName);}public String findById(String id) {// TODO Auto-generated method stubreturn null;}public void modifyUser(String userId, String userName) {// TODO Auto-generated method stub}}
package com.test.dao;public class userDaoImpl implements UserDao {public void addUser(String userId, String userName) {System.out.println("userDaoImpl----->addUser()"+userName);}public String findById(String id) {// TODO Auto-generated method stubreturn null;}public void modifyUser(String userId, String userName) {// TODO Auto-generated method stub}}
package com.test.dao;public class Client {/** * @param args */public static void main(String[] args) {UserDao userDao= new UserProxyImpl(new userDaoImpl());userDao.addUser("a001", "郭靖");}}
package com.test.dao;public interface UserDao {public void addUser(String userId,String userName);public String findById(String id);public void modifyUser(String userId,String userName);}
package com.test.dao;public class userDaoImpl implements UserDao {public void addUser(String userId, String userName) {System.out.println("userDaoImpl----->addUser()"+userName);}public String findById(String id) {// TODO Auto-generated method stubreturn null;}public void modifyUser(String userId, String userName) {// TODO Auto-generated method stub}}
package com.test.dao;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;public class LogHendler implements InvocationHandler {//声明目标对象 所有类用继承之Objectprivate Object targetObject; //根据转入的对象创建代理类public Object newProxyObject(Object targetObject){this.targetObject=targetObject;//返回一个指定接口的代理类实例,该接口可以将方法调用指派到指定的调用处理程序return Proxy.newProxyInstance(targetObject.getClass().getClassLoader(), targetObject.getClass().getInterfaces(), this);}/** * proxy - 在其上调用方法的代理实例 * method - 对应于在代理实例上调用的接口方法的 Method 实例 * args - 包含传入代理实例上方法调用的参数值的对象数组 */public Object invoke(Object proxy, Method method, Object[] args)throws Throwable { System.out.println("代理start()------>"+method.getName()); //取方法参数个数 for(int i=0;i<args.length;i++){System.out.println(args[i]); } //用作返回 Object ret=null; try{ //调用对象方法,如果有返回则存入ret中返回结果 ret=method.invoke(this.targetObject, args); System.out.println("代理success------>"+method.getName()); }catch(Exception e){ e.printStackTrace(); System.out.println("调用代理------>失败!"); }return ret;}}
package com.test.dao;public class Client {/** * @param args */public static void main(String[] args) {//UserDao userDao= new UserProxyImpl(new UserDaoImpl());////userDao.addUser("a001", "郭靖");LogHendler hendler =new LogHendler();//根据真实的类生成代理对象UserDao userDao=(UserDao)hendler.newProxyObject(new UserDaoImpl());userDao.addUser("aoo1", "郭靖");}}
public class TransactionHandler implements InvocationHandler {//取得目标对象private Object targetObject;public Object newProxyObject(Object targetObject){this.targetObject=targetObject;//根据传入的对象产生目标对象return Proxy.newProxyInstance(targetObject.getClass().getClassLoader(), targetObject.getClass().getInterfaces(), this);}public Object invoke(Object proxy, Method method, Object[] args)throws Throwable {Object ret=null;Connection conn=null;try{conn=ConnectionManager.getConnection();//只对add,modify,del开头的方法,开启事务,对find开头方法不开启事务if(method.getName().startsWith("add")||method.getName().startsWith("modify")||method.getName().startsWith("del")){//开启事务ConnectionManager.setAutoCommit(conn, false);}//调用目标方法,返回结果ret=method.invoke(targetObject, args);//如果是自动提交,则不要再重复提交if(!conn.getAutoCommit()){//提交事务ConnectionManager.commit(conn);}}catch(AppException e ){//事务回滚ConnectionManager.rollback(conn);throw e;}finally{ConnectionManager.closeConnection();}return ret;}}