黑马程序员技术交流社区
标题:
哪位大侠帮忙解析一下代理,进来看看
[打印本页]
作者:
路漫漫_求索
时间:
2014-5-26 12:40
标题:
哪位大侠帮忙解析一下代理,进来看看
本帖最后由 路漫漫_求索 于 2014-5-31 10:38 编辑
------------------配置文件---------------------------config.properties配置文件它是怎么被调用的?希望哪位大侠详细解析一下。。。。
#xxx=java.util.ArrayList
xxx=cn.itcast.day3.aopframwork.ProxyFactoryBean
xxx.advice=cn.itcast.day3.MyAdvice
xxx.target=java.util.ArrayList
------------------测试代理---------------------------public class AopFramewokeTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
//1.首先搞到一个流加载配置文件,用相对路径获取
InputStream ips = AopFramewokeTest.class.getResourceAsStream("config.properties");
//2.获取bean
Object bean = new BeanFactory(ips).getBean("xxx");
//3.
System.out.println(bean.getClass().getName());
}
------------------BeanFactory---------------------------
package cn.itcast.day3.aopframwork;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import cn.itcast.day3.Advice;
/*
* BeanFactory:这是Java一个工厂类
*
*/
public class BeanFactory {
Properties props = new Properties();
public BeanFactory(InputStream ips)
try {
props.load(ips);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Object getBean(String name){ //疑惑1:这个getBean方法是BeanFactor的方法嘛?可以把它理解成thread线程中的run, 对象一创建,就会存在这个方法?
//疑惑2:这个方法里面接收的String name是谁给它传递的参数?
String className = props.getProperty(name)
Object bean = null
try {
Class clazz = Class.forName(className)
bean = clazz.newInstance()
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(bean instanceof ProxyFactoryBean)
Object proxy = null;
ProxyFactoryBean proxyfactoryBean = (ProxyFactoryBean)bean;
try {
//疑惑3:这个代码块的各个步骤大家都是怎么理解的,我看了几遍,还是不太明白Advice和Target是怎么调用关系的
Advice advice = (Advice)Class.forName(props.getProperty(name+".advice")).newInstance();
Object target = Class.forName(props.getProperty(name+".target")).newInstance();
proxyfactoryBean.setAdvice(advice);
proxyfactoryBean.setTarget(target);
proxy = proxyfactoryBean.getProxy();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return proxy;
}
return bean;//否则,就返回bean
}
}
------------------------------ProxyFactoryBean--------------------------------------
package cn.itcast.day3.aopframwork;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import cn.itcast.day3.Advice;
public class ProxyFactoryBean {
public Advice getAdvice() {
return advice;
}
public void setAdvice(Advice advice) {
this.advice = advice;
}
public Object getTarget() {
return target;
}
public void setTarget(Object target) {
this.target = target;
}
private Advice advice;
private Object target;
public Object getProxy() {
Object proxy3 = Proxy.newProxyInstance(
target.getClass().getClassLoader(), //通过目标类获取加载器
target.getClass().getInterfaces(), //根据目标,获取相应接口
new InvocationHandler(){ //将InvocationHandler匿名实例化
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
/*Long startTime = System.currentTimeMillis();
Object reVal = method.invoke(target, args);
Long endTime = System.currentTimeMillis();
System.out.println(method.getName()+"rum Time of "+(endTime - startTime));
return reVal;*/
//通过接口,创建系统功能
advice.beforMethod(method);
Object reVal = method.invoke(target, args);
advice.afterMethod(method);
return reVal;
}
});
return proxy3;
}
}
作者:
yinxjfly
时间:
2014-5-26 15:39
第一遍学代理肯定多少迷糊,这是基础没学扎实的原因,先搞基础的东西吧!
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2