您的位置:首页 > 职场人生

黑马程序员_JAVA学习笔记11

2014-02-18 20:22 411 查看
----------------------
ASP.Net+Android+IOS开发、.Net培训、期待与您交流! ----------------------详细请查看:http://edu.csdn.net

//下面是动态代理 , 的一个小的框架 , 与spring类似。

ArrayList al1 = new ArrayList();

MyAdvice ma = new MyAdvice();

Collection c3 = (Collection)getProxy(al1,ma);

c3.add("aaa");

c3.add("bbb");

c3.add("ccc");

System.out.println(c3.size());

}

private static Object getProxy(final Object al,final Advice advice) {

Object c3 = Proxy.newProxyInstance(

Object.class.getClassLoader(),

new Class[]{Collection.class},

new InvocationHandler(){

public Object invoke(Object proxy, Method method, Object[] args)

throws Throwable {

//ArrayList al = new ArrayList();

//long beginTime = System.currentTimeMillis();

advice.before(method);

Object o = method.invoke(al, args);

advice.after(method);

//long endTime = System.currentTimeMillis();

//System.out.println(method.getName() + " time: "+ 

//(endTime - beginTime));

return o;

}

});

return c3;



上面是主类中的代码。

下面是一个接口,Advice 与Advice 的实现类,MyAdvice:

public interface Advice {

void before(Method method);

void after(Method method); 

}

public class MyAdvice implements Advice{

long beginTime ; 

public void after(java.lang.reflect.Method method) {

System.out.println("结束:");

long endTime = System.currentTimeMillis();

System.out.println("Time : " + (endTime - beginTime));

};

public void before(Method method) {

System.out.println("开始:");

beginTime = System.currentTimeMillis();

}



上面就是把代理与系统功能生成了对象,只要把对象传入就可以调用相应 的方法,类似动态的实现。

下面是类似spring的 AOP框架的简单实现 :

下面是利用代理对象做的简单的spring框架:

添加的切面代码的接口,

public interface Advice {

void before(Method method);

void after(Method method);



//这个是上面接口的实现类

public class MyAdvice implements Advice{

long beginTime ; 

public void after(java.lang.reflect.Method method) {

System.out.println("结束:");

long endTime = System.currentTimeMillis();

System.out.println("Time : " + (endTime - beginTime));

};

public void before(Method method) {

System.out.println("开始:");

beginTime = System.currentTimeMillis();

}

}

//这个用于得到 javaBean对象,传递一个inputStream对象把一个配置文件传入,

//用属性文件得到其中的java类。看是否是代理对象如果是代理对象返回代理,如果不是就返//回该对象的实例。

 public class BenFactory {

Properties pros = new Properties();

public BenFactory(InputStream is)

{

try {

pros.load(is);

} catch (IOException e) {

e.printStackTrace();

}

}

public Object getBean(String name) throws Exception

{

//传递的名字是配置文件中的 名字。

//下面可以用反射得到该类的 实例对象。

String className = pros
4000
.getProperty(name);

//bean是 反射得到的普通对象

// proxy是 反射得到的代理对象

Object bean = null;

Object proxy = null;

Class c = Class.forName(className);

bean = c.newInstance();

if(bean instanceof ProxyBeanFactory)

{

ProxyBeanFactory pbf = (ProxyBeanFactory)bean;

//下面是利用反射得到 代理对象中 用到 的切面对象 的实例对象,传入代理 //类。

Advice advice = (Advice)Class.forName(pros.getProperty(name + ".advice")).newInstance();

Object target = Class.forName(pros.getProperty(name + ".target")).newInstance();

//为这两个对象初始化

pbf.setAdvice(advice);

pbf.setTarget(target);

proxy = pbf.getProxy();

//返回代理

return proxy;

}

//返回普通对象

return bean;

}  

//处理代理对象的类 

public class ProxyBeanFactory{

private static Advice advice;

private static Object target;

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;

}

public  Object getProxy() { 

//得到实例,调用相应的invoke方法去得到实例对象。

Object c3 = Proxy.newProxyInstance(

Object.class.getClassLoader(),

new Class[]{Collection.class},

new InvocationHandler(){

public Object invoke(Object proxy, Method method, Object[] args)

throws Throwable {

//ArrayList al = new ArrayList();

//long beginTime = System.currentTimeMillis();

advice.before(method);

Object o = method.invoke(target, args);

advice.after(method);

//long endTime = System.currentTimeMillis();

//System.out.println(method.getName() + " time: "+ 

//(endTime - beginTime));

return o;

}

});

return c3;

}



//测试类

public class Spring {

public static void main(String[] args) throws Exception {

InputStream ips =  Spring.class.getResourceAsStream("config.properties");

Object bean = new BenFactory(ips).getBean("xxx");

System.out.println(bean.getClass().getName());





//配置文件信息。

#xxx=java.util.ArrayList

xxx=DaiLi.ProxyBeanFactory

xxx.advice=DaiLi.MyAdvice

xxx.target=java.util.ArrayList
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: