动态代理
- 1.介绍
- 2.案例
1.介绍
public interface Star {String sing(String name);void dance();
}public class BigStar implements Star{private String name;public BigStar(String name) {this.name = name;}public String sing(String name) {System.out.println(this.name + "正在唱:" + name);return "谢谢!谢谢!";}public void dance() {System.out.println(this.name + "正在跳舞");}
}
// 类似于中介公司
public class ProxyUtil {public static Star createProxy(BigStar bigStar) {//参数1:CLassLoader 用于指定一个类加载器//参数2:interfaces 指定生成的代理长什么样,也就是有什么方法//参数3:InvocationHandler 指定生成的代理对象要干什么事情Star starProxy = (Star) Proxy.newProxyInstance(ProxyUtil.class.getClassLoader(), new Class[]{Star.class}, new InvocationHandler() {@Overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable {// 代理对象要做的事情,会在这里写代码if (method.getName().equals("sing")) {System.out.println("代理准备话筒,收钱20万");} else if (method.getName().equals("dance")) {System.out.println("代理准备场地,收钱20万");}return method.invoke(bigStar, args);}});return starProxy;}
}
// 测试类
public class Test {public static void main(String[] args) {BigStar s = new BigStar("杨超越");Star starProxy = ProxyUtil.createProxy(s);String rs = starProxy.sing("好日子");System.out.println("rs = " + rs);starProxy.dance();}
}
2.案例
案例:使用代理优化用户管理类
场景:某系统有一个用户管理类,包含登录,删除用户,查询用户等功能,系统要求统计每个功能的执行耗时情况,以便后期观察程序性能
需求:现在已经开发好了该模块,找出目前存在的问题,并对其进行改造
/*** 用户业务接口*/
public interface UserService {// 登录功能void login(String loginName,String passWord) throws Exception;// 删除用户void deleteUser() throws Exception;// 查询用户,返回数组的形式String[] selectUsers() throws Exception;
}public class UserServiceImpl implements UserService{@Overridepublic void login(String loginName, String passWord) throws Exception {long startTime = System.currentTimeMillis();if ("admin".equals(loginName) && "123456".equals(passWord)) {System.out.println("登录成功");} else {System.out.println("登录失败");}Thread.sleep(1000);long endTime = System.currentTimeMillis();System.out.println("login方法执行耗时:" + (endTime - startTime)/1000.0 + "s");}@Overridepublic void deleteUser() throws Exception{long startTime = System.currentTimeMillis();System.out.println("成功删除1万个用户");Thread.sleep(1500);long endTime = System.currentTimeMillis();System.out.println("deleteUser方法执行耗时:" + (endTime - startTime)/1000.0 + "s");}@Overridepublic String[] selectUsers() throws Exception{long startTime = System.currentTimeMillis();System.out.println("查询出3个用户");String[] names = {"张三", "李四", "王五"};long endTime = System.currentTimeMillis();System.out.println("deleteUser方法执行耗时:" + (endTime - startTime)/1000.0 + "s");return names;}
}
存在的问题:主要业务逻辑不应该包含时间相关的代码
解决办法:将时间相关的代码通过代理模式实现,把业务类的相关代码删除
public class UserServiceProxy {public static UserService createProxy(UserService userService) {UserService userServiceProxy = (UserService) Proxy.newProxyInstance(UserServiceProxy.class.getClassLoader(),new Class[]{UserService.class}, new InvocationHandler() {@Overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable {if (method.getName().equals("login") || method.getName().equals("deleteUser") ||method.getName().equals("selectUsers")) {long startTime = System.currentTimeMillis();Object rs = method.invoke(userService, args);long endTime = System.currentTimeMillis();System.out.println(method.getName() + "方法执行耗时:" + (endTime - startTime)/1000.0 + "s");return rs;} else {Object rs = method.invoke(userService, args);return rs;}}});return userServiceProxy;}
}