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 ...
随机推荐
- spring+springmvc+ibatis整合注解方式实例
需求说明 实现用户通过数据库验证登录需求.採用 Myeclipse+Tomcat 6.0+Mysql 5.0+JDK 1.6 2.数据库表 开发所用是Mysql数据库,仅仅建立单张用户表T_USER, ...
- jquery中选择器input:hidden和input[type=hidden]的区别
关于选择器:hidden的说明,在jquery说明文档中是这样说的:匹配所有不可见元素,或者type为hidden的元素.而[type=hidden]是查找所有type属性等于hidden的元素.两者 ...
- Docker Mysql数据库双主同步配置方法
一.背景 可先查看第一篇<Docker Mysql数据库主从同步配置方法>介绍 二.具体操作 1.创建目录(~/test/mysql_test1): --mysql --mone --da ...
- MySQL技术内幕读书笔记(六)——索引与算法之全文索引
全文索引 概述 通过索引字段的前缀进行查找,B+树索引是支持的,利用B+树索引就可以进行快速查询. SELECT * FROM blog WHERE content like 'xxx%'; ...
- SQL 数据库结构化查询语言
1.数据库 常见数据库 MySQL:开源免费的数据库,小型的数据库. Oracle:收费的大型数据库,Oracle 公司的产品 DB2:IBM 公司收费的数据库,常应用在银行系统中 SQLServer ...
- 查看PostgreSQL正在执行的SQL
SELECT procpid, START, now() - START AS lap, current_query FROM ( SELECT backendid, pg_stat_get_back ...
- WebViewJavascriptBridge 进行js 与native通信。
1, iOS端加载web页面.开启日志并给webView建立JS与OC的桥梁 - (void)viewWillAppear:(BOOL)animated { if (_bridge) { retur ...
- 【OpenFOAM案例】02 自己动手
前言:很多人说OpenFOAM很难,要啃上很多的理论书籍,什么流体力学.计算流体力学.矩阵理论.线性代数.数值计算.C++程序设计神马的,看看光这一堆书就能吓倒绝大多数的人.其实我们并不一定要从这些基 ...
- HTML Table to Json
HTML 表格输出JSON <table class="table table-striped table-bordered table-hover dataTable no-foot ...
- Java多线程:Java内存模型
参考资料: 程晓明:Java内存模型 <Java并发编程的艺术> <深入理解Java虚拟机:JVM高级特性与最佳实践>