AOP : 面向切面编程
-
Joinpoint(连接点)
所谓连接点是指那些被拦截到的点.在spring中,这些点指的是方法,因为spring只支持方法类型的连接点 -
Pointcut(切入点)
就是我们要增强的Joinpoint -
Advice(通知/增强)
所谓通知是指拦截到 Joinpoint 之后所要做的事情就是通知。
通知的类型:前置通知, 后置通知, 异常通知, 最终通知, 环绕通知。 -
Target(目标对象)
被代理增强的目标对象 -
Weaving(织入)
是指把增强应用到目标对象来创建代理对象的过程。
spring 采用动态代理织入,而 AspectJ 采用编译期织入和类装载期织入。 -
Aspect(切面)
是切入点和通知(引介)的结合。 -
Proxy(代理)
一个类被 AOP 织入增强后,就产生一个结果代理类。 -
Introduction(引介)
引介是一种特殊的通知在不修改类代码的前提下, Introduction 可以在运行期为类动态地添加一些方法或 Field。
AOP的XML文件标签配置
-
<aop:config>
标签表明开始aop的配置 -
<aop:aspect>
标签表明配置切面id属性: 是给切面一个唯一标示 ref属性: 是指定通知类的bean的ID
在标签内部使用对应标签来配置通知的类型
<aop:before>
: 表示配置前置通知
method属性: 用于指定<aop:aspect>标签ref属性类的方法
pointcut属性: 用于指定切入点表达式,指的是对业务层中哪些方法进行增强
切入点表达式的写法:
关键字 : execution(表达式)
标准写法 :
public.void.lorihen.service.AccountServiceImpl.saveAccount()
访问修饰符可以省略
void.lorihen.service.AccountServiceImpl.saveAccount()
返回值可以使用通配符,表示任意返回值
* com.itheima.service.impl.AccountServiceImpl.saveAccount()
包名可以使用通配符,表示任意包。但是有几级包,就需要写几个*.
* *.*.*.*.AccountServiceImpl.saveAccount())
包名可以使用 *…表示当前包及其子包
* *..AccountServiceImpl.saveAccount()
类名和方法名都可以使用*
来实现通配
* *..*.*()
参数列表:
可以直接写数据类型:
基本类型直接写名称 int
引用类型写包名.类名的方式 java.lang.String
可以使用通配符表示任意类型,但是必须有参数
可以使用…表示有无参数均可,有参数可以是任意类型
全通配写法:
* *..*.*(..)
实际开发中切入点表达式的通常写法:
切到业务层实现类下的所有方法:
* lorihen.service.impl.*.*(..)
例如:
<!--配置AOP-->
<aop:config>
<!--配置切面 -->
<aop:aspect id="logAdvice" ref="logger">
<!-- 配置通知的类型,并且建立通知方法和切入点方法的关联-->
<aop:pointcut id="pl" expression="execution(* lorihen.service.impl.*.*(..))"/>
<!--前置通知-->
<aop:before method="printLog" pointcut="execution(* lorihen.service.impl.*.*(..))"/>
<!--后置通知-->
<aop:after-returning method="afterPrintLog" pointcut="execution(void lorihen.*.impl.*.*(..))"/>
<!--异常通知-->
<aop:after-throwing method="afterThorwingPrintLog" pointcut-ref="pl"/>
<!--最终通知-->
<aop:after method="finalPrintLog" pointcut="execution(* *..*.*(..))"/>
</aop:aspect>
</aop:config>
注 : before比after优先权高,after和after-returning优先权次一级,after-throwing优先权最低,after和after-returning谁写在前面谁先执行.
AOP的注解配置
1、注解和bean.xml配置文件结合的方式
2、通过注解直接在Logger类上配置切点、切面
/**
* 用于记录日志的工具类,它里面提供了公共的代码
*/
@Component("logger")
@Aspect//表示当前类是一个切面类
public class Logger {
@Pointcut("execution(* com.itheima.service.impl.*.*(..))")
private void pt1(){}
/**
* 前置通知
*/
@Before("pt1()")
public void beforePrintLog(){
System.out.println("前置通知Logger类中的beforePrintLog方法开始记录日志了。。。");
}
/**
* 后置通知
*/
@AfterReturning("pt1()")
public void afterReturningPrintLog(){
System.out.println("后置通知Logger类中的afterReturningPrintLog方法开始记录日志了。。。");
}
/**
* 异常通知
*/
@AfterThrowing("pt1()")
public void afterThrowingPrintLog(){
System.out.println("异常通知Logger类中的afterThrowingPrintLog方法开始记录日志了。。。");
}
/**
* 最终通知
*/
@After("pt1()")
public void afterPrintLog(){
System.out.println("最终通知Logger类中的afterPrintLog方法开始记录日志了。。。");
}
注意:注解AOP,最终通知在后置通知之前执行,最终通知里面释放了连接,后置通知在提交事务会报异常
@Around("pt1()")
public Object aroundPringLog(ProceedingJoinPoint pjp){
Object rtValue = null;
try{
Object[] args = pjp.getArgs();//得到方法执行所需的参数
System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。前置");
rtValue = pjp.proceed(args);//明确调用业务层方法(切入点方法)
System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。后置");
return rtValue;
}catch (Throwable t){
System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。异常");
throw new RuntimeException(t);
}finally {
System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。最终");
}
}
}
环绕通知注意:
1、加上 ProceedingJoinPoint 类型的参数去通过 proceedingJoinPoint.proceed() 去调用目标方法,否则,目标方法都执行不了了
2、环绕通知方法一定要有返回值,返回值是 result = proceedingJoinPoint.proceed() 调用目标方法之后返回值
环绕通知注解的方法 , 如果没有返回值 , 则在所有Joinpoint (连接点) 中 只有无返回值的才能成为 Pointcut(切点) ,有返回值的则 无法成为 Pointcut(切点) ???
3、在bean.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置spring创建容器时要扫描的包-->
<context:component-scan base-package="com.itheima"/>
<!-- 配置spring开启注解AOP的支持 -->
<aop:aspectj-autoproxy/>
</beans>
另外:
<!-- proxy-target-class="true"使用cglib子类代理方式,返回代理对象是子类对象-->
<aop:aspectj-autoproxy proxy-target-class="true"/>
spring框架中,如果Target(目标对象)没有父接口,则自动调用由第三方cglib库提供的基于子类的动态代理,如果有父接口,则使用由JDK提供的基于接口的动态代理.
- 完全不使用xml配置文件的纯注解方式
@Configuration
@ComponentScan(basePackages="lorihen")
@EnableAspectJAutoProxy //开启注解AOP支持 等价于xml配置的<aop:aspectj-autoproxy/>
public class SpringConfiguration {
}