上一篇已经写了基于JDK的动态代理的小例子,JDK动态代理必须有接口,才能使用,但是实际情况中没有接口该怎么办呢,就得用到cglib了。Spring AOP就是基于这两种代理模式的。可任意切换,也可以强制使用cglib的代理。
此篇主要是写JDK的动态代理,把上一篇的改造成一个小型的mini框架。
首先写个接口
package org.jdkTest; public interface Hello { public void catHello(); }
package org.aop; import java.lang.reflect.Method; public interface Advice { public void before(Method method); public void after(Method method); }
package org.aop; import java.lang.reflect.Method; public class MyAdvice implements Advice{ public void after(Method method) { // TODO Auto-generated method stub System.out.println("--------------end----------"); } public void before(Method method) { // TODO Auto-generated method stub System.out.println("----------------start----------"); } }
package org.aop; import java.io.IOException; import java.io.InputStream; import java.util.Properties; 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){ ProxyFactoryBean proxyFactoryBean=(ProxyFactoryBean)bean; Object proxy=null; try { Advice advice=(Advice) Class.forName(props.getProperty(name+".advice")).newInstance(); Object target=Class.forName(props.getProperty(name+".target")); proxyFactoryBean.setAdvice(advice); proxyFactoryBean.setTarget(target); proxy = proxyFactoryBean.getProxy(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return proxy; } return bean; } }
package org.aop; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; 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(){ 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.before(method); Object result=method.invoke(target, args); advice.after(method); return result; } }); return proxy; } }
package org.aop; import java.io.InputStream; import java.util.Collection; import com.sun.org.apache.bcel.internal.generic.NEW; public class AopTest { public static void main(String[] args) { InputStream ips=AopTest.class.getResourceAsStream("config.properties"); Object bean=new BeanFactory(ips).getBean("three"); System.out.println(bean.getClass().getName()); System.out.println(bean.toString()); } }
同一个包里面写一个配置文件 config.properties
three=org.aop.ProxyFactoryBean three.advice=org.aop.MyAdvice three.target=org.jdkTest.Hello