假设我们有一个Person接口和它的实现类PersonImpl,我们希望每次调用Person方法的时候,都能够额外做其他一些操作。这时我们配置如下:
<beanid="personTarget"class="com.mycompany.PersonImpl"> <propertyname="name"value="Tony"/> <propertyname="age"value="51"/> </bean> <beanid="myAdvisor"class="com.mycompany.MyAdvisor"> <propertyname="someProperty"value="Custom string property value"/> </bean> <beanid="debugInterceptor"class="org.springframework.aop.interceptor.DebugInterceptor"> </bean> <bean id="person" class="org.springframework.aop.framework.ProxyFactoryBean"> <propertyname="proxyInterfaces"value="com.mycompany.Person"/> <propertyname="target"ref="personTarget"/> <propertyname="interceptorNames"> <list> <value>myAdvisor</value> <value>debugInterceptor</value> </list> </property> </bean>
以上配置的含义是:定义一个AOP,用myAdvisor和debugInterceptor去拦截personTarget的操作。对使用者而言,他们都只是拿到一个person(接口)类型的对象,并不知道它真正的实现类。它的真正使用类是经2个advisor代理过的PersonImpl。
在实际应用中,这种一个对象需要被多个advisor拦截的情况不多,倒是一个advisor拦截多个接口居多,比如数据库的事务。示例配置如下:
<beanclass="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <propertyname="interceptorNames"> <list> <value>transactionInterceptor</value> </list> </property> <propertyname="beanNames"> <list> <value>productOverService</value> <value>ticketOverService</value> </list> </property> <propertyname="proxyTargetClass" value="true"/> </bean>
本配置的意思是用transactionInterceptor去拦截productOverService和ticketOverService。
因为aop的原理一样,所以aop各元素之间的装配过程也类似。无论是哪种spring的配置,所不同的只是方便性而已。凡是spring的AOP,它的底层都是JDK动态代理(代理接口)或CGLIB(代理类)。以上配置中,ProxyFactoryBean实现FactoryBean接口,这是spring预定义的一个接口,所有实现该接口的bean。它在spring容器中存储的真正对象,并不是定义时候的那个类,比如这里的ProxyFactoryBean。而是它的getObject所返回的对象,也就是说初始化person这个bean的时候,它会调用它的getObject方法,spring在这个方法里创建AOP代理。对于第二种配置,BeanNameAutoProxyCreator实现了BeanPostProcessor接口,spring在这个接口里创建AOP代理
相关推荐
SpringAop xml方式配置通知
SpringAOP的注解配置完成切面的编程,完成execution,annotation两种表达式的实例Ddemo
使用Spring配置文件实现AOP
Spring AOP配置的实例,最基本的Spring AOP配置
SpringAop面向切面编程
spring aop spring aop spring aop spring aop spring aop spring aop spring aop spring aop spring aop
spring aop jar 包
spring-aop-1.1.1.jar spring-aop-1.2.6.jar spring-aop-1.2.9.jar spring-aop-2.0.2.jar spring-aop-2.0.6.jar spring-aop-2.0.7.jar spring-aop-2.0.8.jar spring-aop-2.0.jar spring-aop-2.5.1.jar spring-aop-...
springAOP配置实现动态代理,有利于熟悉动态代理原理,深入了解spring。
描述一下Spring AOP? 在Spring AOP中关注点(concern)和横切关注点(cross-cutting concern)有什么不同? AOP有哪些可用的实现? Spring中有哪些不同的通知类型(advice types)? Spring AOP 代理是什么? 引介...
一、适合人群 1、具备一定Java编程基础,初级开发者 2、对springboot,mybatis,mysql有基本认识 3、对spring aop认识模糊的,不清楚如何实现Java 自定义注解的 ...4、spring boot,mybatis,druid,spring aop的使用
基于注解实现SpringAop基于注解实现SpringAop基于注解实现SpringAop
Spring源码最难问题:当Spring AOP遇上循环依赖.docx
Spring之AOP配置文件详解.txt Spring之AOP配置文件详解.txt 帮助你更快张握Java的学习!
Spring AOP:通过配置管理特性,Spring AOP 模块直接将面向方面的编程功能集成到了 Spring 框架中。所以,可以很容易地使 Spring 框架管理的任何对象支持 AOP。Spring AOP 模块为基于 Spring 的应用程序中的对象提供...
spring aop的demo spring aop的demo
最近使用了springAOP编程,文档里面包含了springAOP的代码示例及详细注释说明,使用的是注解配置模式
Spring AOP配置事务方法,描述了spring的事务配置,方便开发应用和数据库的连接管理。
Spring AOP 几个不同使用方法的完整例子,使用Junit4c测试, 在我的博客上有不同配置组合的说明,可以参考
Spring框架的关键组件之一是面向方面编程(AOP)框架。 面向方面的编程需要将程序逻辑分解成不同的部分。 此教程将通过简单实用的方法来学习Spring框架提供的AOP/面向方面编程。