AspectJ是一個面向切面的框架,它擴展了Java語言。AspectJ定義了AOP語法,因此它有一個專門的編譯器用來生成遵照Java字節編碼規範的Class文件。java
SpringBoot中AOP的使用方式主要有兩種:註解式攔截與方法規則攔截,具體使用以下文所示。spring
1、建立一個簡單springboot 2.03項目,添加aop依賴數據庫
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
此依賴已包含AspectJ相關依賴包。編程
2、編寫攔截規則的註解springboot
package com.cenobitor.aop.annotation; import java.lang.annotation.*; @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Action { String name(); }
註解說明:元註解是指註解的註解,包括@Retention @Target @Document @Inherited四種。框架
1.@Retention: 定義註解的保留策略ide
首先要明確生命週期長度 SOURCE < CLASS < RUNTIME ,因此前者能做用的地方後者必定也能做用。通常若是須要在運行時去動態獲取註解信息,那隻能用 RUNTIME 註解;若是要在編譯時進行一些預處理操做,好比生成一些輔助代碼(如 ButterKnife),就用 CLASS註解;若是隻是作一些檢查性的操做,好比 @Override 和 @SuppressWarnings,則可選用 SOURCE 註解。函數
2.@Target:定義註解的做用目標
源碼爲:spring-boot
@Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.ANNOTATION_TYPE) public @interface Target { ElementType[] value(); }
3.@Document:說明該註解將被包含在javadoc中
4.@Inherited:說明子類能夠繼承父類中的該註解編碼
3、編寫使用註解的被攔截類
package com.cenobitor.aop.service; import com.cenobitor.aop.annotation.Action; import org.springframework.stereotype.Service; @Service public class DemoAnnotationService { @Action(name = "註解式攔截的add操做") public void add(){} }
4、編寫使用方法規則被攔截類
package com.cenobitor.aop.service; import org.springframework.stereotype.Service; @Service public class DemoMethodService { public void add(){} }
5、編寫切面
package com.cenobitor.aop.aspect; import com.cenobitor.aop.annotation.Action; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.stereotype.Component; import java.lang.reflect.Method; @Aspect @Component public class LogAspect { @Pointcut("@annotation(com.cenobitor.aop.annotation.Action)") public void annotationPoinCut(){} @After("annotationPoinCut()") public void after(JoinPoint joinPoint){ MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); Action action = method.getAnnotation(Action.class); System.out.println("註解式攔截 "+action.name()); } @Before("execution(* com.cenobitor.aop.service.DemoMethodService.*(..))") public void before(JoinPoint joinPoint){ MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); System.out.println("方法規則式攔截,"+method.getName()); } }
AOP註解說明:
6、運行
public class main { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AopApplication.class); DemoAnnotationService demoAnnotationService = context.getBean(DemoAnnotationService.class); DemoMethodService demoMethodService = context.getBean(DemoMethodService.class); demoAnnotationService.add(); demoMethodService.add(); } }
AopApplication.class爲本項目的啓動類。
運行結果以下:
註解式攔截 註解式攔截的add操做
方法規則式攔截,add
注:摘抄自《JavaEE開發的顛覆者SpringBoot 實戰》,根據springboot 2.0.3作些許修改,省略一些配置項。