Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ Two kinds of agents of Spring AOP     - C / C ++ various data types Conversion Summary (Programming)

- Android Studio 1.0.2 set the memory size (Linux)

- RAID disk array Description (Linux)

- iSCSI + multipath achieve DM multipath storage (Server)

- The virtual memory (Linux)

- Java Database Programming JDBC configuration (Programming)

- Docker startups use continuous integration deployment (Server)

- Ubuntu Control Panel to resolve network-manager icon display issue (Linux)

- CentOS 6.5 configuration SSDB 1.8.0 (Server)

- Tecplot Installation under Linux (Linux)

- Tomcat session clustering and server session (Server)

- ORA-01000 Solution (Database)

- Linux mention the right notes (Linux)

- Install and manage Java under mac (Linux)

- Oracle Database Performance Optimization of memory disk (Database)

- Python developer of time-saving method (Programming)

- HTTP Client Hints Introduction (Server)

- C ++ two second pointer memory model (two-dimensional array) (Programming)

- Android Fragment really fully resolve (Programming)

- Learning and Practice (Linux)

  Two kinds of agents of Spring AOP
  Add Date : 2016-06-02      
  Benpian record both of Spring AOP proxy, do the next foreshadowing the next AOP implementation.

1.JDK dynamic proxy agent 2.cglib

1, if the target object implements the interface, by default, will use the JDK dynamic proxy AOP realization
2, if the target object implements the interface, you can force the use of CGLIB achieve AOP
3, if the target object does not implement an interface, you must use CGLIB library, spring automatically between JDK dynamic proxies and CGLIB conversion

Note: JDK dynamic proxy than cglib agent execution speed, but the performance is not as good cglib. Therefore, the choice of which agent to use will depend on the specific circumstances of a single case of the general model with cglib better.

one. JDK dynamic proxy implementation (works by using reflection)

