紧接着(三)中的例子。其实Spring AOP注解的概念理解了后,看XML配置就是件很简单的事情了。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="com.bjsxt"/>
<bean id="logInterceptor" class="com.bjsxt.aop.LogInterceptor"></bean>
<aop:config>
<aop:pointcut expression="execution(public * com.bjsxt.service..*.add(..))" id="logPointCut"/>
<aop:aspect id="logAspect" ref="logInterceptor">
<aop:before method="before" pointcut-ref="logPointCut" />
</aop:aspect>
</aop:config>
</beans>
(1) 首先去掉了<aop:aspectj-autoproxy />这句
(2) 还是得先初始化一个LogInteceptor对象,所以要先写一个<bean>
(3) AOP的配置写在<aop:config></aop:config>内
(4) 调用add()方法时,Spring发现符合pointcut("logPointCut"),然后这个poincut又被aspect("logAspect")引用到了,所以就去执行相应的切面逻辑
(5) 上面定义了一个全局的pointcut("logPointCut"),这意味着其他的aspect都可以通过id引用这个pointcut。其实也可以将pointcut写在aspect内,这样相当于一个私有pointcut,其他的aspect无法引用这个pointcut(没有id):
<bean id="logInterceptor" class="com.bjsxt.aop.LogInterceptor"></bean>
<aop:config>
<aop:aspect id="logAspect" ref="logInterceptor">
<aop:before method="before" pointcut="execution(public * com.bjsxt.service..*.add(..))" />
</aop:aspect>
</aop:config>
某些情况下,要使用别人的切面类(比如一个测量代码性能的工具,要把测量的逻辑织入你自己的代码),这时你不可能在别人的切面类代码上加注解,所以只有通过XML来配置。目前在实际使用中,XML的使用也是多于注解。
分享到:
相关推荐
Spring AOP 入门 作者:廖雪峰
NULL 博文链接:https://aoyi.iteye.com/blog/723571
演示了spring对aop的支持,包括注解方式、基于xml方式。
SpringAop xml方式配置通知
赠送jar包:spring-aop-5.0.10.RELEASE.jar; 赠送原API文档:spring-aop-5.0.10.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.0.10.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.0.10.RELEASE....
Spring AOP 代理一、两种代理方式1. JDK动态代理:其代理对象必须是某个接口的实现,它是通过在运行期间创建一个接口的实现类来完成对目标对象的代理。二
里面包括4个例子:(1)Spring实现AOP方式之一:基于XML配置的Spring AOP (2)Spring实现AOP方式之二:使用注解配置 Spring AOP (3)Spring AOP : AspectJ Pointcut 切点 (4)Spring AOP : Advice 声明 (通知注解)
Spring AOP 的实现例子(基于XML配置实现); 具体介绍看这里:https://blog.csdn.net/qq_22078107/article/details/85865543
Spring源码最难问题:当Spring AOP遇上循环依赖.docx
Spring基础:Spring AOP简单使用
使用Spring配置文件实现AOP
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-...
spring aop spring aop spring aop spring aop spring aop spring aop spring aop spring aop spring aop
基于xml代码的SpringAOP实例详解,适合初步学习springAOP的开发人员了解该功能的使用。
SpringAOP学习笔记以及四个可运行的Demo,涵盖经典代理模式、基于注解、基于xml配置这3方面的Demo
spring aop jar 包
Spring AOP面向方面编程原理:AOP概念,主要介绍面向对象的概念及原理,及作者的一些理解。
NULL 博文链接:https://czj4451.iteye.com/blog/2051461
SpringAop面向切面编程
配置可以通过xml文件来进行,大概有四种方式: 1. 配置ProxyFactoryBean,显式地设置advisors, advice, target等(基于代理的AOP ) 2. 配置AutoProxyCreator,这种方式下,还是如以前一样使用定义的bean,但是从...