首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件开发 >

Spring框架的设计理念与设计方式(8)-代理模式

2012-09-15 
Spring框架的设计理念与设计模式(8)-代理模式关键词:Spring,设计模式,工厂模式,代理模式上一章:Spring框架

Spring框架的设计理念与设计模式(8)-代理模式
关键词:Spring,设计模式,工厂模式,代理模式
上一章:Spring框架的设计理念与设计模式(7)-Spring中AOP特性与实现    http://javapub.iteye.com/blog/751642
下一章:Spring框架的设计理念与设计模式(9)-策略模式                 http://javapub.iteye.com/blog/751653

Spring中使用的设计模式也很多,比如工厂模式、单例模式、模版模式等,在《Webx框架的系统架构与设计模式》、《Tomcat的系统架构与模式设计分析》已经有介绍,这里就不赘述了。这里主要介 绍代理模式和策略模式。

代理模式

代理模式原理

代理模式就是给某一个对象创建一个代理对象,而由这个代理对象控制对原对象的引用,而创建这个代理对象就是可以在调用原对象是可以增加一些额外的操作。下面是代理模式的结构:

Subject:抽象主题,它是代理对象的真实对象要实现的接口,当然这可以是多个接口组成。

ProxySubject:代理类除了实现抽象主题定义的接口外,还必须持有所代理对象的引用

RealSubject:被代理的类,是目标对象。

Spring中如何实现代理模式

Spring Aop中Jdk动态代理就是利用代理模式技术实现的。在Spring中除了实现被代理对象的接口外,还会有 org.springframework.aop.SpringProxy和 org.springframework.aop.framework.Advised 两个接口。Spring中使用代理模式的结构图如下:

$Proxy就是创建的代理对象,而Subject是抽象主题,代理对象是通过InvocationHandler来持有对目标对象的引用的。
Spring中一个真实的代理对象结构如下:

清单10代理对象$Proxy4

public class $Proxy4 extends java.lang.reflect.Proxy implements       org.springframework.aop.framework.PrototypeTargetTests$TestBean           org.springframework.aop.SpringProxy           org.springframework.aop.framework.Advised   {       java.lang.reflect.Method m16;       java.lang.reflect.Method m9;       java.lang.reflect.Method m25;       java.lang.reflect.Method m5;       java.lang.reflect.Method m2;       java.lang.reflect.Method m23;       java.lang.reflect.Method m18;       java.lang.reflect.Method m26;       java.lang.reflect.Method m6;       java.lang.reflect.Method m28;       java.lang.reflect.Method m14;       java.lang.reflect.Method m12;       java.lang.reflect.Method m27;       java.lang.reflect.Method m11;       java.lang.reflect.Method m22;       java.lang.reflect.Method m3;       java.lang.reflect.Method m8;       java.lang.reflect.Method m4;       java.lang.reflect.Method m19;       java.lang.reflect.Method m7;       java.lang.reflect.Method m15;       java.lang.reflect.Method m20;       java.lang.reflect.Method m10;       java.lang.reflect.Method m1;       java.lang.reflect.Method m17;       java.lang.reflect.Method m21;       java.lang.reflect.Method m0;       java.lang.reflect.Method m13;       java.lang.reflect.Method m24;           int hashCode();       int indexOf(org.springframework.aop.Advisor);       int indexOf(org.aopalliance.aop.Advice);       boolean equals(java.lang.Object);       java.lang.String toString();       void sayhello();       void doSomething();       void doSomething2();       java.lang.Class getProxiedInterfaces();       java.lang.Class getTargetClass();       boolean isProxyTargetClass();       org.springframework.aop.Advisor; getAdvisors();       void addAdvisor(int, org.springframework.aop.Advisor)                  throws org.springframework.aop.framework.AopConfigException;       void addAdvisor(org.springframework.aop.Advisor)                  throws org.springframework.aop.framework.AopConfigException;       void setTargetSource(org.springframework.aop.TargetSource);       org.springframework.aop.TargetSource getTargetSource();       void setPreFiltered(boolean);       boolean isPreFiltered();       boolean isInterfaceProxied(java.lang.Class);       boolean removeAdvisor(org.springframework.aop.Advisor);       void removeAdvisor(int)throws org.springframework.aop.framework.AopConfigException;       boolean replaceAdvisor(org.springframework.aop.Advisor,                  org.springframework.aop.Advisor)                  throws org.springframework.aop.framework.AopConfigException;       void addAdvice(org.aopalliance.aop.Advice)                  throws org.springframework.aop.framework.AopConfigException;       void addAdvice(int, org.aopalliance.aop.Advice)                  throws org.springframework.aop.framework.AopConfigException;       boolean removeAdvice(org.aopalliance.aop.Advice);       java.lang.String toProxyConfigString();       boolean isFrozen();       void setExposeProxy(boolean);       boolean isExposeProxy();   } 


目录--Spring框架的设计理念与设计模式
Spring框架的设计理念与设计模式(1)-Spring的架构             http://javapub.iteye.com/blog/751539
Spring框架的设计理念与设计模式(2)-Spring的设计理念         http://javapub.iteye.com/blog/751545
Spring框架的设计理念与设计模式(3)-Bean组件                 http://javapub.iteye.com/blog/751550
Spring框架的设计理念与设计模式(4)-Context组件              http://javapub.iteye.com/blog/751625
Spring框架的设计理念与设计模式(5)-Core组件                 http://javapub.iteye.com/blog/751627
Spring框架的设计理念与设计模式(6)-Ioc容器及BeanFactory工厂 http://javapub.iteye.com/blog/751635
Spring框架的设计理念与设计模式(7)-Spring中AOP特性与实现    http://javapub.iteye.com/blog/751642
Spring框架的设计理念与设计模式(8)-代理模式                 http://javapub.iteye.com/blog/751652
Spring框架的设计理念与设计模式(9)-策略模式                 http://javapub.iteye.com/blog/751653

热点排行