动态代理和静态代理以及spring的aop

来源:互联网 发布:淘宝童装店铺起名 编辑:程序博客网 时间:2024/05/22 07:54

原文链接:


[java] view plain copy
  1. <span style="font-family:Arial, Verdana, sans-serif;white-space: normal; background-color: rgb(255, 255, 255); ">代理模式:为其他对象提供一种代理以控制对这个对象的访问。</span>  



代理角色和真是角色均实现同一个抽象角色,在代理角色内部有对真实角色的引用,从而用户可以通过对代理角色的访问来访问真是角色。


动态代理:被代理的对象可以在运行时动态的进行改变,增加了灵活性。

实现一个动态代理:

1.创建被代理的类以及接口。
[java] view plain copy
  1. public interface Subject  
  2. {  
  3.     public void request();  
  4. }  
[java] view plain copy
  1. public class RealSubject implements Subject  
  2. {  
  3.   
  4.     public void request()  
  5.     {  
  6.         System.out.println("realSubject");  
  7.           
  8.     }  
  9.       
  10. }  


2。创建一个实现接口InvocationHandler的类,实现其invoke的方法。
[java] view plain copy
  1. public class DynamicProxy implements InvocationHandler  
  2. {  
  3.     private Object obj;  
  4.       
  5.     public DynamicProxy(Object obj)  
  6.     {  
  7.         this.obj = obj;  
  8.     }  
  9.       
  10.     public Object invoke(Object proxy, Method method, Object[] args)  
  11.             throws Throwable  
  12.     {     
  13.         method.invoke(obj, args);     
  14.         return null;  
  15.     }  
  16. }  
在该类中有一个Object类型的成员变量,并且通过构造方法为其赋值,因为类型是Object,所以我们能够为任何类型的类做代理
查看java API帮助文档关于InvocationHandler接口的invoke()方法的说明:在代理实例上处理方法调用并返回结果。在与方法关联的代理实例上调用方法时,将在调用处理程序上调用此方法。即在第三步中执行subject.request()的时候会转到该方法中,并传入对应的方法以及参数


3。通过proxy的静态方法newProxyInstance(ClassLoader loader, Class[] interfaces, InvocationHandler handler)创建一个代理,然后我们就可以通过这个代理来对真是对象进行访问了
[java] view plain copy
  1. public class Client  
  2. {  
  3.     public static void main(String[] args)  
  4.     {  
  5.         RealSubject sub = new RealSubject();  
  6.         InvocationHandler handler = new DynamicProxy(sub);  
  7.         Class<?> classType = handler.getClass();  
  8.                 //此处只能向上转型为Subject,而不能转型为RealSubject具体类型,具体类型是在invoke()方法中进行判断的  
  9.                 Subject subject = (Subject)Proxy.newProxyInstance(classType.getClassLoader(), RealSubject.class.getInterfaces(), handler);  
  10.         subject.request();  
  11.         System.out.println(subject.getClass());  
  12.     }  
  13. }  
subject.getClass()打印出来的结果是class $Proxy0,这是java动态生成的一个类,相当于代理角色。



对于spring的aop的应用和动态代理及其相似。
我们新建一个接口
[java] view plain copy
  1. public interface Person  
  2. {  
  3.     void info();  
  4. }  
为该接口添加一个实现类
[java] view plain copy
  1. public class PersonImpl implements Person  
  2. {  
  3.     private String name;  
  4.     private String age;  
  5.       
  6.     public String getName()  
  7.     {  
  8.         return name;  
  9.     }  
  10.   
  11.     public void setName(String name)  
  12.     {  
  13.         this.name = name;  
  14.     }  
  15.   
  16.     public String getAge()  
  17.     {  
  18.         return age;  
  19.     }  
  20.   
  21.     public void setAge(String age)  
  22.     {  
  23.         this.age = age;  
  24.     }  
  25.   
  26.     public void info()  
  27.     {  
  28.         System.out.println("name = " + name + " " + "age = " + age);  
  29.     }  
  30. }  

接下来我们创建拦截器,对方法的调用进行拦截

实现AfterReturningAdvice接口,实现其中的afterReturning()方法,该方法会在请求的方法调用之后执行
[java] view plain copy
  1. import java.lang.reflect.Method;  
  2.   
  3. import org.springframework.aop.AfterReturningAdvice;  
  4.   
  5. public class MethodAfter implements AfterReturningAdvice  
  6. {  
  7.     public void afterReturning(Object arg0, Method arg1, Object[] arg2,  
  8.             Object arg3) throws Throwable  
  9.     {  
  10.         System.out.println("after method execute");  
  11.     }  
  12. }  
下面方法实现了MethodBeforeAdvice方法,并实现其中的before方法,该方法会在请求的方法被调用之前执行
[java] view plain copy
  1. import java.lang.reflect.Method;  
  2.   
  3. import org.springframework.aop.MethodBeforeAdvice;  
  4.   
  5. public class MethodBefore implements MethodBeforeAdvice  
  6. {  
  7.   
  8.     public void before(Method method, Object[] args, Object target)  
  9.             throws Throwable  
  10.     {  
  11.         System.out.println("before call the method");  
  12.         System.out.println("methodName:" + method.getName() );  
  13.         System.out.println("args is:" + args);  
  14.         System.out.println("target is:" + target);  
  15.     }  
  16. }  
然后我们配置spring的配置文件如下:
[html] view plain copy
  1. <bean id="personTarger" class="com.PersonImpl">  
  2.     <property name="name">  
  3.         <value>microbingbing</value>  
  4.     </property>  
  5.     <property name="age">  
  6.         <value>23</value>  
  7.     </property>  
  8. </bean>  
  9. <bean id="myAdvice" class="com.MethodBefore"></bean>  
  10. <bean id="methodAfter" class="com.MethodAfter"></bean>  
  11. <bean id="person" class="org.springframework.aop.framework.ProxyFactoryBean">  
  12.     <property name="proxyInterfaces">  
  13.         <value>com.Person</value>  
  14.     </property>  
  15.     <property name="target">  
  16.         <ref local="personTarger"/>  
  17.     </property>  
  18.     <property name="interceptorNames">  
  19.         <list>  
  20.             <value>methodAfter</value>  
  21.             <value>myAdvice</value>  
  22.         </list>  
  23.     </property>  
  24. </bean>  
新建一个测试类:
[java] view plain copy
  1. import org.springframework.context.ApplicationContext;  
  2. import org.springframework.context.support.FileSystemXmlApplicationContext;  
  3.   
  4. public class BeanTest  
  5. {  
  6.     public static void main(String[] args)  
  7.     {  
  8.         ApplicationContext context = new FileSystemXmlApplicationContext("applicationContext.xml");  
  9.         Person p = (Person)context.getBean("person");  
  10.         p.info();  
  11.     }  
  12. }  
观察控制台输出结果:
before call the method
methodName:info
args is:[Ljava.lang.Object;@8acf6e
target is:com.PersonImpl@1112783
name = yangnianbing age = 24
after method execute

原创粉丝点击