Wu Yuxiong - a natural framework for the development of natural JAVA SPRING study notes: Spring CGLlB dynamic proxy

JDK dynamic proxy very simple to use, but it also has some limitations, because the JDK dynamic proxy must implement one or more interfaces, if you do not want to implement an interface, you can use CGLIB agent. 
CGLIB (Code Generation Library) is a high performance package open source code generator, which is used by many AOP framework, which is the bottom layer by using a small, fast bytecode processing frame ASM (Java byte code manipulation frame) conversion words section of code and generate a new class. Thus CGLIB depends on the ASM, extract the core package Spring Spring -core-3.2.2.RELEASE.jar, file directory FIG.

Extract the core package contains cglib and asm, that is to say Spring3.2.13 version of the core package has been integrated package CGLIB need, there is no need to import additional ASM JAR packages in development. The following scenario illustrates achieve through the proxy process CGLIB.
1 . GoodsDao create the target class 
created in the target class GoodsDao com.mengma.dao package, a class defined in the add, delete, change, method, and write the output statement in each method, as follows. 
Package com.mengma.dao;
 public  class GoodsDao {
     public  void the Add () { 
        System.out.println ( "Add product ..." ); 
    } 
    public  void Update () { 
        System.out.println ( "modified product .. . " ); 
    } 
    public  void the delete () { 
        System.out.println ( " delete item ... " ); 
    } 
    public  void the Find () { 
        System.out.println ( " modified product ... " );
    } 
}
 2 . MyBeanFactory creates a proxy class 
to create a package in the src directory named com.mengma.cglib to create class MyBeanFactory this package, as shown below. 
Package com.mengma.cglib;
 Import the java.lang.reflect.Method;
 Import org.springframework.cglib.proxy.Enhancer;
 Import org.springframework.cglib.proxy.MethodInterceptor;
 Import org.springframework.cglib.proxy.MethodProxy;
 Import com.mengma.dao.GoodsDao;
 Import com.mengma.jdk.MyAspect;
 public  class MyBeanFactory {
     public  static goodsDao the getBean () {
         // prepare certain class 
        Final goodsDao goodsDao = new new GoodsDao ();
         // create a class instance cut 
        Final MyAspect myAspect = new new MyAspect ();
         // generated proxy class, CGLIB-in operation, a subclass of the specified object, enhancing 
        Enhancer Enhancer = new new Enhancer ();
         // determine need to enhance class 
        enhancer.setSuperclass (goodsDao.getClass ());
         // add callback 
        enhancer.setCallback ( new new MethodInterceptor () {
             // Intercept corresponds jdk invoke, the first three parameters jdk invoke- actuator 
            @Override
             public Object intercept (Object proxy, Method method, Object [] args,
                    MethodProxy MethodProxy) throws  the Throwable {
                myAspect.myBefore (); // reinforcing front 
                Object obj = Method.invoke (goodsDao, args); // target method performs 
                myAspect.myAfter (); // enhanced to 
                return obj; 
            } 
        }); 
        // creates a proxy class 
        GoodsDao goodsDaoProxy = (GoodsDao) enhancer.create ();
         return goodsDaoProxy; 
    } 
} 
the above code, the application of the core classes Enhancer CGLIB. In the first 19 lines of code calls the class setSuperclass Enhancer () method, the target object is determined. 
The firstLine 21 code calls the setCallback () method adds the callback function; Intercept first 24 lines of code () method is equivalent to the dynamic proxy mode JDK invoke () method, the method will be performed before and after the target method, the method aspect class enhanced; create proxy class to create 33 to 34 lines of code calls the class Enhancer () method, and finally return to the proxy class.
3 . Create a test class 
to create a test packet class CGLIBProxyTest at com.mengma.cglib, edited as shown below. 
Package com.mengma.cglib;
 Import org.junit.Test;
 Import com.mengma.dao.GoodsDao;
 public  class CGLIBProxyTest { 
    @Test 
    public  void Test () {
         // get the specified content from the plants (corresponding to spring obtained, but this object is contents agent) 
        GoodsDao goodsDao = MyBeanFactory.getBean ();
         // perform a method 
        goodsDao.add (); 
        goodsDao.update (); 
        goodsDao.delete (); 
        goodsDao.find (); 
    } 
}
The above code, when calling the getBean () method, obtained is still goodsDao proxy object, and then call the object's methods. Use JUnit test after test () method is successful, as shown in FIG console output

 

Guess you like

Origin www.cnblogs.com/tszr/p/12129921.html