main test class: (Bulkbean, fastclass use test code not saved)
Import Net.sf.cglib.proxy.Callback;
Import Net.sf.cglib.proxy.MethodInterceptor;
Import Net.sf.cglib.proxy.MethodProxy;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
Import Java.lang.reflect.Field;
Import Java.lang.reflect.Method;
Import java.util.ArrayList;
Import java.util.Collections;
Import java.util.List;
/** * Created by the Administrator on 2014/11/25. */public class Main {public static void main (string[] args) throws Exception {Classpathxmlapplicationcontex
T ctx = new Classpathxmlapplicationcontext (new string[]{"Service.xml"});
Beana obj = (beana) ctx.getbean ("Beana");
System.out.println (obj);
System.out.println (Obj.getname ()); public static class Mycallback implements Methodinterceptor, Callback {@Override public Object int Ercept (Object O, method method, object[] objects, Methodproxy methodproxy) throws Throwable {System.out.print ln ("Invoke Method:" +Method.getname ());
Return Methodproxy.invokesuper (O, objects);
} public static method[] Getgetmethods (Class clazz) throws Exception {field[] arr = GetFields (clazz);
Method[] methods = new Method[arr.length];
for (int i = 0; i < arr.length ++i) {String methodname = getmethodname (' Get ', arr[i].getname ());
Method method = Clazz.getmethod (methodname, New class[]{});
Methods[i] = method;
return methods;
public static string[] Getgetmethodnames (Class clazz) throws Exception {field[] arr = GetFields (clazz);
String[] methods = new String[arr.length];
for (int i = 0; i < arr.length ++i) {String methodname = getmethodname (' Get ', arr[i].getname ());
Methods[i] = methodname;
return methods; public static method[] Getsetmethods (Class clazz) throws Exception {field[] arr = GetFields (CLAZZ);
Method[] methods = new Method[arr.length];
for (int i = 0; i < arr.length ++i) {String methodname = Getmethodname ("Set", Arr[i].getname ());
Method method = Clazz.getmethod (methodname, New Class[]{arr[i].getclass ());
Methods[i] = method;
return methods;
public static string[] Getsetmethodnames (Class clazz) throws Exception {field[] arr = GetFields (clazz);
String[] methods = new String[arr.length];
for (int i = 0; i < arr.length ++i) {String methodname = Getmethodname ("Set", Arr[i].getname ());
Methods[i] = methodname;
return methods;
public static string Getmethodname (string prefix, string varName) {StringBuilder sb = new StringBuilder ();
Sb.append (prefix);
Sb.append (VarName);
Sb.setcharat (Prefix.length (), Character.touppercase (Varname.charat (0)));
return sb.tostring (); }
public static class[] Getfieldtypes (Class clazz) {field[] arr = GetFields (clazz);
Class[] result = new Class[arr.length];
for (int i = 0; i < arr.length ++i) {Result[i] = Arr[i].gettype ();
return result;
public static field[] GetFields (Class clazz) {list<field> fields = new arraylist<field> (10);
while (!clazz.equals (Object.class)) {field[] arr = Clazz.getdeclaredfields ();
Collections.addall (fields, arr);
Clazz = Clazz.getsuperclass ();
} field[] result = new field[fields.size ()];
for (int i = 0; i < fields.size (); ++i) {Result[i] = Fields.get (i);
return result;
}
}
beanfactory after Processor:
Package cglib;
Import Net.sf.cglib.proxy.Enhancer;
Import org.springframework.beans.BeansException;
Import org.springframework.beans.factory.config.BeanDefinition;
Import Org.springframework.beans.factory.config.BeanFactoryPostProcessor;
Import Org.springframework.beans.factory.config.BeanPostProcessor;
Import Org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
Import Net.sf.cglib.proxy.Callback;
Import Java.lang.reflect.Method;
/** * Created by the Administrator on 2014/11/25. */public class Mybeanfactorypostprocessor implements Beanfactorypostprocessor {@Override public void postprocess Beanfactory (Configurablelistablebeanfactory configurablelistablebeanfactory) throws Beansexception {String[]arr=c
Onfigurablelistablebeanfactory.getbeandefinitionnames (); for (String s:arr) {beandefinition definition =configurablelistablebeanfactory.getbeandefin
Ition (s); String Name=definition.getbeanclassname ();
try {Class clazz=class.forname (name);
Enhancer enhancer=new Enhancer ();
Enhancer.setsuperclass (Clazz);
Enhancer.setcallbacktype (Mycallback.class);
Definition.setbeanclassname (Enhancer.createclass (). GetName ());
catch (ClassNotFoundException e) {e.printstacktrace ();
} configurablelistablebeanfactory.addbeanpostprocessor (New Beanpostprocessor () {@Override public Object Postprocessbeforeinitialization (Object o, String s) throws beansexception {retur
n o; @Override public Object Postprocessafterinitialization (Object o, String s) throws Beansexceptio
n {Class clazz=o.getclass ();
try {method M=clazz.getdeclaredmethod ("Setcallbacks", New Class[]{callback[].class}); M.invoke (O,new Object[]{neW callback[]{new mycallback ()});
catch (Exception e) {e.printstacktrace ();
return o;
}
});
}
}
method AOP Logic:
Package cglib;
Import Net.sf.cglib.proxy.Callback;
Import Net.sf.cglib.proxy.MethodInterceptor;
Import Net.sf.cglib.proxy.MethodProxy;
Import Java.lang.reflect.Method;
/**
* Created by the Administrator on 2014/11/25
. *
/public class Mycallback implements Methodinterceptor,callback {@Override the public
Object Intercept ( Object O, method method, object[] objects, Methodproxy methodproxy) throws Throwable {
System.out.println ("Invoke Me Thod: "+method.getname ());
Return Methodproxy.invokesuper (o,objects);
}
Beana class file:
/**
* Created by the Administrator on 2014/11/25
. *
/public class Beana {
private String name;
Private Long ID;
Public String GetName () {return
name;
}
public void SetName (String name) {
this.name = name;
}
Public Long GetId () {return
ID;
}
public void SetId (Long id) {
this.id = ID;
}
}
Spring Bean configuration file:
<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns=
"Http://www.springframework.org/schema/beans"
xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation= "http:// Www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd "
Default-autowire= "ByName" >
<bean id= "tt" Name= "Beana" class= "Beana" >
<property name= "name" Value= "MyName" "/>
</bean> <bean id=" mybeanfactorypostprocessor "class=" Cglib
. Mybeanfactorypostprocessor "></bean>
</beans>
Pom File:
<?xml version= "1.0" encoding= "UTF-8"?> <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "HTT" P://www.w3.org/2001/xmlschema-instance "xsi:schemalocation=" http://maven.apache.org/POM/4.0.0 Http://maven.apach E.org/xsd/maven-4.0.0.xsd "> <modelVersion>4.0.0</modelVersion> <groupid>cglib-zxl</group id> <artifactId>study</artifactId> <version>1.0-SNAPSHOT</version> <dependencie s> <dependency> <groupId>cglib</groupId> <artifactid>cglib-node p</artifactid> <version>2.1_3</version> </dependency> <DEPENDENCY&G
T
<groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.0.3.RELEASE</version> </dependency> <dependency> <g Roupid>oRg.springframework</groupid> <artifactId>spring-beans</artifactId> <VERSION&G t;4.0.3.release</version> </dependency> </dependencies> </project>