springboot中的5種通知小例子
阿新 • • 發佈:2018-09-29
int [] work service round 代理 rop -a lis springboot中的5種通知的小例子
1.環境搭建
- pom中導入
<!--增加AOP需要的包--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
application.properties文件中配置 #Spring AOP spring.aop.auto=true spring.aop.proxy-target-class=true
類上面增加@Aspect標註
2.切入點表達式
/** * 定義一個方法, 用於聲明切入點表達式. 一般地, 該方法中再不需要添入其他的代碼. * 使用 @Pointcut 來聲明切入點表達式. * 後面的其他通知直接使用方法名來引用當前的切入點表達式. * (..)表示任意參數 */ @Pointcut("execution(public int com.jztey.omronhealth.service.ArithmeticCalculator.*(..))") public void declareJointPointExpression() { }
3.前置通知
//聲明該方法執行之前執行,前置通知 //直接導入切面點,上面的 @Before("declareJointPointExpression()") public void beforeMethod(JoinPoint joinPoint) { String methodName = joinPoint.getSignature().getName(); Object[] args = joinPoint.getArgs(); System.out.println("這是切面開始打印出來的--->The method " + methodName + " begins with " + Arrays.asList(args)); }
4.後置通知
//後置通知:在目標方法執行後(無論是否發生異常),執行通知
//在後置通知中還不能訪問目標方法的執行的結果,不是在執行方法後調用的
/**
* 這是切面開始打印出來的--->The method add begins with [3, 5]
* 這是切面結束打印出來的--->The method add ends
* 和--->8
*/
@After("declareJointPointExpression()")
public void afterMethod(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName();
System.out.println("這是切面結束打印出來的--->The method " + methodName + " ends");
}
5.帶有返回值通知
/**
* 帶有返回值的切面
* 在方法法正常結束受執行的代碼
* 返回通知是可以訪問到方法的返回值的!
* 可以使用returning = "result"進行獲取後得到
*/
@AfterReturning(value = "declareJointPointExpression()",
returning = "result")
public void afterReturning(JoinPoint joinPoint, Object result) {
String methodName = joinPoint.getSignature().getName();
System.out.println("The method " + methodName + " ends with " + result);
}
6.異常通知
/**
* 異常處理切面
* 在目標方法出現異常時會執行的代碼.
* 可以訪問到異常對象; 且可以指定在出現特定異常時在執行通知代碼
*/
@AfterThrowing(value = "declareJointPointExpression()",
throwing = "e")
public void afterThrowing(JoinPoint joinPoint, Exception e) {
String methodName = joinPoint.getSignature().getName();
System.out.println("The method " + methodName + " occurs excetion:" + e);
}
7.環繞通知
/**
* 環繞切面,一般用的不是很多,類似於動態代理,可以包含前面4種的任意個
* 環繞通知需要攜帶 ProceedingJoinPoint 類型的參數.
* 環繞通知類似於動態代理的全過程: ProceedingJoinPoint 類型的參數可以決定是否執行目標方法.
* 且環繞通知必須有返回值, 返回值即為目標方法的返回值
*/
/*
@Around("execution(public int com.atguigu.spring.aop.ArithmeticCalculator.*(..))")
public Object aroundMethod(ProceedingJoinPoint pjd){
Object result = null;
String methodName = pjd.getSignature().getName();
try {
//前置通知
System.out.println("The method " + methodName + " begins with " + Arrays.asList(pjd.getArgs()));
//執行目標方法
result = pjd.proceed();
//返回通知
System.out.println("The method " + methodName + " ends with " + result);
} catch (Throwable e) {
//異常通知
System.out.println("The method " + methodName + " occurs exception:" + e);
throw new RuntimeException(e);
}
//後置通知
System.out.println("The method " + methodName + " ends");
return result;
}
*/
優先級
/**
* 可以使用 @Order 註解指定切面的優先級, 值越小優先級越高
*/
@Order(2)
完整的切面例子
//通過添加 @Aspect 註解聲明一個 bean 是一個切面!
/**
* 可以使用 @Order 註解指定切面的優先級, 值越小優先級越高
*/
@Order(2)
@Aspect
@Component
@Slf4j
public class LoggingAspect {
/**
* 定義一個方法, 用於聲明切入點表達式. 一般地, 該方法中再不需要添入其他的代碼.
* 使用 @Pointcut 來聲明切入點表達式.
* 後面的其他通知直接使用方法名來引用當前的切入點表達式.
* (..)表示任意參數
*/
@Pointcut("execution(public int com.jztey.omronhealth.service.ArithmeticCalculator.*(..))")
public void declareJointPointExpression() {
}
//聲明該方法執行之前執行,前置通知
//直接導入切面點,上面的
@Before("declareJointPointExpression()")
public void beforeMethod(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName();
Object[] args = joinPoint.getArgs();
System.out.println("這是切面開始打印出來的--->The method " + methodName + " begins with " + Arrays.asList(args));
}
//後置通知:在目標方法執行後(無論是否發生異常),執行通知
//在後置通知中還不能訪問目標方法的執行的結果,不是在執行方法後調用的
/**
* 這是切面開始打印出來的--->The method add begins with [3, 5]
* 這是切面結束打印出來的--->The method add ends
* 和--->8
*/
@After("declareJointPointExpression()")
public void afterMethod(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName();
System.out.println("這是切面結束打印出來的--->The method " + methodName + " ends");
}
/**
* 帶有返回值的切面
* 在方法法正常結束受執行的代碼
* 返回通知是可以訪問到方法的返回值的!
* 可以使用returning = "result"進行獲取後得到
*/
@AfterReturning(value = "declareJointPointExpression()",
returning = "result")
public void afterReturning(JoinPoint joinPoint, Object result) {
String methodName = joinPoint.getSignature().getName();
System.out.println("The method " + methodName + " ends with " + result);
}
/**
* 異常處理切面
* 在目標方法出現異常時會執行的代碼.
* 可以訪問到異常對象; 且可以指定在出現特定異常時在執行通知代碼
*/
@AfterThrowing(value = "declareJointPointExpression()",
throwing = "e")
public void afterThrowing(JoinPoint joinPoint, Exception e) {
String methodName = joinPoint.getSignature().getName();
System.out.println("The method " + methodName + " occurs excetion:" + e);
}
/**
* 環繞切面,一般用的不是很多,類似於動態代理,可以包含前面4種的任意個
* 環繞通知需要攜帶 ProceedingJoinPoint 類型的參數.
* 環繞通知類似於動態代理的全過程: ProceedingJoinPoint 類型的參數可以決定是否執行目標方法.
* 且環繞通知必須有返回值, 返回值即為目標方法的返回值
*/
/*
@Around("execution(public int com.atguigu.spring.aop.ArithmeticCalculator.*(..))")
public Object aroundMethod(ProceedingJoinPoint pjd){
Object result = null;
String methodName = pjd.getSignature().getName();
try {
//前置通知
System.out.println("The method " + methodName + " begins with " + Arrays.asList(pjd.getArgs()));
//執行目標方法
result = pjd.proceed();
//返回通知
System.out.println("The method " + methodName + " ends with " + result);
} catch (Throwable e) {
//異常通知
System.out.println("The method " + methodName + " occurs exception:" + e);
throw new RuntimeException(e);
}
//後置通知
System.out.println("The method " + methodName + " ends");
return result;
}
*/
}
springboot中的5種通知小例子