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