一文帶你了解Spring中@Enable開頭注解的使用
@Enable 注解
首先我們先看一下有哪些常用的 @Enable
開頭的注解,以及都是干什么用的。
@EnableRetry
:開啟Spring
的重試功能;@EnableScheduling
:開啟Spring
的定時功能;@EnableAsync
:開啟Spring
的異步功能;@EnableAutoConfiguration
:開啟Spring
的自動裝配功能;
上面這幾個是我們經(jīng)常會用到和看到的,都知道在使用相應的功能的時候,如果沒有配置上面的注解功能都是不生效的。以我們前面的文章的 Spring
重試為例,我們需要在啟動類上面配置 @EnableRetry
,否則自動重試注解 @Retryable
是不會生效的,如下所示,沒看過的可以去看下,Java 遠程調(diào)用失???如何優(yōu)雅的進行重試?。
@Import 注解
那有的小伙伴就要問了,這個 @EnableRetry
注解到底有什么作用呢?不用這個注解就沒辦法了嗎?
要知道這個注解有什么功效,我們可以點開看看源碼,代碼如下
package?org.springframework.retry.annotation; import?java.lang.annotation.Documented; import?java.lang.annotation.ElementType; import?java.lang.annotation.Retention; import?java.lang.annotation.RetentionPolicy; import?java.lang.annotation.Target; import?org.springframework.context.annotation.EnableAspectJAutoProxy; import?org.springframework.context.annotation.Import; @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @EnableAspectJAutoProxy(proxyTargetClass?=?false) @Import(RetryConfiguration.class) @Documented public?@interface?EnableRetry?{ ?boolean?proxyTargetClass()?default?false; }
可以看到源碼很簡單,其中最有用的就一行 @Import(RetryConfiguration.class)
,我們可以嘗試把這一行代碼放到啟動類上面看看效果,如下所示,可以看到項目可以正常啟動,并且也還是有效果的,說明跟我們的 @EnableRetry
注解是一樣的。
從上面的實驗效果我們可以看到 @EnableRetry
注解其實就是對 @Import(RetryConfiguration.class)
的一個封裝,同樣的通過源碼我們還可以看到 @EnableScheduling
注解就是對 @Import({SchedulingConfiguration.class})
的一個封裝。
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Import({SchedulingConfiguration.class}) @Documented public?@interface?EnableScheduling?{ }
那如果在沒有 @Enablexxx
注解的時候,我們直接通過 @Import
注解是可以這樣寫的,在一個 @Import
注解里面包含多個配置類,不過這種在配置類較多的場景下還是相對不夠簡潔的,因而才有了各自功能對應的 @Enable
注解。
package?com.example.demo; import?org.springframework.boot.SpringApplication; import?org.springframework.boot.autoconfigure.SpringBootApplication; import?org.springframework.context.annotation.ComponentScan; import?org.springframework.context.annotation.Import; import?org.springframework.retry.annotation.RetryConfiguration; import?org.springframework.scheduling.annotation.SchedulingConfiguration; @SpringBootApplication @ComponentScan(value?=?"com.example.demo.*") @Import({RetryConfiguration.class,?SchedulingConfiguration.class}) public?class?DemoApplication?{ ??public?static?void?main(String[]?args)?{ ????SpringApplication.run(DemoApplication.class,?args); ??} }
為什么要使用 @Import 注解呢
那么很多的小伙伴又要問了,為啥要通過使用 @Import
注解將配置類加載進來呢?在項目中的 Spring
上下文中不是能直接獲取到嗎?為此我們來實驗一下,通過下面的代碼我們看下是否能在 Spring
的容器中獲取到 RetryConfiguration
的 Bean
package?com.example.demo; import?org.springframework.boot.SpringApplication; import?org.springframework.boot.autoconfigure.SpringBootApplication; import?org.springframework.context.ConfigurableApplicationContext; import?org.springframework.context.annotation.ComponentScan; import?org.springframework.context.annotation.Import; import?org.springframework.retry.annotation.RetryConfiguration; import?org.springframework.scheduling.annotation.SchedulingConfiguration; @SpringBootApplication @ComponentScan(value?=?"com.example.demo.*") //@Import({RetryConfiguration.class,?SchedulingConfiguration.class}) public?class?DemoApplication?{ ??public?static?void?main(String[]?args)?{ ????ConfigurableApplicationContext?applicationContext?=?SpringApplication.run(DemoApplication.class,?args); ????Object?bean?=?applicationContext.getBean("org.springframework.retry.annotation.RetryConfiguration"); ????System.out.println(bean.toString()); ??} }
啟動過后我們可以看到結果如下,提示我們在容器中找不到這個 bean
,有點小伙伴會說是不是 bean
的名字寫錯了,其實并不是,緊接著我們再把注釋的那一行放開再運行一下。
可以看到,這次我們成功的獲取到了這個 Bean
,這個實驗就是告訴我們,其實在默認情況下,Spring
的容器中是找不到RetryConfiguration
這個 Bean
的,因此我們需要通過使用 @Import
注解,將該類加載到容器中。
那么為什么在容器中找不到這個 Bean
呢?
其實很簡單,因為這個 Bean
跟我們當前環(huán)境的類是不是同一個包里面的,在項目啟動的過程中并不會掃描到 RetryConfiguration
類所在的包,因此找不到是很正常的。
總結
上面通過 @EnableRetry
這個注解帶大家了解了一下 Spring
的 @Enable
開頭的注解的使用原理,相信大家對這些注解有了更深入的了解。簡單來說就是因為我們要使用的很多類并不在我們項目所在的包下面,我們不能將所有的依賴包都進行掃描,也不不方便將所有的配置類都通過 @Import
的方式進行導入,而是讓每個功能的項目包都提供一個 @Enable
開頭的注解,我們直接啟用注解就可以達到效果。
這種方式我們在平時的開發(fā)中也可以自己實現(xiàn),實現(xiàn)一個自己的 @Enable
開頭的注解來實現(xiàn)特定的功能。
到此這篇關于一文帶你了解Spring中@Enable開頭注解的使用的文章就介紹到這了,更多相關Spring @Enable注解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Flowable數(shù)據(jù)庫表分類及數(shù)據(jù)字典解析
這篇文章主要介紹了Flowable數(shù)據(jù)庫表分類及數(shù)據(jù)字典解析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11SpringMVC使用@Valid注解實現(xiàn)數(shù)據(jù)驗證的代碼示例
在 Web 開發(fā)中,數(shù)據(jù)驗證是一個非常重要的環(huán)節(jié),它可以確保數(shù)據(jù)的合法性和正確性,保護系統(tǒng)不受到惡意攻擊或用戶誤操作的影響,在 SpringMVC 中,我們可以使用 @Valid 注解來實現(xiàn)數(shù)據(jù)驗證,所以本文就給大家介紹具體的使用方法,需要的朋友可以參考下2023-07-07SpringSecurity數(shù)據(jù)庫進行認證和授權的使用
本文主要介紹了用戶的賬號、密碼以及角色信息在數(shù)據(jù)庫中的認證和授權,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08Spring security用戶URL權限FilterSecurityInterceptor使用解析
這篇文章主要介紹了Spring security用戶URL權限FilterSecurityInterceptor使用解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-12-12