SpringBoot AOP AspectJ切面技術(shù)介紹與實(shí)現(xiàn)方式
AspectJ簡介
- 它不屬于spring;
- AspectJ是一個AOP的框架;
- 定義了AOP語法;
- 有一個專門的編譯器用來生成遵守Java字節(jié)編碼規(guī)范的Class文件
什么是AspectJ
- AspectJ是使用面向切面的一個框架
- 它擴(kuò)展了Java語言(它本身也是一種語言)
- 支持原生Java代碼 有自己的編譯器
- 將代碼翻譯成Java字節(jié)碼文件
- 是為了方便編寫AOP代碼而出現(xiàn)的
- 使用AOP編程的三個重點(diǎn) 通知 切點(diǎn) 織入
實(shí)現(xiàn)AOP的方式
原生使用切面
添加AOP坐標(biāo)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>創(chuàng)建增強(qiáng)類MyAOP
對service層下的所有類的所有方法進(jìn)行增強(qiáng)
@Component
@Aspect
public class MyAOP {
//定義切點(diǎn)
@Pointcut("execution(* com.moming.service.*.*(..))")
public void point(){}
@Before("point()")
public void before(){
System.out.println("===>前置通知");
}
@After("point()")
public void after(){
System.out.println("===>后置通知");
}
@Around("point()")
public Object arround(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("===>環(huán)繞前");
Object resules = pjp.proceed();
System.out.println("===>環(huán)繞后");
return resules;
}
@AfterReturning(value = "point()",returning = "ret")
public void returning(JoinPoint jp, String ret){
Object[] args = jp.getArgs();
System.out.println("返回后通知獲取參數(shù): "+Arrays.toString(args));
System.out.println("===>返回后通知,返回值: "+ret);
}
@AfterThrowing("point()")
public void throwing(){
System.out.println("===>異常通知");
}
}service/OrderService
@Service
public class OrderService {
public String order(int id){
System.out.println("===>目標(biāo)方法:訂單業(yè)務(wù)ID:"+id);
return "001202210121010";
}
}啟動類測試
@SpringBootApplication
public class App2 {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(App2.class, args);
OrderService bean = context.getBean(OrderService.class);
System.out.println(bean.order(1));
}
}無異常時

有異常時,后續(xù)代碼就不再執(zhí)行了

補(bǔ)充配置說明
//兩種占位符
//* 代表的是一個單詞,b* 代表的是以b開頭的單詞。 例如 bds
//.. 通配符 ,代表的是0個或者多個匹配項(xiàng)
通過注解使用切面
聲明注解NeedCut
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE,ElementType.METHOD})
public @interface NeedCut {
}切換注解
@Component
@Aspect
public class MyAOP {
//定義切點(diǎn)
@Pointcut("@annotation(com.moming.annotation.NeedCut)")
public void point(){}
@Before("point()")
public void before(){
System.out.println("===>前置通知");
}
@After("point()")
public void after(){
System.out.println("===>后置通知");
}
@Around("point()")
public Object arround(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("===>環(huán)繞前");
Object resules = pjp.proceed();
System.out.println("===>環(huán)繞后");
return resules;
}
@AfterReturning(value = "point()",returning = "ret")
public void returning(JoinPoint jp, String ret){
Object[] args = jp.getArgs();
System.out.println("返回后通知獲取參數(shù): "+Arrays.toString(args));
System.out.println("===>返回后通知,返回值: "+ret);
}
@AfterThrowing("point()")
public void throwing(){
System.out.println("===>異常通知");
}
}使用注解@NeedCut
@Service
public class OrderService {
@NeedCut
public String order(int id){
System.out.println("===>目標(biāo)方法:訂單業(yè)務(wù)ID:"+id);
return "001202210121010";
}
public void add(){
System.out.println("===>添加訂單");
}
}測試
@SpringBootApplication
public class App2 {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(App2.class, args);
OrderService bean = context.getBean(OrderService.class);
System.out.println(bean.order(01));
System.out.println("-------------------");
bean.add();
}
}使用@NeedCut注解的方法才進(jìn)行增強(qiáng)

到此這篇關(guān)于SpringBoot AOP AspectJ切面技術(shù)介紹與實(shí)現(xiàn)方式的文章就介紹到這了,更多相關(guān)SpringBoot AOP AspectJ切面內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java 多線程Synchronized和Lock的區(qū)別
這篇文章主要介紹了Java 多線程Synchronized和Lock的區(qū)別,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下2021-01-01
Java中內(nèi)存異常StackOverflowError與OutOfMemoryError詳解
這篇文章主要介紹了 Java中內(nèi)存異常StackOverflowError與OutOfMemoryError詳解的相關(guān)資料,需要的朋友可以參考下2017-03-03
MyBatis實(shí)現(xiàn)萬能Map和模糊查詢
本文主要介紹了MyBatis實(shí)現(xiàn)萬能Map和模糊查詢,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07
mybatis實(shí)現(xiàn)mapper配置并查詢數(shù)據(jù)的思路詳解
這篇文章主要介紹了mybatis實(shí)現(xiàn)mapper配置并查詢數(shù)據(jù),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04
Spring Boot中的@ConfigurationProperties注解解讀
在SpringBoot框架中,@ConfigurationProperties注解是處理外部配置的強(qiáng)大工具,它允許開發(fā)者將配置文件中的屬性自動映射到Java類的字段上,實(shí)現(xiàn)配置的集中管理和類型安全,通過定義配置類并指定前綴,可以將配置文件中的屬性綁定到Java對象2024-10-10
spring中@Configuration和@Bean注解的用法
這篇文章主要介紹了spring中@Configuration和@Bean注解的用法,@Configuration用于定義配置類,可替換xml配置文件,被注解的類內(nèi)部包含有一個或多個被@Bean注解的方法,需要的朋友可以參考下2023-05-05
Spring Boot+Jpa多數(shù)據(jù)源配置的完整步驟
這篇文章主要給大家介紹了關(guān)于Spring Boot+Jpa多數(shù)據(jù)源配置的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01

