本文介绍了简单Aop实现的方法,意在抛砖引玉。
首先是简单的Aspect接口
然后是代理工厂
最后是代理执行类
测试代码
执行Test,控制台返回
do Before One
do Before Two
Student
do After Two
do After One
首先是简单的Aspect接口
package org.aspect;
/**
* 切面接口
*/
public interface Aspect {
/**
* 事先执行
*/
public void doBefore();
/**
* 事后执行
*/
public void doAfter();
}
然后是代理工厂
package org.aspect;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;
/**
* 代理工厂类
*/
public class DynamicProxyFactory {
/**
* 私有构造方法
*/
private DynamicProxyFactory() {}
/**
* 工厂方法
*
* @param instance 代理目标类实例对象
* @param aspect 切面对象
*/
public static Object newInstance(Object instance, Aspect aspect) {
List<Aspect> aspectList = new ArrayList<Aspect>();
aspectList.add(aspect);
return newInstance(instance, aspectList);
}
/**
* 工厂方法
*
* @param instance 代理目标类实例对象
* @param aspectList 切面集合
*/
public static Object newInstance(Object instance, List<Aspect> aspectList) {
SimpleInvocationHandler hander = new SimpleInvocationHandler();
hander.setAspectList(aspectList);
hander.setSource(instance);
return Proxy.newProxyInstance(instance.getClass().getClassLoader(),
instance.getClass().getInterfaces(),
hander);
}
}
最后是代理执行类
package org.aspect;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.List;
/**
* 代理委托接口实现
*/
public class SimpleInvocationHandler implements InvocationHandler {
private Object source = null;
private List<Aspect> aspectList = null;
public Object getSource() {
return source;
}
public void setSource(Object source) {
this.source = source;
}
public List<Aspect> getAspectList() {
return aspectList;
}
public void setAspectList(List<Aspect> aspectList) {
this.aspectList = aspectList;
}
/**
* 委托方法
*
* @param proxy 代理对象
* @param method 代理方法
* @param args 方法参数
*/
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
for (Aspect aspect : aspectList) {
aspect.doBefore();
}
Object retObj = method.invoke(getSource(), args);
for (int index = aspectList.size() - 1; index >= 0; index--) {
aspectList.get(index).doAfter();
}
return retObj;
}
}
测试代码
public interface Student {
public void sayHello();
}
import org.aspect.Aspect;
public class StudentAspectOne implements Aspect {
@Override
public void doAfter() {
System.out.println("do After One");
}
@Override
public void doBefore() {
System.out.println("do Before One");
}
}
import org.aspect.Aspect;
public class StudentAspectTwo implements Aspect{
@Override
public void doAfter() {
System.out.println("do After Two");
}
@Override
public void doBefore() {
System.out.println("do Before Two");
}
}
public class StudentImpl implements Student {
@Override
public void sayHello() {
System.out.println("Student");
}
}
import java.util.ArrayList;
import java.util.List;
import org.aspect.Aspect;
import org.aspect.DynamicProxyFactory;
public class Test {
public static void main(String[] args) {
List<Aspect> aspectList = new ArrayList<Aspect>();
aspectList.add(new StudentAspectOne());
aspectList.add(new StudentAspectTwo());
Student s = (Student)DynamicProxyFactory.newInstance(new StudentImpl(), aspectList);
s.sayHello();
}
}
执行Test,控制台返回
do Before One
do Before Two
Student
do After Two
do After One