CGlib动态代理CallbackFilter

1、Car.java

package com.ast.cglib.test;

public class Car {

}

2、Truck.java

package com.ast.cglib.test;

public class Truck extends Car{

}

3、MyInterceptor.java

package com.ast.cglib.test;

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

public class MyInterceptor implements MethodInterceptor{

	@Override
	public Object intercept(Object invoker, Method method, Object[] args,
			MethodProxy proxy) throws Throwable {
		
		System.out.println("NewCarInterceptor intercept before invoke");
		
		Object result = proxy.invokeSuper(invoker, args);
		
		System.out.println("NewCarInterceptor intercept after invoke");
		
		return result;
	}

}

4、MyCallbackFilter.java

package com.ast.cglib.test;

import java.lang.reflect.Method;

import net.sf.cglib.proxy.CallbackFilter;

public class MyCallbackFilter implements CallbackFilter {
	
	public int accept(Method method) {
		if (method.getName().equals("NewTruck")) {
			return 0;
		} else {
			return 1;
		}
	}
}

5、CarFactory.java

package com.ast.cglib.test;

import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.NoOp;

public class CarFactory{
	public Car NewCar()
	{
		System.out.println("CarFactory NewCar");
		return new Car();
	}
	
	public Truck NewTruck()
	{
		System.out.println("CarFactory NewTruck");
		return new Truck();
	}
	
	public static void EnhancerTest()
	{
		Callback[] callbacks = new Callback[] {new MyInterceptor(),  NoOp.INSTANCE};
		Enhancer enhancer = new Enhancer();
		enhancer.setSuperclass(CarFactory.class);
		enhancer.setCallbacks(callbacks);
		enhancer.setCallbackFilter(new MyCallbackFilter());
		
		CarFactory fac = (CarFactory)enhancer.create();
		//NoOp
		fac.NewCar();
		//MyInterceptor
		fac.NewTruck();
	}
	
	public static void main(String[] args)
	{
		CarFactory.EnhancerTest();
	}
}

CGlib类型整合范例Mixin

1、IA1.java

package com.ast.cglib.test;

public interface IA1 {
	void methodA1();
}

2、IA2.java

package com.ast.cglib.test;

public interface IA2 {
	void methodA2();
}

3、A1Impl.java

package com.ast.cglib.test;

public class A1Impl implements IA1{

	@Override
	public void methodA1() {
		System.out.println("A1Impl methodA1");
	}

}

4、A2Impl.java

package com.ast.cglib.test;

public class A2Impl implements IA2{

	@Override
	public void methodA2() {
		System.out.println("A2Impl methodA2");
	}

}

5、MixinTest.java

package com.ast.cglib.test;

import net.sf.cglib.proxy.Mixin;

public class MixinTest {
	public static void main(String[] args) {

		Class[] interfaces = new Class[] { IA1.class, IA2.class };

		Object[] delegates = new Object[] { new A1Impl(), new A2Impl() };

		Object obj = Mixin.create(interfaces, delegates);

		IA1 a1 = (IA1) obj;
		a1.methodA1();

		IA2 a2 = (IA2) obj;
		a2.methodA2();
	}
}

CGlib动态代理范例

1、ProxyFactory.java

package com.ats.proxy;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

public class ProxyFactory implements MethodInterceptor{
	//private Object invoker;
	private List<Object> interceptors;
	
	private ProxyFactory(Object invoker,List<Object> interceptors)
	{
		//this.invoker = invoker;
		if(interceptors==null)
		{
			this.interceptors = new ArrayList<Object>();
		}
		else
		{
			this.interceptors = interceptors;
		}
	}
	
	 public static final Object newInstance(Object invoker,List<Object> interceptors)
	 {
		 Enhancer enhancer = new Enhancer();
		 enhancer.setSuperclass(invoker.getClass());
		 enhancer.setCallback(new ProxyFactory(invoker,interceptors));
		 return enhancer.create();
	 }
	
