Spring notes (3) -- proxy mode, spring notes -- proxy

Source: Internet
Author: User

Spring notes (3) -- proxy mode, spring notes -- proxy

Static Proxy: Custom proxy class;

Dynamic Proxy: The program is generated at runtime.

I. Static Proxy:

Interface: UserManager: adds, deletes, modifies, and queries a user.

1 package com.dwr.spring.proxy;2 3 public interface UserManager {4     public void addUser(String username,String password);5     public void deleteUser(int userid);6     public void modifyUser(int userid,String username,String password);7     public void findUser(int userid);8 }

Implementation class: UserManagerImpl:

 1 package com.dwr.spring.proxy; 2  3 public class UserManagerImpl implements UserManager { 4     @Override 5     public void addUser(String username, String password) { 6         System.out.println("--------UserManagerImpl.addUser()----------"); 7     } 8  9     @Override10     public void deleteUser(int userid) {11         System.out.println("--------UserManagerImpl.deleteUser()----------");12     }13 14     @Override15     public void modifyUser(int userid, String username, String password) {16         System.out.println("--------UserManagerImpl.modifyUser()----------");17     }18 19     @Override20     public void findUser(int userid) {21         System.out.println("--------UserManagerImpl.findUser()----------");22     }

Client class: Call method;

1 package com.dwr.spring.proxy;2 3 public class Client {4 5     public static void main(String[] args){6         UserManager userManager = new UserManagerImpl();7         userManager.addUser("Jack","123456");8     }9 }

Result:

1 -------- UserManagerImpl. addUser ()----------

The result is displayed normally.

Add security check in UserManagerImpl:

1 public void checkSecurity () {2 System. out. println ("-------- UserManagerImpl. checkSecurity () ----------"); 3}

Each method in UserManagerImpl must call this method to improve coupling. Use a proxy to implement this function:

In the Proxy: the reference of the object to be controlled must be available;

Put the Security Detection Method Into the proxy class. Security Detection is performed before each method is called. The structure of the original class is not damaged and the requirement is also fulfilled.

UserManagerImplProxy class:

 1 package com.dwr.spring.proxy; 2  3 public class UserManagerImplProxy implements UserManager { 4     private UserManager userManager; 5  6     public UserManagerImplProxy(UserManager userManager){ 7         this.userManager = userManager; 8     } 9 10     public void checkSecurity(){11         System.out.println("--------UserManagerImpl.checkSecurity()----------");12     }13 14     @Override15     public void addUser(String username, String password) {16         checkSecurity();17         this.userManager.addUser(username,password);18     }19 20     @Override21     public void deleteUser(int userid) {22         checkSecurity();23         this.userManager.deleteUser(userid);24     }25 26     @Override27     public void modifyUser(int userid, String username, String password) {28         checkSecurity();29         this.userManager.modifyUser(userid,username,password);30     }31 32     @Override33     public void findUser(int userid) {34         checkSecurity();35         this.userManager.findUser(userid);36     }37 }

Client test:

1 package com. dwr. spring. proxy; 2 3 public class Client {4 5 public static void main (String [] args) {6 // use static proxy mode to implement 7 UserManager userManager = new UserManagerImplProxy (new UserManagerImpl (); 8 userManager. addUser ("Jack", "123456"); 9} 10}

The static proxy mode is self-written. If there are many methods in the interface, it will be very troublesome. In this case, we need to use AOP for processing !!!

Ii. Dynamic Proxy:

In dynamic proxy mode, UserManagerImplProxy is not required.

You must declare a class to implement the InvocationHandler interface and define a newProxy method to return a dynamic proxy.

1 package com. dwr. spring. proxy; 2 3 import java. lang. reflect. invocationHandler; 4 import java. lang. reflect. method; 5 import java. lang. reflect. proxy; 6 7 public class SecurityHandler implements InvocationHandler {8 private Object targetObject; 9 10 public Object newProxy (Object targetObject) {11 this.tar getObject = targetObject; 12 // return dynamic Proxy 13 return Proxy. newProxyInstance (targetObject. getClass (). getClassLoader (), targetObject. getClass (). getInterfaces (), this); 14} 15 16 17 public void checkSecurity () {18 System. out. println ("-------- UserManagerImpl. checkSecurity () ---------- "); 19} 20 21 @ Override22 public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {23 checkSecurity (); 24 Object ret = null; 25 26 try {27 // call the real method of the target Object and save the returned value in ret 28 ret = method.invoke(this.tar getObject, args ); 29} catch (Exception e) {30 e. printStackTrace (); 31} 32 return ret; 33} 34}

Client test:

1 SecurityHandler securityHandler = new SecurityHandler (); 2 UserManager userManager = (UserManager) securityHandler. newProxy (new UserManagerImpl (); // create proxy object 3 userManager. addUser ("Tom", "123456 ");

Result:

1 -------- UserManagerImpl. checkSecurity () ---------- 2 -------- UserManagerImpl. addUser ()----------

Use the dynamic generation ideal for Security Detection to carry out security detection; do not want to detect will not detect, it is so capricious...

Client:

1 package com. dwr. spring. proxy; 2 3 public class Client {4 5 public static void main (String [] args) {6 SecurityHandler securityHandler = new SecurityHandler (); 7 UserManager userManager = (UserManager) securityHandler. newProxy (new UserManagerImpl (); // Security Detection 8 userManager. addUser ("Tom", "123456"); 9 System. out. println ("++ ++ "); 10 UserManager userManager1 = new UserManagerImpl (); // No security detection is performed. 11 userManager1.addUser ("Lily", "123456"); 12} 13}

Result:

-------- UserManagerImpl. checkSecurity ()----------
-------- UserManagerImpl. addUser ()----------
++ ++
-------- UserManagerImpl. addUser ()----------

Implemented Using CGLib:

Create a class to implement the MethodInterceptor interface and override the intercept method: This method is called when the method of the contemporary object is called !!

CGlibProxyFactory class:

1 package com. dwr. spring. proxy; 2 3 import org. springframework. cglib. proxy. enhancer; 4 import org. springframework. cglib. proxy. methodInterceptor; 5 import org. springframework. cglib. proxy. methodProxy; 6 7 import java. lang. reflect. method; 8 9 public class CGlibProxyFactory implements MethodInterceptor {10 11 private Object targetObject; 12 13 public Object newProxy (Object targetObject) {14 this.tar getObject = targetObject; 15 Enhancer enhancer = new Enhancer (); 16 enhancer.setSuperclass(this.tar getObject. getClass (); 17 enhancer. setCallback (this); 18 // return proxy Object 19 return enhancer. create (); 20} 21 22 public void checkSecurity () {23 System. out. println ("-------- UserManagerImpl. checkSecurity ()----------"); 24} 25 26/** 27 * @ param proxy the object itself is 28 * @ param method blocked method 29 * @ param objects parameter 30 * @ param methodProxy method 31 */32 @ Override33 public Object intercept (Object proxy, method method, Object [] objects, MethodProxy methodProxy) throws Throwable {34 checkSecurity (); 35 Object ret = null; 36 37 try {38 ret = method.invoke(this.tar getObject, objects ); 39} catch (Exception e) {40 e. printStackTrace (); 41} 42 return ret; 43} 44}

Test:

 1 package com.dwr.spring.proxy; 2  3 public class CGlibProxyTest { 4  5     public static void main(String[] args){ 6         CGlibProxyFactory factory = new CGlibProxyFactory(); 7         UserManagerImpl userManager = (UserManagerImpl) factory.newProxy(new UserManagerImpl()); 8         userManager.addUser("Tom","123456"); 9     }10 }

Result:

-------- UserManagerImpl. checkSecurity ()----------
-------- UserManagerImpl. addUser ()----------

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.