spring框架中的aop技术

1. 什么是AOP, 面向切面编程
  AOP为Aspect Oriented Programming的缩写, 意为:面向切面编程,主要是使各部分之间的耦合度降低, 提高程序的可重用性, 同时提高了开发的效率。
2. AOP的作用及优势
  作用:
  在程序运行期间,不修改源码对已有方法进行增强(加上统一的逻辑处理)。
  优势:
  减少重复代码、提高开发效率、维护方便
3. AOP相关术语
  Joinpoint(连接点) :就是根据规则,可以指定拦截的方法,我们将每一个被拦截的方法称为连接点
  Pointcut(切入点) : 所谓切入点就是拦截方法设置的规则
  Advice(通知/增强,场景:在之前拦截,还是方法执行后拦截呢?)
  通知就是可以设置在方法之前拦截或者方法执行之后拦截或者方法出异常后拦截,或者方法之前和之后都拦截。我们将这些拦截场景称为通知
4. 基于xml配置aop
4.1.第一步加入aop需要的包
Spring的AOP包基于AspectJ框架,所以必须加入AspectJ-->aspectjweaver.jar
 
4.2编写一个切面类
--切面类的作用,用于编写统一处理的逻辑代码。
 
package com.sxt.aop;
 
import org.aspectj.lang.ProceedingJoinPoint;
public class Aop {
public void before() {
System.out.println("-before-方法执行之前-");
}
public void after() {
System.out.println("-after-方法执行之后-");
}
//around需要一个ProceedingJoinPoint对象指定方法调用之前后之后
public void around(ProceedingJoinPoint point) {
System.out.println("-around-在方法之前后执行-");
try {
//调用拦截方法的逻辑代码
point.proceed();
} catch (Throwable e) {
e.printStackTrace();
}
System.out.println("-around-在方法之后执行-");
}
public void afterThrowing() {
System.out.println("-afterThrowing-出了异常后执行");
}
public void afterReturning() {
System.out.println("-afterReturning-方法执行成功之后-");
}
}
 
4.3、配置AOP配置
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
<bean name="now" class="java.util.Date"></bean>
<bean name="studentService" class="com.sxt.service.impl.SutdentServiceImpl">
</bean>
 
<!-- aop配置 -->
<!-- 创建AOP的对象到容器 -->
<bean name="aop" class="com.sxt.aop.Aop"></bean>
<!-- aop:config: 用于设置AOP拦截的跟标签 -->
<aop:config >
<!--aop:aspect标签 指定AOP的切面,以及拦截规则对应的方法 -->
<aop:aspect id="aop-aspect" ref="aop">
<!-- 拦截的方法必须不出异常,才在方法执行之后加入切面逻辑代码 -->
<aop:after-returning method="afterReturning" pointcut="execution(* com.sxt.service.impl.SutdentServiceImpl.*())"/>
<!-- 拦截的方法必须出了异常,才在方法执行之后加入切面逻辑代码 -->
<aop:after-throwing method="afterThrowing" pointcut="execution(* com.sxt.service.impl.SutdentServiceImpl.*())"/>
<!-- 拦截的方法不管有没有异常,可以实现在拦截方法之前和之后分别加入不同的逻辑代码 -->
<aop:around method="around" pointcut="execution(* com.sxt.service.impl.SutdentServiceImpl.*())" />
<!-- 拦截的方法不管有没有异常,可以实现在拦截方法之前加入切面的逻辑代码 -->
<aop:before method="before" pointcut="execution(* com.sxt.service.impl.SutdentServiceImpl.*())"/>
<!-- 拦截的方法不管有没有异常,可以实现在拦截方法之后加入切面的逻辑代码 -->
<aop:after method="after" pointcut="execution(* com.sxt.service.impl.SutdentServiceImpl.*())"/>
</aop:aspect>
</aop:config>
</beans>
 
4.4. 切入点表达式说明
execution:
匹配方法的执行(常用)
execution(表达式)
表达式语法:execution([修饰符] 返回值类型 包名.类名.方法名(参数))
写法说明:
全匹配方式:
public void cn.gzsxt.service.impl.CustomerServiceImpl.saveCustomer()
访问修饰符可以省略
void cn.gzsxt.service.impl.CustomerServiceImpl.saveCustomer()
返回值可以使用*号,表示任意返回值
* cn.gzsxt.service.impl.CustomerServiceImpl.saveCustomer()
包名可以使用*号,表示任意包,但是有几级包,需要写几个*
* *.*.*.*.CustomerServiceImpl.saveCustomer()
使用..来表示当前包,及其子包
* com..CustomerServiceImpl.saveCustomer()
类名可以使用*号,表示任意类
* com..*.saveCustomer()
方法名可以使用*号,表示任意方法
* com..*.*()
参数列表可以使用*,表示参数可以是任意数据类型,但是必须有参数
* com..*.*(*)
参数列表可以使用..表示有无参数均可,有参数可以是任意类型
* com..*.*(..)
全通配方式:
* *..*.*(..)
*:通配的是任何的字符串
 
5. 常用标签
5.1. <aop:config
作用:
用于声明开始aop的配置
5.2. <aop:aspect>
作用:
用于配置切面。
属性:
id:给切面提供一个唯一标识。
ref:引用配置好的通知类bean的id。
5.3. <aop:pointcut>
作用:
用于配置切入点表达式
属性:
expression:用于定义切入点表达式。
id:用于给切入点表达式提供一个唯一标识。
5.4. <aop:before>
作用:
用于配置前置通知
属性:
method:指定通知中方法的名称。
pointct:定义切入点表达式
pointcut-ref:指定切入点表达式的引用
5.5. <aop:after-returning>
作用:
用于配置后置通知,如果出了异常就一定不会调用切面的方法
属性:
method:指定通知中方法的名称。
pointct:定义切入点表达式
pointcut-ref:指定切入点表达式的引用
5.6. <aop:after-throwing>
作用:
用于配置异常通知,只有出了异常才会调用切面对应的方法
属性:
method:指定通知中方法的名称。
pointct:定义切入点表达式
pointcut-ref:指定切入点表达式的引用
5.7. <aop:after>
作用:
用于配置最终通知,不管出不出异常,调用的切面的方法
属性:
method:指定通知中方法的名称。
pointct:定义切入点表达式
pointcut-ref:指定切入点表达式的引用
5.8. <aop:around>
作用:
用于配置环绕通知
属性:
method:指定通知中方法的名称。
pointct:定义切入点表达式
pointcut-ref:指定切入点表达式的引用

猜你喜欢

转载自www.cnblogs.com/Virtual-field/p/10724091.html
今日推荐