Spring中AOP注解@Aspect的使用詳解
AOP思想
AOP(Aspect Oriented Programming)是一種面向切面的編程思想。面向切面編程是將程序抽象成各個切面,即解剖對象的內部,將那些影響了多個類的公共行為抽取到一個可重用模塊里,減少系統(tǒng)的重復代碼,降低模塊間的耦合度,增強代碼的可操作性和可維護性。AOP把軟件系統(tǒng)分為兩個部分:核心關注點和橫切關注點。業(yè)務處理的主要流程是核心關注點,與之關系不大的部分是橫切關注點。橫切關注點的一個特點是,他們經常發(fā)生在核心關注點的多處,而各處都基本相似。比如權限認證、日志、事務處理、增強處理。
AOP的使用場景
權限認證、日志、事務處理、增強處理
@Aspect的使用以及基本概念
1.切面類 @Aspect: 定義切面類,加上@Aspect、@Component注解
@Aspect @Component //設置注解執(zhí)行的順序 @Order(2) public class AnnotationAspectTest
2.切點 @Pointcut
/**
* 定義切點,切點為對應controller
*/
@Pointcut("execution(public * com.example.zcs.Aop.controller.*.*(..))")
public void aopPointCut(){
}注:execution表達式第一個*表示匹配任意的方法返回值,第二個*表示所有controller包下的類,第三個*表示所有方法,第一個..表示任意參數(shù)個數(shù)。
3.Advice,在切入點上執(zhí)行的增強處理,主要有五個注解:
@Before 在切點方法之前執(zhí)行
@After 在切點方法之后執(zhí)行
@AfterReturning 切點方法返回后執(zhí)行
@AfterThrowing 切點方法拋異常執(zhí)行
@Around 屬于環(huán)繞增強,能控制切點執(zhí)行前,執(zhí)行后
4.JoinPoint :方法中的參數(shù)JoinPoint為連接點對象,它可以獲取當前切入的方法的參數(shù)、代理類等信息,因此可以記錄一些信息,驗證一些信息等;
5.使用&&、||、!、三種運算符來組合切點表達式,表示與或非的關系;
6.@annotation(annotationType) 匹配指定注解為切入點的方法;
具體代碼實現(xiàn)
1.AopController,用于校驗aop是否生效:
@Controller
@RequestMapping("aop")
public class AopController {
@RequestMapping("test")
@ResponseBody
public String aopTest(User user) {
// System.out.println(user);
System.out.println("aop測試");
return "success";
}
@TestAnnotation(flag = false)
@RequestMapping("aopAnnotationTest")
@ResponseBody
public String aopAnnotationTest(User user) {
// System.out.println(user);
System.out.println("aopAnnotationTest");
return "success";
}
}2.AspectTest,具體的切面類,用于添加橫切邏輯,切點使用execution表達式進行匹配
@Aspect
@Component
//設置注解執(zhí)行的順序
@Order(1)
public class AspectTest {
/**
* 定義切點,切點為對應controller
*/
@Pointcut("execution(public * com.example.zcs.Aop.controller.*.*(..))")
public void aopPointCut(){
}
@Before("aopPointCut()")
public void testbefor(JoinPoint joinPoint) {
illegalParam(joinPoint);
System.out.println("執(zhí)行方法之前執(zhí)行。。。。。");
}
@After("aopPointCut()")
public void testAfter(JoinPoint joinPoint) {
//illegalParam(joinPoint);
System.out.println("執(zhí)行方法之后執(zhí)行。。。。。");
}
/**
*獲取請求參數(shù)
* @param joinPoint
* @return
*/
private static void illegalParam(JoinPoint joinPoint) {
if(joinPoint == null){
return;
}
boolean flag = false;
try{
// 參數(shù)值
Object[] args = joinPoint.getArgs();
if (args != null) {
for (Object o : args) {
System.out.println(o);
}
}
}catch(Exception e){
}
}
}3.AnnotationAspectTest類,具體的切面類,用于添加橫切邏輯,切點指定注解
@Aspect
@Component
//設置注解執(zhí)行的順序
@Order(2)
public class AnnotationAspectTest {
/**
* 定義切點,切點為添加了注解的方法
*/
@Pointcut("@annotation(com.example.zcs.Aop.annotation.TestAnnotation)")
public void aopPointCut(){
}
@Around("aopPointCut()")
public Object Around(ProceedingJoinPoint point) throws Throwable {
System.out.println("AnnotationAspectTest Around start ");
//獲取注解和注解的值
TestAnnotation annotation = getAnnotation(point);
if (annotation != null) {
boolean flag = annotation.flag();
System.out.println("注解flags的值:" + flag);
}
//獲取參數(shù)
Object[] args = point.getArgs();
for (Object arg : args) {
System.out.println("arg ==>" + arg);
}
//去調用被攔截的方法
Object proceed = point.proceed();
return proceed;
}
//獲取注解
public TestAnnotation getAnnotation(ProceedingJoinPoint point) {
Signature signature = point.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
Method method = methodSignature.getMethod();
if (method != null){
return method.getAnnotation(TestAnnotation.class);
}
return null;
}
}4.注解類TestAnnotation
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface TestAnnotation {
boolean flag() default true;
}到此這篇關于Spring中AOP注解@Aspect的使用詳解的文章就介紹到這了,更多相關Spring的@Aspect注解內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Spring Boot 中嵌入式 Servlet 容器自動配置原理解析
這篇文章主要介紹了Spring Boot 中嵌入式 Servlet 容器自動配置原理解析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
maven的settings.xml、pom.xml配置文件使用詳解
本文詳解了Maven中的配置文件settings.xml和pom.xml,闡述了它們的作用、配置項以及優(yōu)先級順序,settings.xml存在于Maven安裝目錄和用戶目錄下,分別作用于全局和當前用戶,pom.xml位于項目根路徑下2024-09-09
關于Controller層和Service層的類報錯問題及解決方案
這篇文章主要介紹了關于Controller層和Service層的類報錯問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02
Spring?Boot?Security認證之Redis緩存用戶信息詳解
本文介紹了如何使用Spring Boot Security進行認證,并通過Redis緩存用戶信息以提高系統(tǒng)性能,通過配置RedisUserDetailsManager,我們成功地將用戶信息存儲到了Redis中,并在Spring Security中進行了集成,需要的朋友可以參考下2024-01-01

