JDK dynamic proxy is achieved by the JDK java.lang.reflect.Proxy class. The following demonstrates the use JDK dynamic proxy by specific cases. 1 . Create a project Create a name for the springDemo03 in MyEclipse Web project, the Spring JAR dependencies and support package to the WEB Web project -INF / lib directory, and posted to the class path. 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. Package com.mengma.dao; public interface CustomerDao { public void the Add (); // add public void Update (); // modify public void Delete (); // delete public void 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. package com.mengma.dao; public class CustomerDaoImpl implements CustomerDao { @Override public void add() { System.out.println ( "Add customer ..." ); } @Override public void update() { System.out.println ( "modify the client ..." ); } @Override public void delete() { System.out.println ( "Delete client ..." ); } @Override public void find() { System.out.println ( "modify the client ..." ); } } 4 Create cut 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. package com.mengma.jdk; public class MyAspect { public void myBefore() { System.out.println ( "before execution method" ); } public void myAfter() { 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. Package com.mengma.jdk; 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 (); // After enhancements 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 an instance customerDao. The first 16 lines of code to create an instance of the class cut a corresponding method aspect class calls; 18 ~ 26 rows is to use a proxy class created for instance 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. package com.mengma.jdk; import org.junit.Test; import com.mengma.dao.CustomerDao; public class JDKProxyTest { @Test public void Test () { // get the designated content from the factory (corresponding spring obtained, but the agent object of this contents) 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 run () method, after the successful operation, the output test console