当前位置:编程学习 > wap >>

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

之前给大家写过一个简单的IOC容器,这个AOP功能就是在这个上面添加的
 写 Intercept 类 继承 InvocationHandler
public 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().getInte易做图ces(), this);
}
@Override
public 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.Inte易做图ces.BeforeAfter;
import com.metarnet.Inte易做图ces.Inte易做图ce1;
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.Inte易做图ces.imps.Inte易做图ce1Impl", Intercept = true)
private Inte易做图ce1 inte易做图ce1;
@Inject(ClassName = "com.metarnet.Inte易做图ces.imps.Inte易做图ce1Impl2", Intercept = false)
private Inte易做图ce1 inte易做图ce2;
@Inject(ClassName = "com.metarnet.Inte易做图ces.imps.After", Intercept = false)
private BeforeAfter after;
@Inject(ClassName = "com.metarnet.Inte易做图ces.imps.Before", Intercept = false)
private BeforeAfter before;
public Inte易做图ce1 getInte易做图ce1() {
return inte易做图ce1;
}


public void setInte易做图ce1(Inte易做图ce1 inte易做图ce1) {
this.inte易做图ce1 = inte易做图ce1;
}
/**
 * 此方法可以修改为读取配置文件然后初始化
 */
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("inte易做图ce1");
}
public static void main(String[] args) {
IOCInit.Init();
TestIOC ioc = new TestIOC();
ioc.init();
ioc.inte易做图ce1.SayHello();
ioc.inte易做图ce2.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;
}
}


 

补充:移动开发 , IOS ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,