`
elite20130514
  • 浏览: 45021 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

模拟spring AOP框架(基于annotation)

阅读更多

反射,反射,程序员的快乐。哈哈哈。

最近看了张孝祥关于模拟AOP的视频,自己动手做了修改,也吸取了一些网络上关于模拟spring aop的一些思想,做了一些整理。下面把代码分享给有需要的朋友。

bean工厂:

public class ProxyFactory {
	
	public static <T> T getBean(Class<T> clz) throws Exception{
		T t = clz.newInstance();
		Method[] methods = clz.getMethods();
		
		for(Method method:methods) {
			ProxyTag pt = method.getAnnotation(ProxyTag.class);
			
			if(pt != null) {
				ProxyBean pb =(ProxyBean)pt.proxyClass().newInstance();
				pb.setTarget(t);
				pb.setMethodName(method.getName());
			//	pb.setAdvisor(new CustomAdvisor());
				t = (T)pb.getProxy();
			}
		}
		return t;
	}
}

 

代理bean(抽象类):

abstract class ProxyBean {
	
	private Object target;
	
	private String methodName;
	
	//private Advisor advisor;

	public Object getTarget() {
		return target;
	}

	public void setTarget(Object target) {
		this.target = target;
	}

	public String getMethodName() {
		return methodName;
	}

	public void setMethodName(String methodName) {
		this.methodName = methodName;
	}
	
	/*public Advisor getAdvisor() {
		return advisor;
	}

	public void setAdvisor(Advisor advisor) {
		this.advisor = advisor;
	}*/
	
	public Object getProxy() {
		Object proxy = Proxy.newProxyInstance(
				target.getClass().getClassLoader(),
				target.getClass().getInterfaces(),
				new InvocationHandler() {
					
					@Override
					public Object invoke(Object proxy, Method method, Object[] args)
							throws Throwable {
						if(method.getName().equals(methodName)) {
							before(method);
						}
						Object retVal = method.invoke(target, args);
						if(method.getName().equals(methodName)) {
							after(method);
						}
						return retVal;
					}
				}
		);
		return proxy;
	}
	
	public abstract void before(Method method);
	
	public abstract void after(Method method);
}

 代理bean实现类,主要用于实现接口方法,将父类的实现延迟到子类(有关设计模式的东西,有点模糊,是不是应该这样写?)

public class ProxyBean1 extends ProxyBean{

	@Override
	public void before(Method method) {
		System.out.println("before the "+method.getName());
	}

	@Override
	public void after(Method method) {
		System.out.println("after the "+method.getName());
	}
	
}

target(接口-jdk代理):

public interface Target {
	void reflect();
	void reflect2();
	void reflect3();
}

 target实现类:

public class TargetDerive implements Target{
	
	@ProxyTag(proxyClass=ProxyBean1.class)
	public void reflect() {
		System.out.println("enjoy the fun of reflect...");
	}
	
	@ProxyTag(proxyClass=ProxyBean1.class)
	public void reflect2() {
		System.out.println("enjoy the fun of reflect...");
	}
	
	public void reflect3() {
		System.out.println("enjoy the fun of reflect...");
	}
}

注解类:

@Retention(RetentionPolicy.RUNTIME)	//保留到运行时,可通过反射获取
@Target(ElementType.METHOD)			//目标于方法上
public @interface ProxyTag {
	public Class proxyClass();
}

 测试:

public class AopTest {
	public static void main(String[] args) throws Exception {
		Target target = ProxyFactory.getBean(TargetDerive.class);
		target.reflect();
		target.reflect2();
		target.reflect3();
	}
}

 

 输出:

before the reflect
enjoy the fun of reflect...
after the reflect
before the reflect2
enjoy the fun of reflect...
after the reflect2
enjoy the fun of reflect...

 

 一定要多比较,多思考,多总结。。。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics