这是我在看张孝祥老师的java教程视频之后写的代码
【1.制定一个借口Advice.java】
接口描述:此接口用于实现计算调用类代理方法所用的时间
package cn.itcast.wg.proxy.aop.framework;
import java.lang.reflect.Method;
public interface Advice {
void beforeMethod(Method method);
void afterMethod(Method method);
}
【2.写一个类AdviceTest.java实现Advice.java】
类描述用于实现Advice接口
package cn.itcast.wg.proxy.aop.framework;
import java.lang.reflect.Method;
public class AdviceTest implements Advice {
private long beginTime;
private long endTime;
public void afterMethod(Method method) {
endTime = System.currentTimeMillis();
System.out.println("执行" + method.getName() + "方法用了" + (endTime - beginTime) + "毫秒" );
}
public void beforeMethod(Method method) {
beginTime = System.currentTimeMillis();
}
}
【3.写代理生产工厂ProxyBeanFactory.java】
类描述生产代理类
package cn.itcast.wg.proxy.aop.framework;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class ProxyBeanFactory {
private Advice advice;
private Object target;
public ProxyBeanFactory() {
}
public ProxyBeanFactory(Advice advice, Object target) {
super();
this.advice = advice;
this.target = target;
}
public Object getProxy() {
return Proxy.newProxyInstance(
target.getClass().getClassLoader(),
target.getClass().getInterfaces(),
new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
// TODO Auto-generated method stub
advice.beforeMethod(method);
Object objVar = method.invoke(target, args);
advice.afterMethod(method);
return objVar;
}
});
}
}
【4.写返回代理的实现BeanFactory.java】
根据配置文件的信息返回代理,还是这个接口的实现类
package cn.itcast.wg.proxy.aop.framework;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class BeanFactory {
Properties properties = new Properties();
public BeanFactory(InputStream iso) {
try {
properties.load(iso);
iso.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Object getBean(String name) {
Object obj = null;
try {
obj = Class.forName(properties.getProperty(name)).newInstance();
if(obj instanceof ProxyBeanFactory ) {
Advice advice = (Advice) Class.forName(properties.getProperty(name + ".advice")).newInstance();
Object target = Class.forName(properties.getProperty(name + ".target")).newInstance();
ProxyBeanFactory pFactory = new ProxyBeanFactory(advice,target);
return pFactory.getProxy();
}
} catch (Exception e) {
e.printStackTrace();
}
return obj;
}
}
【5.写自己的config.properties配置文件】
#xxx=java.util.ArrayList
xxx=cn.itcast.wg.proxy.aop.framework.ProxyBeanFactory
xxx.advice=cn.itcast.wg.proxy.aop.framework.AdviceTest
xxx.target=java.util.HashSet
【6.实验自己所写的框架BeanTest.java】
package cn.itcast.wg.proxy.aop.framework;
import java.io.InputStream;
import java.util.Collection;
public class BeanTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
InputStream iso = BeanTest.class.getResourceAsStream("config.properties");
BeanFactory beanFactory = new BeanFactory(iso);
Collection c = (Collection) beanFactory.getBean("xxx");
c.add(1);
}
}