静态代理和动态代理

(1)按照代理的创建时期,代理类可以分为两种: 

         静态:由程序员创建代理类或特定工具自动生成源代码再对其编译。在程序运行前代理类的.class文件就已经存在了。

         动态:在程序运行时运用反射机制动态创建而成。

(2)静态代理:  静态代理需要代理对象和目标对象实现一样的接口。

    举例说明:

    接口类: UserManager

 1 public interface UserManager {
 2 
 3     public void addUser(String username, String password);
 4     
 5     public void delUser(int userId);
 6     
 7     public String findUserById(int userId);
 8     
 9     public void modifyUser(int userId, String username, String password);
10 }
View Code

    目标对象:UserManagerImpl 

 1 public class UserManagerImpl implements UserManager {
 2 
 3     public void addUser(String username, String password) {
 4         System.out.println("---------UserManagerImpl.add()--------");
 5     }
 6 
 7     public void delUser(int userId) {
 8     System.out.println("---------UserManagerImpl.delUser()--------");
 9     }
10 
11     public String findUserById(int userId) {
12     System.out.println("---------UserManagerImpl.findUserById()--------");
13         return "张三";
14     }
15 
16     public void modifyUser(int userId, String username, String password) {
17     System.out.println("---------UserManagerImpl.modifyUser()--------");
18     }
19 }
View Code

    静态代理对象:UserManagerImplProxy 需要实现UserManager 接口!

 1 public class UserManagerImplProxy implements UserManager {
 2     private UserManager userManager;
 3     public UserManagerImplProxy(UserManager userManager) {
 4         this.userManager = userManager;
 5     }
 6     public void addUser(String username, String password) {
 7         checkSecurity();
 8         userManager.addUser(username, password);
 9     }
10     public void delUser(int userId) {
11         checkSecurity();
12         userManager.delUser(userId);
13     }
14     public String findUserById(int userId) {
15         checkSecurity();
16         return userManager.findUserById(userId);
17     }
18     public void modifyUser(int userId, String username, String password) {
19         checkSecurity();
20         userManager.modifyUser(userId, username, password);
21     }
22     private void checkSecurity() {
23         System.out.println("-------checkSecurity-------");
24     }    
25 }
View Code

    测试类:StaticUserProxy

1 public class StaticUserProxy {    
2     @Test
3     public void testStaticProxy(){        
4         UserManager target = new UserManagerImpl();//目标对象
5         UserManagerImplProxy proxy=new UserManagerImplProxy(target);//代理对象
6         proxy.delUser("ndvhue32577349");
7     }
8 }
View Code

 (3)动态代理:在Java中要想实现动态代理机制,需要java.lang.reflect.InvocationHandler接口和 java.lang.reflect.Proxy 类的支持。

    举例说明:

    接口类: UserManager

 1 public interface UserManager {
 2 
 3     public void addUser(String username, String password);
 4     
 5     public void delUser(int userId);
 6     
 7     public String findUserById(int userId);
 8     
 9     public void modifyUser(int userId, String username, String password);
10 }
View Code

        目标对象:UserManagerImpl 

 1 public class UserManagerImpl implements UserManager {
 2 
 3     public void addUser(String username, String password) {
 4         //checkSecurity();
 5         System.out.println("---------UserManagerImpl.add()--------");
 6     }
 7 
 8     public void delUser(int userId) {
 9         //checkSecurity();
10         System.out.println("---------UserManagerImpl.delUser()--------");
11     }
12 
13     public String findUserById(int userId) {
14         //checkSecurity();
15         System.out.println("---------UserManagerImpl.findUserById()--------");
16         return "张三";
17     }
18 
19     public void modifyUser(int userId, String username, String password) {
20         //checkSecurity();
21         System.out.println("---------UserManagerImpl.modifyUser()--------");
22     }
23 
24 }
View Code

        动态创建代理对象的类:SecurityHandler需要实现InvocationHandler 接口!

 1 import java.lang.reflect.InvocationHandler;
 2 import java.lang.reflect.Method;
 3 import java.lang.reflect.Proxy;
 4 
 5 public class SecurityHandler implements InvocationHandler {
 6     
 7     private Object targetObject;
 8     
 9     public Object createProxyInstance(Object targetObject) {
10         this.targetObject = targetObject;
11         return Proxy.newProxyInstance(targetObject.getClass().getClassLoader(), 
12                                targetObject.getClass().getInterfaces(), 
13                                this);
14     }
15     
16     public Object invoke(Object proxy, Method method, Object[] args)
17             throws Throwable {
18         checkSecurity();
19         
20         //调用目标方法
21         Object ret = method.invoke(targetObject, args);
22         
23         return ret;
24     }
25     
26     private void checkSecurity() {
27         System.out.println("-------checkSecurity-------");
28     }        
29 }
View Code

  客户端代码:Client

1 public class Client {
2 
3     public static void main(String[] args) {
4         SecurityHandler hander = new SecurityHandler();
5         UserManager useraManager = (UserManager)hander.createProxyInstance(new UserManagerImpl());
6         useraManager.addUser("张三", "123");
7     }
8 
9 }
View Code

 

 

 

posted on 2019-08-22 21:10  风中的四月  阅读(158)  评论(0编辑  收藏  举报