Cglib Dynamic Agent

Source: Internet
Author: User

The dynamic agent of JDK is slow, can use the agent of Cglib, the speed is relatively fast:

Package cn.demo02;

import Java.lang.reflect.Method;

Import Java.util.zip.CheckedInputStream;

import org.springframework.beans.BeansException;

import org.springframework.beans.factory.config.BeanPostProcessor;

import org.springframework.cglib.proxy.Enhancer;

import org.springframework.cglib.proxy.MethodInterceptor;

import org.springframework.cglib.proxy.MethodProxy;

Public class Postbean implements beanpostprocessor {

@Override

Public Object Postprocessbeforeinitialization (Object bean, String beanname) throws beansexception {

return Bean;

}

@Override

Public Object Postprocessafterinitialization (Object bean, String beanname) throws beansexception {

if (Bean instanceof Child) {

CG(Code generation)Lib dynamic agent for Beans

Object Proxybean = enhancer. Create (Bean.getclass (), new methodinterceptor () {

@Override

Public Object Intercept (Object Proxybean, method, object[] args, Methodproxy methodproxy)

throws Throwable {

if (Method.getname (). Equals ("Say")) {

System. err. println ("Say is beging ...");

Object obj = Methodproxy.invokesuper (Proxybean, args);

System. err. println ("Say Is over ...");

return obj;

} Else {

Direct release

return method.invoke (bean, args); }

}

});

return Proxybean;

}

return Bean;

}

}

Cglib Dynamic Agent

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.