一、AOP的核心概念
1、连接点(JoinPoint): 程序执行的某一个特定位置,即类某一个方法调用前/调用后,方法抛出异 常后,一个类或一段程序代码拥有一些具有边界性质的特定点。Spring仅支持方法连接点。
2、切点(Pointcut): 连接点的集合。
3、增强(Advice(通知)): 切点处要执行的代码。
4、目标对象(target): 增强逻辑的织入目标类。
5、引介(Introduction):一种特殊的增强,它为类添加一些属性和方法.这样,即使一个业务类原本 没有实现某一个接口,通过AOP的引介功能,也可以动态地为该业务类添加接口的实现逻辑. 让业务类成为这个接口的实现类。
6、织入(Weaving):织入是将增强添加到目标类具体链接点上的过程,即通知执行到切点的过程。
7、代理(proxy): 增强后的代理类。
8、切面(Aspect): 由增强(或通知)和切点共同组成。
二、AOP的五种通知
1、前置通知:目标方法运行之前调用。
2、后置通知(如果出现异常不会调用):在目标方法运行之后调用。
3、环绕通知:在目标方法之前和之后都调用。
4、异常拦截通知:如果出现异常,就会调用。
5、返回通知(无论是否出现 异常都会调用):在目标方法运行之后调用。
三、基于注解实现SpringAOP的具体配置
1、在pom.xml中加入如下代码:
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>com.springsource.org.aspectj.weaver</artifactId>
<version>1.6.8.RELEASE</version>
</dependency>
2、在resource下创建beans.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"
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">
<context:component-scan base-package="com.qianfeng.aop06" />
<aop:aspectj-autoproxy />
</beans>
其中context:component-scan标签指组件扫描
base-package是指定要扫描的包的路径
<aop:aspectj-autoproxy />标签实现自动代理
3、创建一个接口类和实现类:
package com.qianfeng.aop06;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
@Component("us")
public class UserServiceImpl implements IUserService {
@Override
public List<Object> getAllUser() {
System.out.println("--------getAllUser----------");
return new ArrayList<>();
}
@Override
public boolean saveUser(Object user) {
System.out.println("--------saveUser----------");
return true;
}
@Override
public boolean deleteUser(int uid) {
System.out.println("--------deleteUser----------");
return false;
}
@Override
public boolean updateUser(Object obj) {
System.out.println("--------updateUser----------");
return true;
}
@Override
public void getUserByUid() {
System.out.println("--------getUserByUid----------");
System.out.println(1 / 0);
String str = null;
System.out.println(str.length());
}
}
package com.qianfeng.aop06;
import java.util.List;
public interface IUserService {
/**
* 获取所有的用户对象列表
* @return
*/
List<Object> getAllUser();
/**
* 保存用户
* @param user
* @return
*/
boolean saveUser(Object user);
/**
* 根据用户uid删除该uid对应的用户信息
* @param uid
* @return
*/
boolean deleteUser(int uid);
/**
* 更新指定用户信息
* @param obj
* @return
*/
boolean updateUser(Object obj);
void getUserByUid();
}
4、以注解的方式实现切面类MyAspect
(1)代码主体前加的注解:
@Component注解标注当前类是一个组件,在扫描时会被扫描进来
@Aspect标注当前类是一个切面类
(2)
@Pointcut(value = "execution(* com.qianfeng.aop06.*.*(..))")
public void setAll(){}
@Pointcut 注解为了避免相同的匹配规则被定义多处,专门定义该方法设置执行的匹配规则,各个自行调用即可
(3)
@Before("setAll()")
public void myBefore(JoinPoint jp){
System.out.println("this is before.");
}
@Before 表示该方法为一个前置通知
(4)
@After("setAll()")
public void myAfter(JoinPoint jp){
System.out.println("this is after.");
}
@After 表示该方法为一个后置通知
(5)
@Around("setAll()")
public Object myAround(ProceedingJoinPoint pjp){
Object obj = null;
try {
System.out.println("this is around before");
obj = pjp.proceed();
System.out.println("this is around after");
} catch (Throwable throwable) {
throwable.printStackTrace();
}
return obj;
}
@Around 表示该方法为一个环绕通知
@ pjp 处理连接点
@return 返回每个业务方法的返回值
(6)
@AfterReturning(value = "setAll()", returning = "obj")
public void myReturn(JoinPoint jp, Object obj){
System.out.println("this is after returnning " + obj);
}
@AfterReturning 表示该方法为一个带有返回值的通知
(7)
@AfterThrowing(value = "setAll()", throwing = "e")
public void myThrowing(JoinPoint jp, Throwable e){
System.out.println("this is after throwing " + e.getMessage());
}
@AfterThrowing 表示该方法为一个带有异常的通知
(8)总体代码如下:
@Component // 标注当前类为一个组件
@Aspect // 标注当前类为一个切面类
public class MyAspect {
/**
* @Pointcut 注解为了避免相同的匹配规则被定义多处,专门定义该方法设置执行的匹配规则,各个自行调用即可
* write once, only once
*/
@Pointcut(value = "execution(* com.qianfeng.aop06.*.*(..))")
public void setAll(){}
/**
* @Before 表示该方法为一个前置通知
* @param jp 连接点
*/
@Before("setAll()")
public void myBefore(JoinPoint jp){
System.out.println("this is before.");
}
/**
* @After 表示该方法为一个后置通知
* @param jp 连接点
*/
@After("setAll()")
public void myAfter(JoinPoint jp){
System.out.println("this is after.");
}
/**
* @Around 表示该方法为一个环绕通知
* @param pjp 处理连接点
* @return 返回每个业务方法的返回值
*/
@Around("setAll()")
public Object myAround(ProceedingJoinPoint pjp){
Object obj = null;
try {
System.out.println("this is around before");
obj = pjp.proceed();
System.out.println("this is around after");
} catch (Throwable throwable) {
throwable.printStackTrace();
}
return obj;
}
/**
* @AfterReturning 表示该方法为一个带有返回值的通知
* @param jp 连接点
* @param obj 业务方法的返回值
*/
@AfterReturning(value = "setAll()", returning = "obj")
public void myReturn(JoinPoint jp, Object obj){
System.out.println("this is after returnning " + obj);
}
/**
* @AfterThrowing 表示该方法为一个带有异常的通知
* @param jp 连接点
* @param e Throwable对象
*/
@AfterThrowing(value = "setAll()", throwing = "e")
public void myThrowing(JoinPoint jp, Throwable e){
System.out.println("this is after throwing " + e.getMessage());
}
注解方式的执行顺序为:(after throwing) around before>before > 业务方法 > around-after > after >after returning