`

springframework【10】

阅读更多

Spring的Autoproxing (自动代理,注意必须使用ApplicaitonContext来获取bean才能进行自动代理,同时要将cglib.jar给加入到classpath中去)


自动代理可以不用为每一个目标对象那个手动定义代理对象,使用自动代理,您可以通过Bean的名称或者是Pointcut对比,自动为符合对比条件的目标对象建立代理对象。
1、BeanNameAutoProxyCreator
  如果要提供的Advice的目标对象很多,这个时候,我们一个个的建立代理对象那个会是件很麻烦的事情,为此我们可以给目标对象取适当的bean的名称,例如某些服务对象取名为***Service,这样我们就可以使用org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator自动创建代理。
  常见写法如下:
 配置文件的定义:

 

<bean id="beforeAdvice" class="com.itcast.advice.LogBeforeAdvice"></bean>
<bean id="afterAdvice" class="com.itcast.advice.LogAfterAdvice"></bean>
<bean id="helloAdvise" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
 <property name="mappedNames" >
 <list>
 <value>helloAaa</value>
 <value>helloBbb</value>
 </list>
 </property>
 <property name="advice" ref="beforeAdvice"></property>
</bean>
<bean id="beanNameAutoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
 <property name="beanNames">
 <list>
 <value>hello*</value>
 </list>
 </property>
 <property name="interceptorNames" value="helloAdvise"></property>
</bean>

 

上边配置文件的意思就是凡是以hello开头的bean的方法被调用的时候都要经过helloAdvise这个advisor的处理。在helloAdvise中,对于helloAaa,helloBbb方法进行了beforeAdvice的拦截。
2、DefaultAdvisorAutoProxyCreator
Spring提供的自动代理建立者还有
org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator,只要我们在定义文件中加上DefaultAdvisorAutoProxyCreator的设置,在Bean的定义文件被读取完成之后,它会自动搜寻所有的advisor,并自动将Advisor应用至符合Pointcuts的目标对象身上。这种方式就带有随机性了。是否被advisor命中完全看advisor对bean的方法名称的匹配了。相当于我定义了一个全局的advisor。applicationContext.xml中声明的所有的bean中的方法被调用的时候,我都看一下advisor中对方法的匹配是否符合,如果符合就将advisor中的advice给应用上,拦截方法的处理。
实际编码的时候如下:

 

<bean id="autoProxy" class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean>

 

 

 

 

分享到:
评论

相关推荐

    spring framework 中文参考手册

    spring framework 中文参考手册

    org.springframework.flex-1.0.3.RELEASE.jar.zip

    org.springframework.flex-1.0.3.RELEASE.jar.zip用于JAR包,org.springframework.flex-1.0.3.RELEASE.jar.zip用于JAR包org.springframework.flex-1.0.3.RELEASE.jar.zip用于JAR包org.springframework.flex-1.0.3....

    Spring Framework API文档

    Spring Framework API文档。Spring是什么呢?首先它是一个开源的项目,而且非常活跃;它是一个基于IOC和AOP的构架多层j2ee系统的框架,但它不强迫你必须在每一层中必须使用Spring,因为它模块化的很好,允许你根据...

    org.spring-framework-3.0.4. 所有jar

    org.springframework.aop-3.0.4.RELEASE.jar org.springframework.asm-3.0.4.RELEASE.jar org.springframework.aspects-3.0.4.RELEASE.jar org.springframework.beans-3.0.4.RELEASE.jar org.springframework....

    Getting started with Spring Framework: covers Spring 5(epub)

    Getting started with Spring Framework (4th Edition) is a hands-on guide to begin developing applications using Spring Framework 5. The examples (consisting of 88 sample projects) that accompany this ...

    spring-framework-2.0.2

    org\springframework\aop org\springframework\beans org\springframework\cache org\springframework\context org\springframework\core org\springframework\dao org\springframework\ejb org\spring...

    Spring Framework 5.1.0源码

    Spring Framework 5.1.0源码 The Spring Framework provides a comprehensive programming and configuration model for modern Java-based enterprise applications - on any kind of deployment platform. A key ...

    SpringFramework_v5.0.7.zip

    Spring Framework 是一个开源的Java/Java EE全功能栈(full-stack)的应用程序框架,以Apache许可证形式发布,也有.NET平台上的移植版本。 该框架基于 Expert One-on-One Java EE Design and Development(ISBN 0-...

    Spring Framework v5.0.11

    Spring Framework 是一个开源的Java/Java EE全功能栈(full-stack)的应用程序框架,以Apache许可证形式发布,也有.NET平台上的移植版本。 该框架基于 Expert One-on-One Java EE Design and Development(ISBN 0-...

    Spring Framework 4.x Reference Documentation 中文翻译

    Spring Framework 4.x Reference Documentation 中文翻译 Chinese translation of the Spring Framework 4.x Reference Documentation ...

    Spring Framework 4.0.2.RELEASE API

    Spring Framework 4.0.2.RELEASE API,该文档来自官网经本人制作而成方便使用的chm格式。

    org.springframework.core.jar

    org.springframework.core.jar

    Apress.Introducing.Spring.Framework.A.Primer.

    This book is an introduction to the well-known Spring Framework that offers an inversion of control container for the Java platform. The Spring Framework is an open source application framework that ...

    org.springframework.web.jar

    org.springframework.web.jar,没问题的,最新的。

    org.springframework.web的jar包.zip

    import org.springframework.web.socket.server.standard.ServerEndpointExporter;

    springframework2.5源代码

    springframework2.5源代码

    spring framework 简介

    spring framework 简介

    spring-mock.jar

    org.springframework.mock.jndi.ExpectedLookupTemplate.class org.springframework.mock.jndi.SimpleNamingContext.class org.springframework.mock.jndi.SimpleNamingContextBuilder.class org.springframework....

    spring framework离线文档

    spring framework4.1的英文离线文档和spring framework2.5中文文档

    springframework.zip

    springframework.zip

Global site tag (gtag.js) - Google Analytics