读书人

反复造轮子-IOC容器的AOP简单实现

发布时间: 2012-11-23 00:03:43 作者: rapoo

重复造轮子--IOC容器的AOP简单实现
之前给大家写过一个简单的IOC容器,这个AOP功能就是在这个上面添加的 写 Intercept 类 继承 InvocationHandlerpublic class Intercept implements InvocationHandler{/** * 要处理的对象(也就是我们要在方法的前后加上业务逻辑的对象,如例子中的Hello) */private Object target; // 被代理的目标对象 /** * 动态生成方法被处理过后的对象 (写法固定) * target(需要关注的类方法) */public Object bind(Object target) throws InstantiationException, IllegalAccessException{this.target = target;return Proxy.newProxyInstance(this.target.getClass().getClassLoader(), this.target.getClass().getInterfaces(), this);}@Overridepublic Object invoke(Object proxy, Method method, Object[] args)throws Throwable {Object result;AOPModel model=null;if(InterceptList.getInterceptMethod().containsKey(method.getName())){model=InterceptList.getInterceptMethod().get(method.getName());this.monitor(model.getBefore(), model.getBefore_aspectName(),this.target);result = method.invoke(this.target, args);this.monitor(model.getAfter(), model.getAfter_aspectName(),this.target);}else {result = method.invoke(this.target, args);}return result;}/** * 依据是否注入横切关注点来决定before、after的调用 */ private void monitor(Object clazz, String aspectName,Object target) throws Exception { if (clazz != null) { Method[] methods = clazz.getClass().getDeclaredMethods(); for (Method method : methods) { if (method.getName().equals(aspectName)) { method.invoke(clazz,target); } } } } }

然后修改我们的IOC容器:当用户继承IOC容器后,自动查找用户配置的参数进行初始化

注意,此时使用this,new Intercept().bind()Load对象

public class IOC {public IOC(){Init();}public void Init(){Field[] fields = getClass().getDeclaredFields();for(Field field : fields){Inject inject=field.getAnnotation(Inject.class);try {if(inject!=null){field.setAccessible(true);if(inject.Intercept()){field.set(this,new Intercept().bind(Class.forName(inject.ClassName().trim()).newInstance()));}else {field.set(this,Class.forName(inject.ClassName().trim()).newInstance());}}} catch (Exception e) {Logger.getLogger(getClass()).error("初始化容器异常:"+inject.ClassName().trim()+"初始化失败",e);}}}}

测试类

package com.metarnet.Main;
import com.metarnet.Injects.Inject;import com.metarnet.Interfaces.BeforeAfter;import com.metarnet.Interfaces.Interface1;import com.metarnet.extend.AOPModel;import com.metarnet.extend.IOC;import com.metarnet.extend.IOCInit;import com.metarnet.extend.InterceptList;
public class TestIOC extends IOC{public TestIOC(){super();}
@Inject(ClassName = "com.metarnet.Interfaces.imps.Interface1Impl", Intercept = true)private Interface1 interface1;@Inject(ClassName = "com.metarnet.Interfaces.imps.Interface1Impl2", Intercept = false)private Interface1 interface2;@Inject(ClassName = "com.metarnet.Interfaces.imps.After", Intercept = false)private BeforeAfter after;@Inject(ClassName = "com.metarnet.Interfaces.imps.Before", Intercept = false)private BeforeAfter before;public Interface1 getInterface1() {return interface1;}
public void setInterface1(Interface1 interface1) {this.interface1 = interface1;}/** * 此方法可以修改为读取配置文件然后初始化 */public void init(){AOPModel model=new AOPModel();model.setAfter(after);model.setBefore(before);model.setAfter_aspectName("SayAfter");model.setBefore_aspectName("SayBefore");InterceptList.getInterceptMethod().put("SayHello", model);InterceptList.getInterceptList().add("interface1");}public static void main(String[] args) {IOCInit.Init();TestIOC ioc = new TestIOC();ioc.init();ioc.interface1.SayHello();ioc.interface2.SayHello();}
} Model

/** * 需要横切的列表 */public class AOPModel {private Object before;// before 横切关注点 (之前调用) private Object after; // after 横切关注点 (之后调用) private String before_aspectName;//before横切调用的方法 private String after_aspectName;//after横切调用的方法public Object getBefore() {return before;}public void setBefore(Object before) {this.before = before;}public Object getAfter() {return after;}public void setAfter(Object after) {this.after = after;}public String getBefore_aspectName() {return before_aspectName;}public void setBefore_aspectName(String before_aspectName) {this.before_aspectName = before_aspectName;}public String getAfter_aspectName() {return after_aspectName;}public void setAfter_aspectName(String after_aspectName) {this.after_aspectName = after_aspectName;} }

/** * 需要监听的方法列表 */public final class InterceptList {private static HashMap<String,AOPModel> map = new HashMap<String,AOPModel>();private static ArrayList<String> list = new ArrayList<String>();public static HashMap<String,AOPModel> getInterceptMethod() {return map;}public static ArrayList<String> getInterceptList(){return list;}}

读书人网 >软件架构设计

热点推荐