/** * 主题接口或者说目标类接口 * @author zhu * */ public interface Subject { void doSomething(); // void doOtherthing(); }
public class Target implements Subject { @Override public void doSomething() { System.out.println("目标类方法"); } }
public class Proxy implements Subject { private Target target = new Target(); @Override public void doSomething() { System.out.println("系统功能:执行前!"); target.doSomething(); System.out.println("系统功能:执行后!"); } }
public class Client { public static void main(String[] args) { Subject subject = new Proxy(); subject.doSomething(); } }
Class clazzProxy1 = Proxy.getProxyClass(Collection.class.getClassLoader(), Collection.class);// 第一步:获取Class字节码对象 Constructor constructor = clazzProxy1.getConstructor(InvocationHandler.class);// 第二步:通过Class得到构造器 class MyInvocationHander1 implements InvocationHandler{ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { return null; } } Collection proxy = (Collection)constructor.newInstance(new MyInvocationHander1());// 第三步:获取Collection的代理类
final ArrayList target = new ArrayList(); Collection proxy = (Collection)Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), new InvocationHandler(){ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { return null; } });
public interface Advice { void beforeMethod(Method method); void afterMethod(Method method); }
public class MyAdvice implements Advice { long beginTime = 0; public void afterMethod(Method method) { // TODO Auto-generated method stub System.out.println("从传智播客毕业上班啦!"); long endTime = System.currentTimeMillis(); System.out.println(method.getName() + " running time of " + (endTime - beginTime)); } public void beforeMethod(Method method) { // TODO Auto-generated method stub System.out.println("到传智播客来学习啦!"); beginTime = System.currentTimeMillis(); } }
private static Object getProxy(final Object target,final Advice advice) { Object proxy3 = Proxy.newProxyInstance( target.getClass().getClassLoader(), /*new Class[]{Collection.class},*/ target.getClass().getInterfaces(), new InvocationHandler(){ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { /*long beginTime = System.currentTimeMillis(); Object retVal = method.invoke(target, args); long endTime = System.currentTimeMillis(); System.out.println(method.getName() + " running time of " + (endTime - beginTime)); return retVal;*/ advice.beforeMethod(method); Object retVal = method.invoke(target, args); advice.afterMethod(method); return retVal; } } ); return proxy3; }
final ArrayList target = new ArrayList(); Collection proxy = (Collection)getProxy(target,new MyAdvice()); proxy.add("zxx");
xxx.advice=cn.itcast.MyAdvice
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){ 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 { 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; } }(2)ProxyFacotryBean充当封装生成动态代理的工厂,需要为工厂类提供哪些配置参数信息?目标和通知
public class ProxyFactoryBean { private Advice advice; private 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() { // TODO Auto-generated method stub Object proxy3 = Proxy.newProxyInstance( target.getClass().getClassLoader(), /*new Class[]{Collection.class},*/ target.getClass().getInterfaces(), new InvocationHandler(){ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { /*long beginTime = System.currentTimeMillis(); Object retVal = method.invoke(target, args); long endTime = System.currentTimeMillis(); System.out.println(method.getName() + " running time of " + (endTime - beginTime)); return retVal;*/ advice.beforeMethod(method); Object retVal = method.invoke(target, args); advice.afterMethod(method); return retVal; } } ); return proxy3; } }
public class AopFrameworkTest {// 通过配置文件获取目标对象和通告对象 /** * 1、拿到配置文件 * 2、通过类名称得到类的实例对象 */ public static void main(String[] args) throws Exception { // TODO Auto-generated method stub InputStream ips = AopFrameworkTest.class.getResourceAsStream("config.properties"); Object bean = new BeanFactory(ips).getBean("xxx"); System.out.println(bean.getClass().getName()); // ((Collection)bean).clear(); } }
2、代理与jdbc
动态代理一个典型应用就是实现数据库的连接池。由于使用JDBC的程序员习惯在使用完数据库的Connection对象后调用该对象的close方法关闭连接,这样连接池就失去作用了。这个问题我们可以使用动态代理技术来解决,当程序员调用Connection的close方法时,我们将其拦截下来,将其归还到连接池中,而不是直接关闭物理连接。
原文地址:http://blog.csdn.net/u010213127/article/details/44835915