	@Override
	public Object intercept(Object invoker, Method method, Object[] args,
			MethodProxy proxy) throws Throwable {
		
		Object result = null;
		
		for(Object o : interceptors)
		{
			if(o instanceof IProxyBefore)
			{
				((IProxyBefore)o).BeforeInvoke();
			}
		}
		
		try
		{
			result = proxy.invokeSuper(invoker, args);
		}
		catch(Exception ex)
		{
			for(Object o : interceptors)
			{
				if(o instanceof IProxyThrow)
				{
					((IProxyThrow)o).ThrowInvoke();
				}
			}
		}
		
		for(Object o : interceptors)
		{
			if(o instanceof IProxyAfter)
			{
				((IProxyAfter)o).AfterInvoke();
			}
		}
		
		return result;
	}
}

2、IProxyBefore.java

package com.ats.proxy;

public interface IProxyBefore {
	public void BeforeInvoke();
}

3、IProxyAfter.java

package com.ats.proxy;

public interface IProxyAfter {
	public void AfterInvoke();
}

4、IProxyAround.java

package com.ats.proxy;

public interface IProxyAround extends IProxyBefore,IProxyAfter{
}

5、IProxyThrow.java

[code lang="java"]
package com.ats.proxy;

public interface IProxyThrow {
	public void ThrowInvoke();
}

6、Car.java

package com.ats.test;

public class Car {
	public Car()
	{
		System.out.println("This is a new Car");
	}
}

7、CarFactoryBefore.java

package com.ats.test;

import com.ats.proxy.IProxyBefore;

public class CarFactoryBefore implements IProxyBefore{

	@Override
	public void BeforeInvoke() {
		System.out.println("CarFactoryBefore BeforeInvoke");
	}

}

8、CarFactoryAfter.java

package com.ats.test;

import com.ats.proxy.IProxyAfter;

public class CarFactoryAfter implements IProxyAfter {

	@Override
	public void AfterInvoke() {
		System.out.println("CarFactoryAfter AfterInvoke");
	}

}

9、CarFactoryAround.java

package com.ats.test;

import com.ats.proxy.IProxyAround;

public class CarFactoryAround implements IProxyAround{
	@Override
	public void AfterInvoke() {
		System.out.println("CarFactoryAround AfterInvoke");
	}
	
	@Override
	public void BeforeInvoke() {
		System.out.println("CarFactoryAround BeforeInvoke");
	}
}

10、CarFactoryThrow.java

package com.ats.test;

import com.ats.proxy.IProxyThrow;

public class CarFactoryThrow implements IProxyThrow {
	@Override
	public void ThrowInvoke() {
		System.out.println("CarFactory ThrowInvoke");
	}
}

11、ProxyFactory.java

package com.ats.test;

import java.util.ArrayList;
import java.util.List;

import com.ats.proxy.ProxyFactory;

public class CarFactory{
	public Car NewCar()
	{
		return new Car();
	}
	
	public static void main(String[] args)
	{
		CarFactory fac = new CarFactory();
		CarFactoryAfter after = new CarFactoryAfter();
		CarFactoryBefore before = new CarFactoryBefore();
		CarFactoryAround around = new CarFactoryAround();
		CarFactoryThrow _throw = new CarFactoryThrow();
		List<Object> l = new ArrayList<Object>();
		l.add(after);
		l.add(before);
		l.add(around);
		l.add(_throw);
		
		CarFactory fac1=(CarFactory)ProxyFactory.newInstance(fac, l);
		fac1.NewCar();
	}
}

JDK动态代理范例

1、ProxyFactory.java

