Proxy mode, proxy mode java

Source: Internet
Author: User

Proxy mode, proxy mode java

1. Static proxy

1.1 A common interface must be maintained for both the agent class of the static proxy and the class to be proxy.

public interface IUserDao {    void save();}

1.2 proxy class, target object

Public class UserDao implements IUserDao {@ Override public void save () {System. out. println ("----- the data has been saved !!! ------");}}

1.3 proxy object

Public class UserDaoProxy implements IUserDao {// receives and saves the target object private IUserDao target; public UserDaoProxy (IUserDao target) {this.tar get = target;} @ Override public void save () {System. out. println ("START transaction... "); target. save (); // method of executing the target object System. out. println ("Commit transaction... ");}}

1.4 test class

Public class App {public static void main (String [] args) {// target object IUserDao target = new UserDao (); // proxy IUserDao proxy = new UserDaoProxy (target ); proxy. save (); // The proxy method is executed }}

 

2. Dynamic proxy

2.1 similarly, a dynamic proxy also needs to complete an interface. (Same as above)

2.2 The target object is the same as the target object.

2.3 is different only on proxy objects

Public class ProxyFactory {// maintain a target Object private Object target; public ProxyFactory (Object target) {this.tar get = target;} // to the target Object, generate the Proxy Object public Object getProxyInstance () {return Proxy. newProxyInstance (target. getClass (). getClassLoader (), target. getClass (). getInterfaces (), new InvocationHandler () {@ Override public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {System. out. println ("enable transaction"); // method of executing the target Object returnValue = method. invoke (target, args); System. out. println ("Commit transaction"); return returnValue ;}});}}

2.4 Test class

Public class App {public static void main (String [] args) {// target object IUserDao target = new UserDao (); System. out. println (target. getClass (); // create the proxy object IUserDao proxy = (IUserDao) new ProxyFactory (target) for the target object ). getProxyInstance (); System. out. println (proxy. getClass (); // execution method [proxy object] proxy. save ();}}

 

3. cglib proxy

3.1cglib the proxy does not need to complete the interface. You only need to write the classes and the proxy classes to be proxies. In this case, the proxy classes are the same as 1.2, so no code is written.

3.2 The agent classes are different. To use the cglib agent mode, you must reference the core framework package of spring.

Public class ProxyFactory implements MethodInterceptor {// maintain the target Object private Object target; public ProxyFactory (Object target) {this.tar get = target ;} // create a proxy Object public Object getProxyInstance () {// 1. tool class Enhancer en = new Enhancer (); // 2. set the parent class en. setSuperclass (target. getClass (); // 3. set the callback function en. setCallback (this); // 4. create a subclass (proxy object) return en. create () ;}@ Override public Object intercept (Object obj, Method method, Object [] args, MethodProxy proxy) throws Throwable {System. out. println ("START transaction ..... "); // method for executing the target Object returnValue = method. invoke (target, args); System. out. println ("commit a transaction ..... "); return returnValue ;}}

3.3 test class

Public class App {public static void main (String [] args) {// target object UserDao target = new UserDao (); System. out. println (target. getClass (); // proxy object UserDao proxy = (UserDao) new ProxyFactory (target ). getProxyInstance (); System. out. println (proxy. getClass (); // execute proxy object method proxy. save ();}}

 

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.