Spring JDK dynamic proxies

1. Create a project to create a name for the springDemo03 in MyEclipse Web project, the Spring support and dependency JAR package to the WEB-INF / lib directory of the Web project, and published to the classpath.

2. Create an interface CustomerDao

Created in the src directory of the project a package called com.mengma.dao to create a CustomerDao interfaces in this package, as shown in the following edited.

 
  
com.mengma.dao Package Penalty for; 

public  interface CustomerDao {
     public  void the Add (); // add 

    public  void Update (); // modify 

    public  void the Delete (); // delete 

    public  void the Find (); // query 
}

 

3. Create a class that implements CustomerDaoImpl

Com.mengma.dao package created under CustomerDaoImpl CustomerDao interface implementation class, and implements all the methods that interface, as shown below.

com.mengma.dao Package; 

public  class CustomerDAOImpl the implements CustomerDao { 

    @Override 
    public  void the Add () { 
        the System. OUT .println ( " Add Customer ... " ); 
    } 

    @Override 
    public  void Update () { 
        . the System OUT .println ( " modify the client ... " ); 
    } 

    @Override 
    public  void the delete () { 
        System. OUT .println ( " delete customers ... " ); 
    } 

    @Override
    public  void Find () { 
        . the System OUT .println ( " Modify Customer ... " ); 
    } 
}

 

4. Create a section class MyAspect

In src directory, create a package called com.mengma.jdk to create a cut in the packet class MyAspect, edited as shown in FIG.

 
  
com.mengma.jdk Package; 

public  class MyAspect {
     public  void myBefore () { 
        the System. OUT .println ( " before execution method " ); 
    } 

    public  void myAfter () { 
        the System. OUT .println ( " After the implementation of the method " ); 
    } 
}

 

The above code, the cut surface is defined in two enhancement methods, respectively myBefore () method and myAfter () method, for the target class (CustomerDAOImpl) enhanced.

5. Create a proxy class MyBeanFactory

In creating a package called MyBeanFactory com.mengma.jdk class java.lang.reflect.Proxy implemented using dynamic agent JDK in the class, as shown below.

com.mengma.jdk Package; 

Import java.lang.reflect.InvocationHandler; 
Import the java.lang.reflect.Method; 
Import the java.lang.reflect.Proxy; 

Import com.mengma.dao.CustomerDao; 
Import com.mengma.dao. CustomerDAOImpl; 

public  class MyBeanFactory { 

    public  static CustomerDao the getBean () {
         // prepare certain class 
        Final CustomerDao customerDao = new new CustomerDAOImpl ();
         // create a class instance cut 
        Final MyAspect myAspect = new new MyAspect ();
         // use a proxy class, enhanced 
        return (CustomerDao) Proxy.newProxyInstance ( 
                MyBeanFactory. class.getClassLoader(),
                new Class[] { CustomerDao.class }, new InvocationHandler() {
                    public Object invoke(Object proxy, Method method,
                            Object[] args) throws Throwable {
                        myAspect.myBefore(); // 前增强
                        Object obj = method.invoke(customerDao, args);
                        myAspect.myAfter(); // 后增强
                        return obj;
                    }
                });
    }
}

 

The above code, a static definition of the getBean () method, simulated here thought IoC Spring framework, creating instance by calling the getBean () method, the first 14 lines of code to create customerDao instance.

Line 16 class code section for creating an instance of the class corresponding method call section; lines 18 through 26 is the use of proxy class instance created customerDao method for enhanced code, the second Proxy 'newProxyInstance where () method a parameter is the current class loader type, the second parameter is the implementation class creates an instance of an interface, the third parameter is the need for enhancement method.

In the method performed before and after the target class, each class execution section myBefore () method and myAfter () method.

6. Create a test class JDKProxyTest

Create a test class named JDKProxyTest at com.mengma.jdk package, as follows.

 
  
com.mengma.jdk Package; 

Import org.junit.Test; 
Import com.mengma.dao.CustomerDao; 

public  class JDKProxyTest { 
    @Test 
    public  void Test () {
         // get the specified content from the plants (corresponding to spring obtained, but when this content proxy objects) 
        customerDao customerDao = MyBeanFactory.getBean ();
         // perform a method 
        customerDao.add (); 
        customerDao.update (); 
        customerDao.delete (); 
        customerDao.find (); 
    } 
}

 

In the above code, when calling the getBean () method, obtained is CustomerDao proxy object class, then call a method in the object.

7. Run the project and view the results

Use JUnit test after test () method is successful, the output of the console shown in Fig.

The results can be seen from Figure 1, the output before and after the target class method calls, successful calls enhanced code, whereby the description, the JDK dynamic proxy has been achieved.

 

 


Figure 1 Operating result

Guess you like

Origin www.cnblogs.com/lowerma/p/11752346.html