package com.ats.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class ProxyFactory implements InvocationHandler{
	private Object invoker;
	private List<Object> interceptors;
	
	private ProxyFactory(Object invoker,List<Object> interceptors)
	{
		this.invoker = invoker;
		if(interceptors==null)
		{
			this.interceptors = new ArrayList<Object>();
		}
		else
		{
			this.interceptors = interceptors;
		}
	}
	
    public static final Object newInstance(Object invoker,List<Object> interceptors)
    {
        return java.lang.reflect.Proxy.newProxyInstance(invoker.getClass().getClassLoader(),
        		invoker.getClass().getInterfaces(), new ProxyFactory(invoker,interceptors));
    }
	
	@Override
	public Object invoke(Object proxy, Method method, Object[] args)
			throws Throwable {
		Object result = null;
		
		for(Object o : interceptors)
		{
			if(o instanceof IProxyBefore)
			{
				((IProxyBefore)o).BeforeInvoke();
			}
		}
		
		try
		{
			result = method.invoke(invoker, args);
		}
		catch(Exception ex)
		{
			for(Object o : interceptors)
			{
				if(o instanceof IProxyThrow)
				{
					((IProxyThrow)o).ThrowInvoke();
				}
			}
		}
		
		for(Object o : interceptors)
		{
			if(o instanceof IProxyAfter)
			{
				((IProxyAfter)o).AfterInvoke();
			}
		}
		return result;
	}
}

2、IProxyBefore.java

package com.ats.proxy;

public interface IProxyBefore {
	public void BeforeInvoke();
}

3、IProxyAfter.java

package com.ats.proxy;

public interface IProxyAfter {
	public void AfterInvoke();
}

4、IProxyAround.java

package com.ats.proxy;

public interface IProxyAround extends IProxyBefore,IProxyAfter{
}

5、IProxyThrow.java

package com.ats.proxy;

public interface IProxyThrow {
	public void ThrowInvoke();
}

6、ICarFactory.java

package com.ats.test;

public interface ICarFactory {
	public Car NewCar();
}

7、Car.java

package com.ats.test;

public class Car {
	public Car()
	{
		System.out.println("This is a new Car");
	}
}

8、IProxyBefore.java

package com.ats.test;

import com.ats.proxy.IProxyBefore;

public class CarFactoryBefore implements IProxyBefore{

	@Override
	public void BeforeInvoke() {
		System.out.println("CarFactoryBefore BeforeInvoke");
	}

}

9、CarFactoryAfter.java

package com.ats.test;

import com.ats.proxy.IProxyAfter;

public class CarFactoryAfter implements IProxyAfter {

	@Override
	public void AfterInvoke() {
		System.out.println("CarFactoryAfter AfterInvoke");
	}

}

10、CarFactoryAround .java

package com.ats.test;

import com.ats.proxy.IProxyAround;

public class CarFactoryAround implements IProxyAround{
	@Override
	public void AfterInvoke() {
		System.out.println("CarFactoryAround AfterInvoke");
	}
	
	@Override
	public void BeforeInvoke() {
		System.out.println("CarFactoryAround BeforeInvoke");
	}
}

11、CarFactoryThrow.java

package com.ats.test;

import com.ats.proxy.IProxyThrow;

public class CarFactoryThrow implements IProxyThrow {
	@Override
	public void ThrowInvoke() {
		System.out.println("CarFactory ThrowInvoke");
	}
}

12、CarFactory.java

package com.ats.test;

import java.util.ArrayList;
import java.util.List;

import com.ats.proxy.IProxyAround;
import com.ats.proxy.IProxyThrow;
import com.ats.proxy.ProxyFactory;

public class CarFactory implements ICarFactory {
	public Car NewCar()
	{
		return new Car();
	}
	
	public static void main(String[] args)
	{
		CarFactory fac = new CarFactory();
		CarFactoryAfter after = new CarFactoryAfter();
		CarFactoryBefore before = new CarFactoryBefore();
		CarFactoryAround around = new CarFactoryAround();
		CarFactoryThrow _throw = new CarFactoryThrow();
		List<Object> l = new ArrayList<Object>();
		l.add(after);
		l.add(before);
		l.add(around);
		l.add(_throw);
		
		ICarFactory factory = (ICarFactory)ProxyFactory.newInstance(fac,l);
		factory.NewCar();
	}
}