Spring框架前置增强,后置增强,环绕增强,异常增强,最终增强
前言
Spring 切点
什么是切点?切点(Pointcut),每个程序类都拥有多个连接点,如一个拥有两个方法的类,这两个方法都是连接点,即连接点是程序类中客观存在的事物。但在这为数从多的连接点中,如何定位到某个感兴趣的连接点上呢?AOP通过"切点"定位特定连接点。通过数据库查询的概念来理解切点和连接点的关系再适合不过了;连接点相当于数据库中的记录,而切点相当于查询条件. 在Spring中,切点通过org.springframework.aop.Pointcut接口进行描述,它使用类和方法作为连接点的查询条 件,Spring AOP的规则解析引擎负责解析切点所设定的查询条件,找到对应的连接点—其实确切地说,应该是执行点而非连接点,因为连接点是方法执行前、执行后等包 括方位信息的具体程序执行点,而切点只定位到某个方法上,所以如果希望定位到具体连接点上,还需要提供方位信息。
实现AOP的切面主要有以下几个要素
使用@Aspect注解将一个java类定义为切面
使用@Pointcut定义一个切入点,可以是一个规则表达式,比如下例中某个package下的所有函数,也可以是一个注解等
使用@Before在切入点开始处切入内
使用@After在切入点结尾处切入内
使用@AfterReturning在切入点return内容之后切入内容(可以用来对处理返回值做一些加工处理
使用@Around在切入点前后切入内容,并自己控制何时执行切入点自身的内
使用@AfterThrowing用来处理当切入内容部分抛出异常之后的处理逻辑
1.添加spring配置文件命名空间如下
xmlns:aop="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
讯享网
2.声明增强方法所在的Bean
3.配置切面
4.配置切面
5.引用包含增强方法的Bean
讯享网<?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:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"> <bean id="userDao" class="dao.Impl.UserDaoImpl"></bean> <bean id="userService" class="service.Impl.UserServiceImpl"> <property name="userDao" ref="userDao"></property> </bean <!-- 声明增强方法所在的Bean --> <!-- <bean id="errorLogger" class="aop.LoggerError"></bean>--> <!-- 配置切面 --> <aop:config> <!-- 定义切入点 --> <aop:pointcut id="pointcut" expression="execution(* service.*.*(..))" /> <!-- 定义切入点 --> <aop:aspect ref="errorLogger"> <!-- 将afterReturning()方法定义为后置增强并引用pointcut切入点 --> <!--after-returning 就是后置增强--> <!--returning能够将目标方法的返回值传到切面增强方法里--> <aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result"/> <!--将before()方法定义为前置增强并引用pointcut切入点--> <!--before就是前置增强--> <aop:before method="before" pointcut-ref="pointcut"/> <!-- 将after()方法定义为最终增强并引用pointcut切入点 --> <!-- 通过throwing属性指定为名为e的参数注入异常实例 --> <!--aop:after 就是最终增强的标签元素--> <aop:after method="after" pointcut-ref="pointcut"/> <!-- 将afterThrowing()方法定义为异常抛出增强并引用pointcut切入点 --> <!-- 通过throwing属性指定为名为e的参数注入异常实例 -- <!--after-throwing 就是异常抛出增强--> <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="e" /> </aop:aspect> </aop:config> </bean>
环绕增强配置
<!-- 配置切面 --> <aop:config> <!-- 定义切入点 --> <aop:pointcut id="pointcut" expression="execution(* service.*.*(..))" /> <!-- 织入环绕增强处理 --> <aop:aspect ref="aroundLogger"> <!--aop:around 为环绕增强的标签元素,引入到切入点 pointcut--> <aop:around method="aroundLogger" pointcut-ref="pointcut"/> </aop:aspect> </aop:config>
Demo:链接:
讯享网https://pan.baidu.com/s/1QbJPKCN59MoJCjjmEOQqxA 提取码:izgf

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/125020.html