網站首頁 編程語言 正文
1. Spring
中的AOP
需要手動開啟
????????在Spring中,如果我們采用注解的方式進行AOP
,則需要手動開啟Spring
的AOP
支持,如下例子:
① 定義Spring的配置類,主要聲明需要掃描的包路徑,并且打開AOP
功能
@Configuration
@ComponentScan("com.single")
@EnableAspectJAutoProxy
public class SpringConfig {
}
????????@EnableAspectJAutoProxy
該注解即為打開AOP
的注解,我們也可以通過該注解選擇動態代理的方式,默認情況下,Spring
采用JDK
的動態代理,我們可以點進該注解看下,請重點看下第一個屬性的注釋:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(AspectJAutoProxyRegistrar.class)
public @interface EnableAspectJAutoProxy {
/**
* Indicate whether subclass-based (CGLIB) proxies are to be created as opposed
* to standard Java interface-based proxies. The default is {@code false}.
* 翻譯過來就是:指示是否創建基于子類(CGLIB)的代理,而不是標準的基于Java接口的代理。默認值為{@code false}
* 說白了就是默認是false,采用JDK,如果你想用Cglib,那你就設置為true
*/
boolean proxyTargetClass() default false;
boolean exposeProxy() default false;
}
②自定義aop
注解
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface AspectAnnotation {
}
③編寫切面類、切點、以及增強
@Component
@Aspect
public class AspectTest {
@Pointcut("@annotation(com.single.annotation.AspectAnnotation)")
public void pointCut() {}
@Before("pointCut()")
public void before(JoinPoint point) {
System.out.println("前置增強");
}
@After("pointCut()")
public void after() {
System.out.println("后置增強");
}
}
④在需要被增強的方法上添加自定義注解,方法所在bean
一定要交給Spring
管理
@Service
public class TestService {
public TestService() {
System.out.println("TestService is created...");
}
@AspectAnnotation
public void test() {
System.out.println("init......");
}
}
⑤啟動容器并調用方法
public class Application {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
TestService bean = ctx.getBean(TestService.class);
bean.test();
}
}
⑥結果
2. SpringBoot
中的AOP
默認開啟,并采用Cglib
代理方式
????????在SpringBoot
中,AOP
的使用與上述沒有區別,只不過不需要我們手動開啟AOP
功能了,主要是因為SpringBoot
的自動裝配,如果你讀過SpringBoot
的源碼,相比你一定會知道在spring-boot-autoconfigure
的META-INF
下有一個spring.factories
文件,它里面指明了很多需要自動裝配的配置類的路徑,在啟動的時候會自動將這些配置類中定義的bean
裝配到IOC
中,原理不多說了,感興趣的可以去研究一下。
????????我們來看一下這個文件
????????可以看到自動配置中有一個AOP
的配置類,找到它,源碼如下:
可以看到第53行和61行都用了@EnableAspectJAutoProxy注解
,但是這個配置生效的前提條件是由@ConditionalOnProperty
注解來控制的,此處還是涉及到了SpringBoot
自動裝配的原理了,因為它是自動裝配,那么外部組件那么多,如果都要自動裝配,那豈不是要加載很多無用的bean
嗎,所以它肯定設定的bean
被加載的條件,在META-INFO/spring-configuration-metadata.json
中我們可以找到如下信息
{
"name": "spring.aop.proxy-target-class",
"type": "java.lang.Boolean",
"description": "Whether subclass-based (CGLIB) proxies are to be created (true), as opposed to standard Java interface-based proxies (false).",
"defaultValue": true
},
上述說明就是spring.aop.proxy-target-class
的默認值為true
,結合@ConditionalOnProperty
注解里的參數,可以知道JDK
的動態代理配置不生效,因為它的加載條件是false
,所以SpringBoot
在啟動的時候會自動開啟AOP
功能并采用Cglib
方式進行動態代理
原文鏈接:https://blog.csdn.net/qq_41563912/article/details/125896748
相關推薦
- 2022-06-12 詳解QListWidget如何實現自定義Item效果_C 語言
- 2022-08-04 C#中Backgroundworker與Thread的區別_C#教程
- 2023-01-23 使用Docker部署Dashdot服務器儀表盤的步驟_docker
- 2022-06-07 SQL?Server內存機制詳解_MsSql
- 2023-04-11 一文帶你搞懂useCallback的使用方法_React
- 2022-03-12 使用docker部署grafana+prometheus配置_docker
- 2022-09-19 C/C++最短路徑算法之迪杰斯特拉Dijkstra的實現詳解_C 語言
- 2022-03-24 C++數組和指針的區別與聯系_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支