網(wǎng)站首頁 編程語言 正文
1. Spring
中的AOP
需要手動(dòng)開啟
????????在Spring中,如果我們采用注解的方式進(jìn)行AOP
,則需要手動(dòng)開啟Spring
的AOP
支持,如下例子:
① 定義Spring的配置類,主要聲明需要掃描的包路徑,并且打開AOP
功能
@Configuration
@ComponentScan("com.single")
@EnableAspectJAutoProxy
public class SpringConfig {
}
????????@EnableAspectJAutoProxy
該注解即為打開AOP
的注解,我們也可以通過該注解選擇動(dòng)態(tài)代理的方式,默認(rèn)情況下,Spring
采用JDK
的動(dòng)態(tài)代理,我們可以點(diǎn)進(jìn)該注解看下,請重點(diǎn)看下第一個(gè)屬性的注釋:
@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}.
* 翻譯過來就是:指示是否創(chuàng)建基于子類(CGLIB)的代理,而不是標(biāo)準(zhǔn)的基于Java接口的代理。默認(rèn)值為{@code false}
* 說白了就是默認(rèn)是false,采用JDK,如果你想用Cglib,那你就設(shè)置為true
*/
boolean proxyTargetClass() default false;
boolean exposeProxy() default false;
}
②自定義aop
注解
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface AspectAnnotation {
}
③編寫切面類、切點(diǎn)、以及增強(qiáng)
@Component
@Aspect
public class AspectTest {
@Pointcut("@annotation(com.single.annotation.AspectAnnotation)")
public void pointCut() {}
@Before("pointCut()")
public void before(JoinPoint point) {
System.out.println("前置增強(qiáng)");
}
@After("pointCut()")
public void after() {
System.out.println("后置增強(qiáng)");
}
}
④在需要被增強(qiáng)的方法上添加自定義注解,方法所在bean
一定要交給Spring
管理
@Service
public class TestService {
public TestService() {
System.out.println("TestService is created...");
}
@AspectAnnotation
public void test() {
System.out.println("init......");
}
}
⑤啟動(dòng)容器并調(diào)用方法
public class Application {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
TestService bean = ctx.getBean(TestService.class);
bean.test();
}
}
⑥結(jié)果
2. SpringBoot
中的AOP
默認(rèn)開啟,并采用Cglib
代理方式
????????在SpringBoot
中,AOP
的使用與上述沒有區(qū)別,只不過不需要我們手動(dòng)開啟AOP
功能了,主要是因?yàn)?code>SpringBoot的自動(dòng)裝配,如果你讀過SpringBoot
的源碼,相比你一定會(huì)知道在spring-boot-autoconfigure
的META-INF
下有一個(gè)spring.factories
文件,它里面指明了很多需要自動(dòng)裝配的配置類的路徑,在啟動(dòng)的時(shí)候會(huì)自動(dòng)將這些配置類中定義的bean
裝配到IOC
中,原理不多說了,感興趣的可以去研究一下。
????????我們來看一下這個(gè)文件
????????可以看到自動(dòng)配置中有一個(gè)AOP
的配置類,找到它,源碼如下:
可以看到第53行和61行都用了@EnableAspectJAutoProxy注解
,但是這個(gè)配置生效的前提條件是由@ConditionalOnProperty
注解來控制的,此處還是涉及到了SpringBoot
自動(dòng)裝配的原理了,因?yàn)樗亲詣?dòng)裝配,那么外部組件那么多,如果都要自動(dòng)裝配,那豈不是要加載很多無用的bean
嗎,所以它肯定設(shè)定的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
的默認(rèn)值為true
,結(jié)合@ConditionalOnProperty
注解里的參數(shù),可以知道JDK
的動(dòng)態(tài)代理配置不生效,因?yàn)樗募虞d條件是false
,所以SpringBoot
在啟動(dòng)的時(shí)候會(huì)自動(dòng)開啟AOP
功能并采用Cglib
方式進(jìn)行動(dòng)態(tài)代理
原文鏈接:https://blog.csdn.net/qq_41563912/article/details/125896748
相關(guān)推薦
- 2022-04-28 SpringBoot?整合mongoDB并自定義連接池的示例代碼_MongoDB
- 2022-10-18 NumPy對數(shù)組按索引查詢實(shí)戰(zhàn)方法總結(jié)_python
- 2023-05-22 解決Python報(bào)錯(cuò):ValueError:operands?could?not?be?broadc
- 2024-03-05 git查看用戶信息命令
- 2022-05-02 Python?私有屬性與私有方法_python
- 2023-01-05 Kotlin?協(xié)程與掛起函數(shù)及suspend關(guān)鍵字深入理解_Android
- 2022-04-03 Python中的tkinter庫簡單案例詳解_python
- 2023-05-26 C++?static的作用解讀_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支