First define the interface and achieve

      public interface TestService {

          public int add ();

      public class TestServiceImpl implements TestService {

        public int add () {
            System.out.println ( "begin add ...");
            return 0;


Define the proxy class, to note here is to import packages import java.lang.reflect. *

  public class JDKDynamicProxy implements InvocationHandler {

    // The target object to be proxied
    private Object proxyObj;
    / **
      * Object newProxyInstance (ClassLoader loader, Class < ?> [] Interfaces, InvocationHandler h)
      * Loader: a class loader ClassLoader objects, which are defined by the ClassLoader object is to load the resulting proxy object
      * Interfaces: an array Interface object represents is that I will give an object I need the agent to provide a set of what the interface, if I provide a set of interfaces to it, then the proxy has stated that implements this interface (polymorphism) so I can call this method a set of interfaces
      * H: a InvocationHandler object representing this is when my dynamic proxy object when calling the method, the object will be associated where a InvocationHandler
    * /
      public Object newProxy (Object proxyObj) {
            this.proxyObj = proxyObj;
            // Returns a proxy object
          return Proxy.newProxyInstance (proxyObj.getClass (). getClassLoader (),
                                      proxyObj.getClass (). getInterfaces (),

      / **
      * Implementation Audience
      * Object proxy: proxied objects
      * Method method: the method to call
      * Object args []: method calls require parameters
      * /
        public Object invoke (Object proxy, Method method, Object [] args)
                                  throws Throwable {
            before ();
            // Method call the target object through the reflection mechanism; Object object = method.invoke (this.proxyObj, args)
            the After ();
            return object;
        public void before () {
              System.out.println ( "Before beginning the audience ...");
        public void after () {
            System.out.println ( "audience started after ...");

Test categories:

      public static void main (String [] args) {
          // We want real object proxy
          TestService testService = new TestServiceImpl ();
          //testJDKProxyService.add();// not by proxy
        JDKDynamicProxy JDKDynamicProxyTarget = new JDKDynamicProxy ();
        TestService testServiceProxy = (TestService) JDKDynamicProxyTarget.newProxy (testService);
        // Execute the proxy class method
        testServiceProxy.add ();


two. CGLIB agent, you need to import cglib-nodep-2.1_3.jar

    Under the first cglib, CGlib is a powerful, high-performance, high-quality Code generation libraries. It can be extended at runtime Java classes and implement Java interfaces.

Define an implementation class (note that does not implement the interface)

  public class TestCGLIBServiceImpl {

    public int add () {
        System.out.println ( "begin add ...");
        return 0;

Cglib proxy class is defined, then the package should be introduced import net.sf.cglib.proxy. *

  import java.lang.reflect.Method;
  import net.sf.cglib.proxy.Enhancer;
  import net.sf.cglib.proxy.MethodInterceptor;
  import net.sf.cglib.proxy.MethodProxy;

    public class CGLIBProxy implements MethodInterceptor {

    private Object targetObject; // audience proxied
    public Object createProxyInstance (Object targetObject) {

          . This targetObject = targetObject;

          Enhancer enhancer = new Enhancer ();

          enhancer.setSuperclass (targetObject.getClass ()); // set the proxy target

          enhancer.setCallback (this); // set callback

          return enhancer.create ();


    / **
    * Process method call and returns the result on a proxy instance
    * @param Object: proxy class
    * @param Method: proxied method
    * @param Args: an array of arguments of the method
    * @param MethodProxy
    * /
    public Object intercept (Object object, Method method, Object [] args,
            MethodProxy methodproxy) throws Throwable {
        Object result = null;
        try {
              . System out .println ( "pre-processing start ...");
              // Method to perform target object; result = methodproxy.invoke (targetObject, args)
              . System out .println ( "post-process started ...");
          } Catch (Exception e) {
              . System out .println ( "Exception Handling ...");
          } Finally {
              . System out .println ( "call to end ...");
          return result;
Test categories:

  public class TestCGLIBProxy {

        public static void main (String [] args) {
          // We want real object proxy
          TestCGLIBServiceImpl testCGLIB = new TestCGLIBServiceImpl ();
          CGLIBProxy CGLIBproxy = new CGLIBProxy ();
          TestCGLIBServiceImpl testCGLIBProxy = (TestCGLIBServiceImpl) CGLIBproxy.createProxyInstance (testCGLIB);
          testCGLIBProxy.add ();

Written on the back: two kinds of agents spring AOP implementation code to write this here just realized, if you want to really understand, which have to be familiar with the principles of mechanisms, such as reflection, newProxyInstance (...), Enhancer () principle, invoke () and so on principle.
- CentOS 6.5 install VNC-Server (Linux)
- iOS Sensor Development - add to APP phone password, fingerprint security authentication (Programming)
- Android in the event delivery and handling mechanism (Programming)
- MySQL configuration file my.cnf increase the log file parameter error (Database)
- Do not find ifconfig eth0 and IP address under CentOS6.5 (Linux)
- Getting Started with Linux: Nginx Web Server How to Block Specific User Agents (UA) (Server)
- The Linux C truncate function clears the file notes (Programming)
- Oracle Database asynchronous IO cause slow query response (Database)
- Shell Programming Regular Expressions (Programming)
- To install Spotify in Ubuntu / Mint (Linux)
- Oracle partition table data migration, process management automation (Database)
- bash login and welcome message: / etc / issue, / etc / motd (Linux)
- Oracle Data Pump Example (Database)
- DELL D630 Wireless LAN Driver Installation CentOS6 (Linux)
- Arronax allows you to easily create desktop startup file (Linux)
- Tune in high resolution to 1280x800 in Fedora 14 (Linux)
- Limit the use of the request being Nginx Flood attack (Linux)
- Ubuntu 14.04 Docker installation (Linux)
- Java implementation linear table - represents the order of representation and chain (Programming)
- 12 Linux Process Management Commands (Linux)
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.