Go The dynamic agent mechanism of Java and the implementation method of spring

Source: Internet
Author: User
Tags throwable

JAVA Proxy Implementation

The implementation of proxy is dynamic agent and static proxy, and the implementation of static proxy is to encapsulate the Java class that has been generated.

Dynamic agents are generated at run time related agent tired, in Java to generate a dynamic agent in general there are two ways.

JDK Implementation method

The JDK implements proxy generation, which is implemented using Class Java.lang.reflect.Proxy, as follows

EX:

Public class Jdkproxy {

Public Static Object Getpoxyobject (final Object c) {

return Proxy. newproxyinstance (C.getclass (). getClassLoader (), C.getclass (). Getinterfaces (),//JDK implements dynamic proxy, but the JDK implementation must require an interface

New Invocationhandler () {

Public Object Invoke (Object proxy, Method method, object[] args) throws Throwable {

TODO auto-generated Method stub

Object reobj = null;

System. out. Print ("say:");

Reobj = Method.invoke (c, args);

System. out. println ("[" + Calendar. GetInstance(). Get (Calendar. HOUR) + ":"

+ Calendar. getinstance (). Get (Calendar. MINUTE) + ""

+ Calendar. getinstance (). Get (Calendar. SECOND) + "]");

return reobj;

}

});

}

}

Test proxy class methods

Public class Testforpoxy {

Public Static void Main (string[] args) {

Servicetest service = new Servicetestimpl ();

System. out. println (Service.getclass (). Getsimplename ());

Servicetest Poxyservice = (servicetest) jdkproxy.getpoxyobject (service);

System. out. println (Poxyservice.getclass (). Getsuperclass ());

Poxyservice.saysomething ("Hello,my QQ code is 107966750.");

Poxyservice.saysomething ("What ' s your name?");

Poxyservice.saysomething ("Only for Test,hehe.");

}

}

1, proxy implements the target class of the agent must have the implementation interface

2, the generated proxy class for the interface implementation class, and the target class can not be converted, only to the interface implementation class to call

Notable features: The class generated by this method is called $Proxy 0

Implemented with Cglib package

Cglib is an open source project, the official website is: http://cglib.sourceforge.net/, you can go to download the latest jar package,

This project is using Cglib-3.0.jar.

This project also added a dependency jar package Asm-4.0.jar,asm-util-4.0.jar

Here's how it's implemented

EX:

Public class Cglibproxy {

Public Static Object Getpoxyobject (object c) {

Enhancer enhancer = New enhancer ();

Enhancer.setsuperclass (C.getclass ());

Enhancer.setcallback (new methodinterceptor () {

Public Object Intercept (Object arg0, Method arg1, object[] arg2, Methodproxy proxy) throws Throwable {

System. out. Print ("say:");

Proxy.invokesuper (arg0, arg2);

System. out. println ("[" + Calendar. GetInstance(). Get (Calendar. HOUR) + ":"

+ Calendar. getinstance (). Get (Calendar. MINUTE) + "" + Calendar. getinstance (). Get (Calendar. SECOND)

+ "]");

return null;

}

});

return enhancer.create ();

}

}

Test proxy class methods

Public class Testforpoxy {

Public Static void Main (string[] args) {

Servicetest service = new Servicetestimpl ();

System. out. println (Service.getclass (). Getsimplename ());

Servicetest Poxyservice = (servicetest) jdkproxy.getpoxyobject (service);

Servicetest Poxyservice = (servicetest) cglibproxy. Getpoxyobject (service);

System. out. println (Poxyservice.getclass (). Getsuperclass ());

Poxyservice.saysomething ("Hello,my QQ code is 107966750.");

Poxyservice.saysomething ("What ' s your name?");

Poxyservice.saysomething ("Only for Test,hehe.");

}

}

1, Cglib is implemented by inheriting the target class of the agent

2, generated by the proxy class can be no method, the generated class can be directly converted to the target class or target class implementation of the implementation class interface, as the Java Up conversion

Obvious features: The output shows that the parent class of the generated proxy class is the target class of the proxy

