日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring Enable annotation – writing a custom Enable annotation

發布時間:2025/4/5 javascript 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Enable annotation – writing a custom Enable annotation 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

Spring provides a range of annotations with names starting with?Enable*, these annotations in essence enable certain Spring managed features to be activated. One good example of such an annotation is?EnableWebMvcwhich brings in all the beans needed to support a MVC flow in Spring based applications. Another good example is the?EnableAsync?annotation to activate beans to support async functionality in Spring based applications.

I was curious about how such annotations work and wanted to document my understanding. The way these annotations are supported can be considered part of the SPI and so may break if the internal implementation changes in future.

Simple Enable* Annotations

One way to think about these custom annotations is that they add a set of new beans into the Spring’s application context. Let us start by defining one such custom annotation:

1@Retention(RetentionPolicy.RUNTIME)
2@Target(ElementType.TYPE)
3@interface?EnableSomeBeans {}

and apply this annotation on a Spring @Configuration class:

1@Configuration
2@EnableSomeBeans
3public?static?class?SpringConfig {}

So now to bring in a set of beans when this annotation is applied is as simple as adding the set of beans to bring in using @Import annotation this way:

1@Retention(RetentionPolicy.RUNTIME)
2@Target(ElementType.TYPE)
3@Import(SomeBeanConfiguration.class)
4@interface?EnableSomeBeans {}

That is essentially it, if this imported @Configuration class defines any beans, they would now be part of the Application context:

01@Configuration
02class?SomeBeanConfiguration {
03?
04????@Bean
05????public?String aBean1() {
06????????return?"aBean1";
07????}
08?
09????@Bean
10????public?String aBean2() {
11????????return?"aBean2";
12????}
13}

Here is a?gist?with a working sample.

Enable* Annotations with Selectors

Enable annotations can be far more complex though, they can activate a different family of beans based on the context around them. An example of such an annotation is?EnableCaching?which activates configuration based on different caching implementations available in the classpath.

Writing such Enable* annotations is a little more involved than the simpler example earlier. As before start with a custom annotation:

1@Retention(RetentionPolicy.RUNTIME)
2@Target(ElementType.TYPE)
3@Import(SomeBeanConfigurationSelector.class)
4public?@interface?EnableSomeBeansSelector {
5????String criteria()?default?"default";
6}

Note that in this case the custom annotation has a sample field called criteria, what I want to do is to activate two different set of beans based on this criteria. This can be achieved using a @Configuration selector which can return different @Configuration file based on the context(in this instance the value of the criteria field). This selector has a simple signature and this is a sample implementation:

01import?org.springframework.context.annotation.ImportSelector;
02import?org.springframework.core.annotation.AnnotationAttributes;
03import?org.springframework.core.type.AnnotationMetadata;
04?
05public?class?SomeBeanConfigurationSelector?implements?ImportSelector {
06????@Override
07????public?String[] selectImports(AnnotationMetadata importingClassMetadata) {
08????????AnnotationAttributes attributes =
09????????????????AnnotationAttributes.fromMap(
10????????????????????????importingClassMetadata.getAnnotationAttributes
11(EnableSomeBeansSelector.class.getName(),?false));
12????????String criteria = attributes.getString("criteria");
13????????if?(criteria.equals("default")) {
14????????????return?new?String[]{"enableannot.selector.SomeBeanConfigurationDefault"};
15????????}else?{
16????????????return?new?String[]{"enableannot.selector.SomeBeanConfigurationType1"};
17????????}
18????}
19}
20?
21@Configuration
22class?SomeBeanConfigurationType1 {
23?
24????@Bean
25????public?String aBean() {
26????????return?"Type1";
27????}
28?
29}
30?
31@Configuration
32class?SomeBeanConfigurationDefault {
33?
34????@Bean
35????public?String aBean() {
36????????return?"Default";
37????}
38?
39}

So if the criteria field is “default”, the beans in “SomeBeanConfigurationDefault” gets added in, else the one in “SomeBeanConfigurationType1”

  • Here is a?gist?with a working sample.

Conclusion

I hope this gives an appreciation for how Spring internally implements the @Enable* annotations, as an application developer you may not need to create such annotations yourself, a simpler mechanism will be to use @Configuration classes and Spring bean profiles to compose applications.

轉載于:https://www.cnblogs.com/davidwang456/p/6245751.html

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的Spring Enable annotation – writing a custom Enable annotation的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 有码视频在线观看 | 一本一道精品欧美中文字幕 | 最新日韩中文字幕 | 中国老太婆性视频 | 欧美另类精品xxxx孕妇 | 九九热精彩视频 | xxxxxhd亚洲人hd | 国产a毛片| 亚洲成网站 | 私密视频在线观看 | 狠狠躁夜夜躁人爽 | 国产精品久久久久久久久夜色 | 奇米亚洲 | 欧美久久一级 | 精品国产免费无码久久久 | 久久久久久久一 | 国产污污在线观看 | 一级黄色在线观看 | 草久网 | 91超薄肉色丝袜交足高跟凉鞋 | 啄木乌欧美一区二区三区 | 亚洲精品一区二区三区中文字幕 | 黄色一级大片在线免费看国产 | 欧美顶级黄色大片免费 | 日韩精选视频 | 相亲对象是问题学生在线观看 | 午夜视频在线免费 | 一起草视频在线播放 | 97国产精品视频人人做人人爱 | 朝桐光av一区二区三区 | 精品人妻天天爽夜夜爽视频 | 吊侵犯の奶水授乳羞羞漫画 | 91午夜精品亚洲一区二区三区 | 深爱婷婷| 中文字幕另类 | 丝袜天堂 | 欧美精品国产 | 国产毛片视频网站 | 久久精品第一页 | 国产日韩一区二区在线 | 伊人免费在线观看高清版 | 久久久午夜精品福利内容 | 插女生下面 | 国产视频二区 | 欧美色久 | 亚洲av成人精品一区二区三区在线播放 | 91ts人妖另类精品系列 | 午夜丁香网 | a天堂视频在线观看 | 一出一进一爽一粗一大视频 | 日韩jizz| 尤物视频免费在线观看 | 日本一级三级三级三级 | 国产精品久久久久久久久久免费看 | 亚洲第一免费 | 亚洲啪av永久无码精品放毛片 | 主人性调教le百合sm | 天堂8av | 久久大胆人体 | 一级片小视频 | 毛片网站免费 | 国产嫩bbwbbw高潮 | 中国精品视频 | 午夜国产福利 | 熟女高潮一区二区三区 | av网站有哪些 | 精品女同一区二区三区 | 欧美人性生活视频 | 日本亚洲欧洲色 | 国产精品区二区三区日本 | 亚洲男女| 成人动漫中文字幕 | 日韩国产欧美一区二区三区 | 91成人亚洲 | 在线观看羞羞漫画 | 美女激情av| 热热色av| 午夜啪啪福利 | www.九九九 | 亚洲码无人客一区二区三区 | 中文字幕a级片 | 草草影院欧美 | 免费在线一级片 | 亚洲男女视频在线观看 | 久久久青 | 日韩精品免费 | 高柳家在线观看 | 秋霞午夜鲁丝一区二区老狼 | 午夜剧场免费在线观看 | 日韩欧美超碰 | 色婷婷中文 | 天堂福利在线 | 成人黄色大片 | 性欧美一区二区 | 亚洲一区二区在线 | 精品一区二区国产 | 久久伊人中文字幕 | 日韩精品一区二区三区无码专区 | 亚洲欧美偷拍一区 |