之前給大家寫過一個簡單的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().getInterfaces(), 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.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;
}
}