Analysis of the proxy class mechanism of Spring AOP

In spring, the bean is generated by the dynamic proxy, then it is implemented by the proxy class of the JDK, or by the Cglib way.

The dependent jar packages required by AOP spring are:

Spring-asm-3.2.0.m1.jar

Spring-beans-3.2.0.m1.jar

Spring-context-3.2.0.m1.jar

Spring-core-3.2.0.m1.jar

Spring-expression-3.2.0.m1.jar

Spring-aop-3.2.0.m1.jar

Spring-aspects-3.2.0.m1.jar

Commons\commons-logging-1.1.1\commons-logging-1.1.1.jar

Aopalliance\aopalliance.jar

Lib\aspectjweaver.jar

Implementing AOP

Implement AOP in a simple first

Configured as follows

<?xml version="1.0" encoding="Utf-8"?>

<beans xmlns="Http://www.springframework.org/schema/beans"

Xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance " xmlns:aop= "http://www.springframework.org/ SCHEMA/AOP "

xsi:schemalocation="

Http://www.springframework.org/schema/beans

Http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

Http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-3.2.xsd ">

<bean id="test" class="org.ben.spring.service.Test" />

<bean id="Aspectbean" class="Org.ben.spring.TestAspect" />

<!--AOP interception of the test class--

<aop:config>

<aop:aspect id="Testaspect" ref="Aspectbean">

<!--configuring Facets--

<aop:pointcut id="Businessservice"

expression="Execution (* Org.ben.spring.service.Test.say (..))" />

<aop:before pointcut-ref="Businessservice" method="Dobefore" />

<aop:after pointcut-ref="Businessservice" method="Doafter" />

</aop:aspect>

</aop:config>

</beans>

Then run the result as follows, indicating that the AOP interception succeeded

AOP Test class

Public class Testbeans {

Public Static void Main (string[] args) {

ApplicationContext CTX = new classpathxmlapplicationcontext ("Test.xml");

Test test= (Test) Ctx.getbean ("test");

System. out. println (Test.getclass (). Getsimplename ());

Test.say ();

}

}

Output:

Do something in befor

Welcome for Test

Do something

How the Print proxy class is generated

In the first case , test does not implement any interfaces, the code is as follows

Public class Test {

Public void say () {

System. println ("Welcome for Test,my QQ is 107966750");

}

}

Add the name of the current object to the Testbeans

As follows:

ApplicationContext CTX = new classpathxmlapplicationcontext ("Test.xml");

Test test= (Test) Ctx.getbean ("test");

System. out. println (Test.getclass (). Getsimplename ());

Test.say ();

Output:

test$ $EnhancerByCGLIB $$4791b36c

Super class is Class Org.ben.spring.service.Test

Do something in befor

Welcome for Test

Do something

Obviously after using AOP, the output is the proxy class object test$ $EnhancerByCGLIB $ $bb 9b6a7c. And its parent class is our proxy target class. Description is generated by cglib

The second case

XML configuration unchanged, change the proxy target class test implementation method, as follows

Public class Test implements testinter{

Public void say () {

System. println ("Welcome for Test,my QQ is 107966750");

}

}

Different from the original is the more inherited an interface, the interface defines the Say () method

Add the name of the current object to the Testbeans

As follows:

ApplicationContext CTX = new classpathxmlapplicationcontext ("Test.xml");

Testinter test= (Testinter) Ctx.getbean ("test");

System. out. println (Test.getclass (). Getsimplename ());

System. out. println ("Super Class is" +test.getclass (). Getsuperclass ());

Test.say ();

Output:

$Proxy 0

Super class is Class Java.lang.reflect.Proxy

Do something in befor

Welcome for Test,my QQ is 107966750

Do something

Conclusion

In Spring AOP, when an interception object implements an interface, it is generated using the JDK's proxy class. When no interface is implemented, a subclass of the interception class generated by the Gclib Open source project is used.

Ext.: http://www.cnblogs.com/springsource/archive/2012/08/30/2664050.html

Go The dynamic agent mechanism of Java and the implementation method of spring

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.