public interface MethodInterceptor
extends Callback
{
public Object intercept(Object obj, java.lang.reflect.Method method, Object[] args,MethodProxy proxy) throws Throwable;
}
public interface InvocationHandler {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable;
}
从参数构成上,methodInterceptor的输入参数比Invocationhandler多1个,其实前3个参数对象的含义与Invocationhandler的含义是相同的
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 MyMethodInterceptor implements MethodInterceptor {
// 接口1
static interface Inter1{
public void fun1();
}
// 接口2
static interface Inter2{
public String fun2(String arg0);
}
// 内部方法
public String myFun1(String arg0){
return "hello," + arg0 ;
}
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
String methodName = method.getName();
if( "fun1" .equals(methodName) ){
System. out .println( "[intercept] fun1 invoked" );
return null;
} else if ( "fun2" .equals(methodName) ){
System. out .println( "[intercept] fun2 invoked before" );
String result = (String)args[0] + "..." ;
System. out .println( result );
System. out .println( "[intercept] fun2 invoked after" );
return result;
} else if ( "myFun1" .equals(methodName) ){
System. out .println( "[intercept] myFun1 invoked before" );
Object result = proxy. invokeSuper(obj, args);
System. out .println( result );
System. out .println( "[intercept] myFun1 invoked after" );
return result;
}
return null;
}
public Object createProxy(){
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(MyMethodInterceptor. class );
enhancer.setInterfaces( new Class[]{Inter1. class,Inter2. class});
enhancer.setCallback( this );
return enhancer.create();
}
public static void main(String[] args) {
MyMethodInterceptor ss = new MyMethodInterceptor();
Object proxy = ss.createProxy();
// 接口
Inter1 inter1 = (Inter1)proxy;
inter1.fun1();
Inter2 inter2 = (Inter2)proxy;
inter2.fun2( "code generate library" );
// 类
MyMethodInterceptor c1 = (MyMethodInterceptor)proxy;
c1.myFun1( "cglib" );
}
}
public Object invoke (Object obj, Object[] args) throws Throwable
public Object invokeSuper(Object obj, Object[] args) throws Throwable
import java.lang.reflect.Method;
import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.CallbackFilter;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
public class CallbackTest {
public void fun1(){
System. out .println( "fun1 invoekd" );
}
public void fun2(){
System. out .println( "fun2 invoekd" );
}
static class ClassA implements MethodInterceptor{
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
System. out .println( "ClassA intercept invoked..." );
return proxy.invokeSuper(obj, args);
}
}
static class ClassB implements MethodInterceptor{
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
System. out .println( "ClassB intercept invoked..." );
return proxy.invokeSuper(obj, args);
}
}
public Object createProxy(){
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(getClass());
enhancer.setCallbacks( new Callback[]{ new ClassA(), new ClassB() });
enhancer.setCallbackFilter( new CallbackFilter() {
public int accept(Method method) {
String methodName = method.getName();
if( "fun1" .equals(methodName) ){
return 0;
} else {
return 1;
}
}
});
return enhancer.create();
}
public static void main(String[] args) {
CallbackTest test = new CallbackTest();
CallbackTest obj = (CallbackTest)test.createProxy();
obj.fun1();
obj.fun2();
}
}
输出结果:
import net.sf.cglib.proxy.Mixin;
public class MixinTest {
static interface Inter1 {
void fun1(String arg0);
}
static interface Inter2 {
void fun1(String arg0);
void fun2(int arg0);
}
public static void main(String[] args) {
Mixin mixin = Mixin. create( new Class[]{Inter1. class ,Inter2.class },
new Object[]{
new Inter1() {
public void fun1(String arg0) {
System.out .println("Inter1 - " + arg0);
}
},
new Inter2() {
public void fun1(String arg0) {
System.out .println("Inter1 - " + arg0);
}
public void fun2( int arg0) {
System.out .println("Inter2 - " + arg0);
}
}
});
Inter1 inter1 = (Inter1) mixin;
inter1.fun1( "hello" );
Inter2 inter2 = (Inter2) mixin;
inter2.fun1( "world" );
inter2.fun2(999);
}
}
class MixinTest$1
implements MixinTest.Inter1
{
public void fun1(String arg0)
{
System.out.println("Inter1 - " + arg0);
}
}
class MixinTest$2
implements MixinTest.Inter2
{
public void fun1(String arg0)
{
System.out.println("Inter1 - " + arg0);
}
public void fun2(int arg0) {
System.out.println("Inter2 - " + arg0);
}
}
import java.util.Arrays;
import java.util.List;
import net.sf.cglib.beans.BeanCopier;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
public class BeanCopierTest {
static class ClassA{
private String username ;
private String password ;
private String score ;
private List<String> list ;
public String getUsername() {
return username ;
}
public void setUsername(String username) {
this .username = username;
}
public String getPassword() {
return password ;
}
public void setPassword(String password) {
this .password = password;
}
public String getScore() {
return score ;
}
public void setScore(String score) {
this .score = score;
}
public List<String> getList() {
return list ;
}
public void setList(List<String> list) {
this .list = list;
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString( this , ToStringStyle.MULTI_LINE_STYLE );
}
}
static class ClassB{
private String username ;
private String password ;
private String address ;
private List<Integer> list ;
public String getUsername() {
return username ;
}
public void setUsername(String username) {
this .username = username;
}
public String getPassword() {
return password ;
}
public void setPassword(String password) {
this .password = password;
}
public String getAddress() {
return address ;
}
public void setAddress(String address) {
this .address = address;
}
public List<Integer> getList() {
return list ;
}
public void setList(List<Integer> list) {
this .list = list;
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString( this , ToStringStyle.MULTI_LINE_STYLE );
}
}
public static void main(String[] args) {
BeanCopier beanCopier = BeanCopier.create(ClassA. class,ClassB. class ,false );
List<String> list = Arrays. asList( new String[]{ "a" ,"b" ,"c" } );
ClassA a = new ClassA();
a.setUsername( "hello" );
a.setPassword( "world" );
a.setScore( "99" );
a.setList(list);
ClassB b = new ClassB();
b.setUsername( "hello" );
b.setPassword( "world" );
b.setAddress( "beijing" );
beanCopier.copy(a, b, null );
System. out .println( a );
System. out .println( b );
}
}