Spring Enable***功能

 

spring博大精深,衍生出了兩大系列:spring boot 和sping cloud快速業務開發模式。

我們進行具體開發時,經常看到這樣以Enable*開頭的註解,如圖

 

等等好多例子。。。框架自帶的Enable*

@EnableAspectJAutoProxy 開啓對AspectJ自動代理的支持

@EnableAsync 開啓異步方法的支持

@EnableScheduling 開啓計劃任務的支持

@EnableWebMvc 開啓Web MVC的配置支持

@EnableConfigurationProperties 開啓對@ConfigurationProperties註解配置Bean的支持

@EnableJpaRepositories 開啓對SpringData JPA Repository的支持

@EnableTransactionManagement 開啓註解式事務的支持

@EnableCaching 開啓註解式的緩存支持

看到不少網文吹噓的上天了、神乎其威,我只說一句話:它(指Enable*)只是一種註解,可參考我的博文:

Spring中@Import的三種情況  https://blog.csdn.net/dong19891210/article/details/106346447  和

Spring Bean註冊的幾種方式https://blog.csdn.net/dong19891210/article/details/105798650

註冊bean時的條件選擇多,導致邏輯判斷多

 

只要是在spring生態裏搞開發,請務必搞清楚Spring Bean的生命週期過程和Bean的幾種不同註冊方式

 

根據各家公司的業務場景需求,也可以自己擴寫Enable功能,如

 

在項目啓動類上添加自定義註解:@EnablePrintService

 

可以把Enable*註解當成一個套路,實現方式其實是一樣的,不管有多少個應用場景(@EnableScheduling、@EnableEurekaServer、@EnableDubbo、@EnablePrintService等等)。

既然是註解,那就

Spring Core Annotations:https://www.javacodegeeks.com/2019/05/spring-core-annotations.html

Spring Annotations [Quick Reference]  https://javabeat.net/spring-annotations/

 

Spring纔是核心,spring boot、spring cloud、第三方擴展都是Spring的衍生品!!!

 

重要的是說三遍: 抓住核心看本源!!!

 

參考:

0 . Annotation Type EnableAutoConfiguration

https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/autoconfigure/EnableAutoConfiguration.html

1. Scheduling Tasks  https://spring.io/guides/gs/scheduling-tasks/

2. Spring Job Scheduling with @Scheduled & @EnableScheduling Annotations  http://websystique.com/spring/spring-job-scheduling-with-scheduled-enablescheduling-annotations/

3.  @EnableScheduling in Spring   https://stackoverflow.com/questions/41377002/enablescheduling-in-spring

4. Spring Annotations: Spring Cloud  https://stackabuse.com/spring-annotations-spring-cloud/

5. Spring - BeanDefinitionRegistry  https://anthony-dong.gitee.io/post/spring-beandefinitionregistry/

6. Grails - Java-Based Configuration 

https://blog.andresteingress.com/2014/02/14/grails-java-based-spring-config.html

7.  Spring Enable annotation – writing a custom Enable annotation 

https://www.javacodegeeks.com/2015/04/spring-enable-annotation-writing-a-custom-enable-annotation.html

8. A Cron Expressions  https://docs.oracle.com/cd/E12058_01/doc/doc.1014/e12030/cron_expressions.htm

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章