Spring-Aop
一、Aop的概述
1.1、什么是Aop(摘自百度)
在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方
式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个
热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑
的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高
了开发的效率
1.2、Aop的相关术语
所有的切入点都是连接点,但不是所有的连接点都是切入点。
-
Joinpoint(连接点):指被拦截到的点。在spring中,这些点指service层接口中的方法。
-
Pointcut(切入点):指我们要对哪些Joinpoint进行拦截的定义(有些连接点被指定不被拦截,那就不是切入点。大概就是要被通知的方法)。
-
Advice(通知/增强):拦截到Joinpoint之后所要做的事情就是通知。
-
在通知代码中,我们总会执行method.invoke()方法(调用切入点方法),所以我们根据通知语句的位置,分为前置通知(invoke方法前的语句),后置通知(invoke方法后的语句),异常通知(catch语句中),最终通知(finally语句中),环绕通知(整体)
-
Target(目标对象):被代理对象,这里就是service层的对象
-
Weaving(织入): 是指把增强应用到目标对象来创建新的代理对象的过程。
-
Proxy(代理):一个类被AOP织入增强后,就产生一个结果代理类。
-
Aspect(切面):是切入点和通知(引介)的结合。(把通知配置到切入点就是切面)
二、基于XML的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: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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--spring中基于XML的AOP配置步骤-->
<!--1、准备工作,配置spring 的约束-->
<!--2、把通知Bean也交给spring来管理-->
<!--2.1、配置目标对象-->
<bean id="userService" class="spring.demo1.UserServiceImpl"></bean>
<!--2.2、配置通知对象-->
<bean id="myAdvice" class="spring.demo1.MyAdvice"></bean>
<!--3、使用aop:config标签表明开始AOP的配置-->
<aop:config>
<!--4、编写切入点
切入点表达式的写法:
关键字:execution(表达式)
表达式:
访问修饰符 返回值 包名.包名.包名...类名.方法名(参数列表)
标准的表达式写法:
public void spring.demo1.UserServiceImpl.save()
访问修饰符可以省略
void spring.demo1.UserServiceImpl.save()
返回值可以使用通配符,表示任意返回值
* spring.demo1.UserServiceImpl.save()
包名可以使用通配符,表示任意包。但是有几级包,就需要写几个*.
* *.*.UserServiceImpl.save()
包名可以使用..表示当前包及其子包
* *..UserServiceImpl.save()
类名和方法名都可以使用*来实现通配
* *..*.*()
参数列表:
可以直接写数据类型:
基本类型直接写名称 int
引用类型写包名.类名的方式 java.lang.String
可以使用通配符表示任意类型,但是必须有参数
可以使用..表示有无参数均可,有参数可以是任意类型
全通配写法:
* *..*.*(..)
实际开发中切入点表达式的通常写法:
切到业务层实现类下的所有方法
* com.xxw.service.impl.*.*(..)-->
<aop:pointcut id="pointcut1" expression="execution(public void spring.demo1.UserServiceImpl.save())"/>
<aop:pointcut id="pointcut2" expression="execution(* spring.demo1.UserServiceImpl.delete())"/>
<aop:pointcut id="pointcut3" expression="execution(* *.*.UserServiceImpl.update())"/>
<aop:pointcut id="pointcut4" expression="execution(* *..UserServiceImpl.find(..))"/>
<aop:aspect ref="myAdvice">
<!--前置通知-->
<aop:before method="before" pointcut-ref="pointcut1"></aop:before>
<!--后置通知-->
<aop:after-returning method="afterReturning" pointcut-ref="pointcut2" returning="result"></aop:after-returning>
<!--环绕通知-->
<aop:around method="around" pointcut-ref="pointcut3" ></aop:around>
<!--异常通知-->
<aop:after-throwing method="afterThrowing" pointcut-ref="pointcut4" throwing="ex"></aop:after-throwing>
<!--最终通知-->
<aop:after method="after" pointcut-ref="pointcut4"></aop:after>
</aop:aspect>
</aop:config>
</beans>
三、基于注解的AOP配置
@Aspect
public class MyAdvice2 {
//前置通知
@Before(value="execution(* spring.demo2.CustomerServiceImpl.save(..))")
public void before(JoinPoint joinPoint){
System.out.println("这是前置通知"+joinPoint);
}
// 后置通知:
@AfterReturning(value="MyAdvice2.pointcut2()",returning="result")
public void afterReturning(Object result){
System.out.println("后置增强==========="+result);
}
// 环绕通知:
@Around(value="MyAdvice2.pointcut3()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable{
System.out.println("环绕前增强==========");
Object obj = joinPoint.proceed();
System.out.println("环绕后增强==========");
return obj;
}
// 异常抛出通知:
@AfterThrowing(value="MyAdvice2.pointcut4()",throwing="e")
public void afterThrowing(Throwable e){
System.out.println("异常抛出增强========="+e.getMessage());
}
// 最终通知
@After(value="MyAdvice2.pointcut4()")
public void after(){
System.out.println("最终增强============");
}
// 切入点注解:
@Pointcut(value="execution(* spring.demo2.CustomerServiceImpl.delete(..))")
private void pointcut2(){}
@Pointcut(value="execution(* spring.demo2.CustomerServiceImpl.update(..))")
private void pointcut3(){}
@Pointcut(value="execution(* spring.demo2.CustomerServiceImpl.find(..))")
private void pointcut4(){}
}
项目链接: spring_Aop.