內容java
java基礎鞏固筆記 - 實現AOP功能的封裝與配置的小框架程序員
設計(目錄):編程
XXX = java.util.ArrayList中框架
代碼ide
Advice接口學習
MyAdvice類測試
BeanFactory類this
ProxyFactoryBean類設計
AopFrameWorkTest類代理
輸出
獲取源碼,學習交流,那就加入小編的學習交流羣吧!616 959 444
根據配置文件的鍵xxx對應的值(類全名)建立相應類的對象。
當且僅當xxx對應的值爲com.iot.proxy.aopframework.ProxyFactoryBean時,則生成相應的動態代理類對象。代理對象的目標類和通知實現類分別由xxx.target和xxx.advice配置
配置文件
瞭解更多Java知識,獲取原視頻,源碼,學習交流,那就加入小編的學習交流羣吧!616 959 444
config.propertiest位於aopframework包下
xxx表明要加載的類
xxx.advice表明通知接口的某個實現類
xxx.target表明委託類
#xxx=java.util.ArrayList
xxx=com.iot.proxy.aopframework.ProxyFactoryBean
xxx.advice=com.iot.proxy.MyAdvice
xxx.target=java.util.ArrayList
包:com.iot.proxy.aopframework,有以下幾個類/接口:
BeanFactory,用於讀取配置文件,根據配置建立相應的對象
ProxyFactoryBean,用於生成代理對象,含有兩個私有屬性:目標和通知
Advice,通知接口,用於把切面的代碼以對象的形式傳遞給InvocationHandler的的invoke方法
MyAdvice,Advice接口的一個實現類,打印執行方法前的時間及執行耗時
AopFrameWorkTest,測試效果
Advice接口
package com.iot.proxy.aopframework;
import java.lang.reflect.Method;
/**
* Created by brian on 2016/2/2.
*/
public interface Advice {
void beforeMethod(Method method);
void aftereMethod(Method method);
}
package com.iot.proxy.aopframework;
import java.lang.reflect.Method;
/**
* Created by brian on 2016/2/2.
*/
public class MyAdvice implements Advice{
long beginTime = 0 ;
@Override
public void beforeMethod(Method method) {
System.out.println(method.getName()+" before at "+beginTime);
beginTime = System.currentTimeMillis();
}
@Override
public void aftereMethod(Method method) {
long endTime = System.currentTimeMillis();
System.out.println(method.getName()+" cost total "+ (endTime-beginTime));
}
}
獲取源碼,學習交流,那就加入小編的學習交流羣吧!
616
959
444
package com.iot.proxy.aopframework;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* Created by brian on 2016/2/2.
*/
public class BeanFactory {
Properties properties = new Properties();
public BeanFactory(InputStream inputStream){
try {
properties.load(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public Object getBean(String name){
String className = properties.getProperty(name);
Object bean = null;
try {
Class clazz = Class.forName(className);
bean = clazz.newInstance();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
if (bean instanceof ProxyFactoryBean){
ProxyFactoryBean proxyFactoryBean = (ProxyFactoryBean)bean;
Advice advice = null;
Object target = null;
try {
advice = (Advice) Class.forName(properties.getProperty(name+".advice")).newInstance();
target = Class.forName(properties.getProperty(name+".target")).newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
proxyFactoryBean.setAdvice(advice);
proxyFactoryBean.setTarget(target);
Object proxy = ((ProxyFactoryBean) bean).getProxy();
return proxy;
}
return bean;
}
}
package com.iot.proxy.aopframework;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
* Created by brian on 2016/2/3.
*/
public class ProxyFactoryBean {
private Object target;
private Advice advice;
public Object getProxy(){
Object proxy = Proxy.newProxyInstance(
target.getClass().getClassLoader(),
target.getClass().getInterfaces(),
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
advice.beforeMethod(method);
Object retVal = method.invoke(target,args);
advice.aftereMethod(method);
return retVal;
}
}
);
return proxy;
}
public Object getTarget() {
return target;
}
public void setTarget(Object target) {
this.target = target;
}
public Advice getAdvice() {
return advice;
}
public void setAdvice(Advice advice) {
this.advice = advice;
}
}
package com.iot.proxy.aopframework;
import java.io.InputStream;
import java.util.Collection;
/**
* Created by brian on 2016/2/3.
*/
public class AopFrameWorkTest {
public static void main(String[] args) {
InputStream inputStream = AopFrameWorkTest.class.getResourceAsStream("config.properties");
Object bean = new BeanFactory(inputStream).getBean("xxx");
System.out.println(bean.getClass().getName());
((Collection) bean).clear();
}
}
獲取源碼,學習交流,那就加入小編的學習交流羣吧!616 959 444
配置
xxx=com.iot.proxy.aopframework.ProxyFactoryBean
輸出爲:
com.sun.proxy.$Proxy0clear before at 0clear cost total 0
配置
xxx=java.util.ArrayList
輸出爲:
java.util.ArrayList
能夠看出,只改變配置文件,就可改變代碼的運行結果,從而達到靈活的效果