Spring Enable* 注解
Spring提供了一系列以Enable开头的注解,这些注解本质上是激活Spring的某些管理功能。比如,EnableWebMvc。 这个注解引入了MVC框架在Spring 应用中需要用到的所有bean。另外一个注解式EnableAsync, 它让Bean在spring 应用中支持异步功能。
我很好奇这些注解是怎样工作的,并把我的理解写下来。这些注解的工作原理可以理解为SPI的一部分,如果将来实现有变化可以切换。
简单的Enable*注解
我们可以把这些简单的注解理解为为spring 上下文引入一组bean。 我们开始来定义一些这样的注解:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface EnableSomeBeans {}
并把这个注解应用到spring 配置类上:
@Configuration
@EnableSomeBeans
public static class SpringConfig {}
如果希望引入一组新的bean, 只需要简单的使用@Import注解,如下:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Import(SomeBeanConfiguration.class)
@interface EnableSomeBeans {}
如果被引入的@Configuration定义了Bean, 那么这么bean就会被引入到spring 上下文。
@Configuration
class SomeBeanConfiguration { @Bean
public String aBean1() {
return "aBean1";
} @Bean
public String aBean2() {
return "aBean2";
}
}
完整代码可以从这里下载:https://gist.github.com/bijukunjummen/847456b55ae2340fff65
带Selector的Enable注解
当然,Enable注解可以更加复杂,可以根据所在上下文来激活不同类型的bean。比如:EnableCaching,可以根据类路径上的不同实现来激活对应的缓存。
这样的注解比前面的例子复杂些:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Import(SomeBeanConfigurationSelector.class)
public @interface EnableSomeBeansSelector {
String criteria() default "default";
}
上面的注解有一个叫criteria的属性,我想根据这个属性来激活不同的bean。这可以通过定义一个@Configuration Selector返回不同的@Configuration来实现。样板代码如下:
mport org.springframework.context.annotation.ImportSelector;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata; public class SomeBeanConfigurationSelector implements ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
AnnotationAttributes attributes =
AnnotationAttributes.fromMap(
importingClassMetadata.getAnnotationAttributes(EnableSomeBeansSelector.class.getName(), false));
String criteria = attributes.getString("criteria");
if (criteria.equals("default")) {
return new String[]{"enableannot.selector.SomeBeanConfigurationDefault"};
}else {
return new String[]{"enableannot.selector.SomeBeanConfigurationType1"};
}
}
} @Configuration
class SomeBeanConfigurationType1 { @Bean
public String aBean() {
return "Type1";
} } @Configuration
class SomeBeanConfigurationDefault { @Bean
public String aBean() {
return "Default";
} }
所以,如果criteria是detaul, SomeBeanConfigurationDefault被加入,反之,加入SomeBeanConfigurationType1。
原文地址:http://www.java-allandsundry.com/2015/04/spring-enable-annotation-writing-custom.html
Spring Enable* 注解的更多相关文章
- Spring 3.1新特性之二:@Enable*注解的源码,spring源码分析之定时任务Scheduled注解
分析SpringBoot的自动化配置原理的时候,可以观察下这些@Enable*注解的源码,可以发现所有的注解都有一个@Import注解.@Import注解是用来导入配置类的,这也就是说这些自动开启的实 ...
- Spring高级话题-@Enable***注解的工作原理
出自:http://blog.csdn.net/qq_26525215 @EnableAspectJAutoProxy @EnableAspectJAutoProxy注解 激活Aspect自动代理 & ...
- Spring Boot @Enable*注解源码解析及自定义@Enable*
Spring Boot 一个重要的特点就是自动配置,约定大于配置,几乎所有组件使用其本身约定好的默认配置就可以使用,大大减轻配置的麻烦.其实现自动配置一个方式就是使用@Enable*注解,见其名知 ...
- Spring中的@Enable注解
本文转载自SpringBoot中神奇的@Enable注解? 导语 在SpringBoot开发过程,我们经常会遇到@Enable开始的好多注解,比如@EnableEurekaServer.@Enable ...
- Spring高级特性之三:@Enable*注解的工作原理
Spring Boot中阐述热插拔技术的时候,简单地提及@Enable*注解.随着多种框架的应用及深入了解,@Enable*这个注解在各种框架中应用相当普及. 那么@Enable*注解工作原理是怎么样 ...
- Spring的@Enable*注解的工作原理
转自:https://blog.csdn.net/chengqiuming/article/details/81586948 一 列举几个@Enable*注解的功能 @EnableAspectJAut ...
- Java 必须掌握的 20+ 种 Spring 常用注解
Spring部分 1.声明bean的注解 @Component 组件,没有明确的角色 @Service 在业务逻辑层使用(service层) @Repository 在数据访问层使用(dao层) @C ...
- [No0000174]Spring常用注解(收藏大全)
Spring部分 1.声明bean的注解 @Component 组件,没有明确的角色 @Service 在业务逻辑层使用(service层) @Repository 在数据访问层使用(dao层) @C ...
- 【面试篇】必须掌握的Spring 常用注解
注解本身没有功能的,就和 xml 一样.注解和 xml 都是一种元数据,元数据即解释数据的数据,这就是所谓配置. 本文主要罗列 Spring|Spring MVC相关注解的简介. Spring部分 1 ...
随机推荐
- window炫丽cmd的别名cmder
windows下的cmd,色彩度用起来不爽,cmder是对cmd的补充,界面很清爽 01.下载 https://github.com/cmderdev/cmder http://cmder.net/ ...
- 【mysql】GitHub 的 MySQL 高可用性实践分享
原文出处: shlomi-noach 译文出处:oschina GitHub 使用 MySQL 作为所有非 git 仓库数据的主要存储, 它的可用性对 GitHub 的访问操作至关重要.Gi ...
- Laravel: 基础篇
一.安装 1)采用一键安装包 http://laravelacademy.org/resources-download 2)Mac 上安装 ----------在Mac上安装composer----- ...
- 转 : 深入解析Java锁机制
深入解析Java锁机制 https://mp.weixin.qq.com/s?__biz=MzU0OTE4MzYzMw%3D%3D&mid=2247485524&idx=1&s ...
- 转:图像处理、显示中的行宽(linesize)、步长(stride)、间距(pitch)
在图像数据传输和显示的过程中有一个不常用的参数:间距. 间距的名称: 它有很多的别名,在使用d3d显示的时候,它叫pitch:在用ffmpeg解码的时候,它叫linesize: 在用ffmpeg转换格 ...
- 如何用cmd命令递归文件夹中的所有特定文件,拷贝到另一个文件夹中
现在有一个文件夹,里面有很多子文件夹,每个子文件夹中有很多不同类型的图片,现在想将其所有.png图片整理出来,一开始我是手动拷贝的,拷贝了几个图片后,突然想能不能让计算机来自动完成此项功能,经过一番尝 ...
- 【资料下载区】【iCore1S相关代码、资料下载地址】更新日期2017/10/09
[iCore1S相关文档][更新中...] iCore1S原理图(PDF)下载iCore1S引脚注释(PDF)下载 [iCore1S相关例程代码][ARM][更新中...] DEMO1.0测试程序发布 ...
- 欢迎访问我的最新个人技术博客http://zhangxuefei.site
博客已经搬家,欢迎访问我的最新个人技术博客:http://zhangxuefei.site
- Selenium IDE 基本概念
要学会Selenium不难,难的是首先你懂不懂测试.没有测试的基础知识,没有对测试理论的实践和认知,没有对测试领域的情感和钻研精神,学会了Selenium这个工具对事情也没有实际帮助. 我是一个技术思 ...
- Ubuntu下pdf和图片互转
前边文章可以将ppt转换为pdf 查看 使用unoconv将ppt转为pdf,再使用imagemagick将pdf转为图片 这次想将pdf和图片进行互转 当前目录下只有2.ppt 1.ppt转pdf ...