重复造轮子--IOC容器的AOP简单实现(2)
}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() {
相关新闻>>
- 发表评论
-
- 最新评论 更多>>