AOP-proxy technology and aop proxy

Source: Internet
Author: User

AOP-proxy technology and aop proxy

I. How to understand proxy technology

Proxy: You don't have to do it. Someone else will handle it for you. Such as Windows shortcuts and housing agencies

It serves as an intermediary. Through proxy objects, you can remove content and services that cannot be seen by the customer or add additional services required by the customer.

Ii. Proxy Mode

Proxy mode uses a proxy object to complete user requests, Shielding users from accessing real objects. In the real world, the agent is authorized to execute some matters of the client. The client does not need to come forward. From the perspective of a third party, it seems that the client does not exist because he only communicates with the agent.

In fact, the agent must have the authorization of the client and request the client for instructions on core issues.

In software design, there are many intentions to use the proxy mode. For example, for security reasons, you need to shield the client from directly accessing the real object, or technical details (such as RMI) that require the proxy class to process remote method calls in remote calls ),

It is also possible to encapsulate real objects to improve system performance, so as to achieve delayed loading.

There are four types of proxy mode roles:

1. Topic interface: defines the public external methods of proxy classes and real themes, and is also the method of proxy class proxy real themes;

2. Real theme: classes that truly implement business logic;

3. Proxy: Used to proxy and encapsulate real themes;

4. Main: the client, which uses the proxy class and topic interface to complete some work.

Iii. Specific Use

1. Static proxy

/*** Created by solverpeng on 2016/6/29. */public interface Factory {void say (); void produce ();}Factory/*** NikeFatory ** @ author solverpeng * @ create 2016-06-29-*/public class NikeFactory implements Factory {@ Override public void say () {System. out. println ("I'm starting production! ") ;}@ Override public void produce () {System. out. println (" creating Nike clothes! ");}}NikeFactory/*** NikeFactoryProxy ** @ author solverpeng * @ create 2016-06-29-*/public class NikeFactoryProxy implements Factory {private NikeFactory nikeFactory; public NikeFactoryProxy () {this. nikeFactory = new NikeFactory () ;}@ Override public void say () {before (); nikeFactory. say (); after () ;}@ Override public void produce () {before (); nikeFactory. produce (); after ();} private void Fter () {System. out. println ("You did a good job! ");} Private void before () {System. out. println (" before the official start of production, I would like to say a few words! ");}}NikeFactoryProxy @ Test public void testStaticProxy () {NikeFactoryProxy nikeFactoryProxy = new NikeFactoryProxy (); nikeFactoryProxy. say (); nikeFactoryProxy. produce ();}TestStaticProxy

2. JDK dynamic proxy

@ Test public void testJDKProxy () {Factory proxyInstance = (Factory) Proxy. newProxyInstance (NikeFactory. class. getClassLoader (), NikeFactory. class. getInterfaces (), new InvocationHandler () {@ Override public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {return method. invoke (NikeFactory. class. newInstance (), args) ;}}); proxyInstance. say (); proxyInstance. produce ();}TestJDKProxy/*** common proxy production factory ** @ author solverpeng * @ create 2016-06-29-*/public class DynamicProxy implements InvocationHandler {private Object target; public DynamicProxy (Object target) {this.tar get = target;} @ SuppressWarnings ("unchecked") public <T> T getProxy () {return (T) Proxy. newProxyInstance (target. getClass (). getClassLoader (), target. getClass (). getInterfaces (), this) ;}@ Override public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {return method. invoke (target, args );}}DynamicProxy @ Test public void testJDKProxy2 () {DynamicProxy dynamicProxy = new DynamicProxy (new NikeFactory (); Factory proxy = dynamicProxy. getProxy (); proxy. say (); proxy. produce ();}TestJDKProxy2

3. CGLIB dynamic proxy (additional cglib package needs to be imported)

/*** CglibProxy *** @ author solverpeng * @ create 2016-06-29-*/public class CglibProxy implements MethodInterceptor {private static CglibProxy instance = new CglibProxy (); public CglibProxy () {} public static CglibProxy getInstance () {return instance;} @ SuppressWarnings ("unchecked") public <T> T getProxy (Class <T> cls) {return (T) Enhancer. create (cls, this) ;}@ Override public Object intercept (Object o, Method method, Object [] objects, MethodProxy methodProxy) throws Throwable {return methodProxy. invokeSuper (o, objects );}}CglibProxy @ Test public void testCglibProxy2 () {NikeFactory proxy = CglibProxy. getInstance (). getProxy (NikeFactory. class); proxy. say (); proxy. produce ();}TestCglibProxy2

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.