1. AOP术语:
1. 连接点
类里面哪些方法可以被增强,这些方法称为连接点
2. 切入点
实际被真正增强的方法
3. 通知(增强)
实际增强的逻辑部分称为通知
4. 切面
把通知应用到切点过程
2.AOP操作
1. Spring框架一般都是基于AspectJ实现AOP操作
2. 基于xml配置文件实现
3. 基于注解方式实现
- 基于xml配置文件实现
1.1 导入依赖的包
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>
1.2 .配置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: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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
1.3 spring 中基于xml的AOP配置步骤
1.把通知Bean交给spring来管理
2.使用AOP:config表明开始AOP的配置
3.使用aop:aspect标签表明配置切面
id属性:是给切面提供一个唯一标识
reg属性:是给指定通知类bean的id
4.在aop:aspect标签内部使用对于标签来配置通知的类型
aop:before:表明配置是前置通知
method属性:用于指定切入点表达式,该表达式的含义指的是对业务层中哪些方法增强
poitcut属性:用于指定切入点表达式,该表达式的含义是对业务层涨哪些方法增强
切入点表达式的写法:
关键字:execution(表达式)
表达式:
访问修饰符 放回置 包名。包名。。。。类名。方法名(参数列表)
标准的表达式写法:
public void com.itheima.service.impl.AccountServiceImpl.saveAccount()
访问修饰符可以省略
返回值可以使用通配符,表示任意返回值
全通配写法:
* *..*.*(..)
<bean id="logger" class="com.itheima.utils.Logger"></bean>
<aop:config>
<aop:aspect id="logAdvice" ref="logger">
<aop:before method="printBeforeLog" pointcut="execution(* com.itheima.service.impl.*.*(..))"></aop:before>
<aop:after-returning method="printReturnLog" pointcut="execution(* com.itheima.service.impl.*.*(..))"></aop:after-returning>
<aop:after-throwing method="printExcepteLog" pointcut="execution(* com.itheima.service.impl.*.*(..))"></aop:after-throwing>
<aop:after method="printAfterLog" pointcut="execution(* com.itheima.service.impl.*.*(..))"></aop:after>
</aop:aspect>
</aop:config>
1.4 基于注解的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/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创建容器时要扫描的包 -->
<context:component-scan base-package="com.itheima"></context:component-scan>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
2. 配置增强类
2.1 用@Aspect注解表示切面
2.2 用@before配置前置通知
2.3 用@pointcut配置切入点
2.4 用@After配置最终通知
2.5 用@AfterReturn配置后置通知
2.6 用@AfterThrowing配置异常通知
2.7 用@Around配置环绕通知
形如:
@Component("logger")
@Aspect //表示当前类是一个切面类
public class Logger {
/**
* 用于打印日志,计划在切入点发法之前执行
*/
@Pointcut("execution(* com.itheima.service.impl.*.*(..))")
private void pt1(){}
@Before("pt1()")
public void printBeforeLog(){
System.out.println("Logger类中的printBeforeLog已经开始执行");
}
@After("pt1()")
public void printAfterLog(){
System.out.println("Logger类中的printAfterLog已经开始执行");
}
@AfterThrowing("pt1()")
public void printExcepteLog(){
System.out.println("Logger类中的printExcepteLog已经开始执行");
}
@AfterReturning("pt1()")
public void printReturnLog(){
System.out.println("Logger类中的printReturnLog已经开始执行");
}
// @Around("pt1()")
public Object aroundPrintLog(ProceedingJoinPoint pjp){
Object reValue = null;
try{
Object[] args = pjp.getArgs();
System.out.println("Logger类中的beforePrintlog");
reValue = pjp.proceed(args); //明确业务层调用方法(切入点方法)
System.out.println("Logger类中的returnPrintlog");
return reValue;
}catch(Throwable t){
System.out.println("Logger类中的throwingPrintlog");
throw new RuntimeException(t);
}finally{
System.out.println("Logger类中的afterPrintlog");
System.out.println("异常");
}
}
}