原文地址: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:
3 |
public 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:
02 |
class SomeBeanConfiguration { |
05 |
public String aBean1() { |
10 |
public String aBean2() { |
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) |
4 |
public @interface EnableSomeBeansSelector { |
5 |
String criteria() default "default"; |
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:
01 |
import org.springframework.context.annotation.ImportSelector; |
02 |
import org.springframework.core.annotation.AnnotationAttributes; |
03 |
import org.springframework.core.type.AnnotationMetadata; |
05 |
public class SomeBeanConfigurationSelector implements ImportSelector { |
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"}; |
16 |
return new String[]{"enableannot.selector.SomeBeanConfigurationType1"}; |
22 |
class SomeBeanConfigurationType1 { |
25 |
public String aBean() { |
32 |
class SomeBeanConfigurationDefault { |
35 |
public String aBean() { |
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.
- Define custom @Required-style annotation in Spring
The @Required annotation is used to make sure a particular property has been set. If you are migrate ...
- Spring 4 Ehcache Configuration Example with @Cacheable Annotation
http://www.concretepage.com/spring-4/spring-4-ehcache-configuration-example-with-cacheable-annotatio ...
- Spring入门6事务管理2 基于Annotation方式的声明式事务管理机制
Spring入门6事务管理2 基于Annotation方式的声明式事务管理机制 201311.27 代码下载 链接: http://pan.baidu.com/s/1kYc6c 密码: 233t 前言 ...
- Spring整合Hibernate:2、使用Annotation方式进行声明式的事务管理
1.加入DataSourceTransactionManager的命名空间 修改applicationContext.xml文件,增加如下内容: 1 2 3 4 5 6 7 8 9 10 11 12 ...
- 看Spring源码不得不会的@Enable模块驱动实现原理讲解
这篇文章我想和你聊一聊 spring的@Enable模块驱动的实现原理. 在我们平时使用spring的过程中,如果想要加个定时任务的功能,那么就需要加注解@EnableScheduling,如果想使用 ...
- 尚学堂Spring视频教程(四):使用Annotation
之前我们的bean都配置在XML里,并且通过bean的property标签来指定依赖关系,如果项目很大,那岂不是要配置很多这样的property标签?Spring提供了注解的方式来解决这个问题 @Au ...
- Spring(3.2.3) - Beans(8): 基于 Annotation 的配置
除了基于 XML 的配置外,Spring 也支持基于 Annotation 的配置.Spring 提供以下介个 Annotation 来标注 Spring Bean: @Component:标注一个普 ...
- Spring MVC 学习笔记3 - 利用Default Annotation 模式获取请求,使Controller与View对应,并传值。
1. WEB-INF/web.xml 这里定义了获取请求后,执行的第一步.抓取请求. <servlet> <servlet-name>appServlet</servle ...
- Spring使用AspectJ开发AOP:基于Annotation
基于 Annotation 的声明式 在 Spring 中,尽管使用 XML 配置文件可以实现 AOP 开发,但是如果所有的相关的配置都集中在配置文件中,势必会导致 XML 配置文件过于臃肿,从而给维 ...
随机推荐
- 使用 JavaScriptService 在.NET Core 里实现DES加密算法
文章<ASP.NET Core love JavaScript>和<跨平台的 NodeJS 组件解决 .NetCore 不支持 System.Drawing图形功能的若干问题> ...
- 前端学HTTP之安全HTTP
前面的话 HTTP的主要不足包括通信使用明文(不加密),内容可能会被窃听:不验证通信方的身份,有可能遭遇伪装:无法证明报文的完整性,有可能被篡改 基本认证和摘要认证能够使得用户识别后较安全的访问服务器 ...
- 11、Struts2 的文件上传和下载
文件上传 表单准备 要想使用 HTML 表单上传一个或多个文件 须把 HTML 表单的 enctype 属性设置为 multipart/form-data 须把 HTML 表单的method 属性设置 ...
- html5 canvas常用api总结(二)--绘图API
canvas可以绘制出很多奇妙的样式和美丽的效果,通过几个简单的api就可以在画布上呈现出千变万化的效果,还可以制作网页游戏,接下来就总结一下和绘图有关的API. 绘画的时候canvas相当于画布,而 ...
- 玩转spring boot——结合JPA事务
接着上篇 一.准备工作 修改pom.xml文件 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&q ...
- 【热门技术】EventBus 3.0,让事件订阅更简单,从此告别组件消息传递烦恼~
一.写在前面 还在为时间接收而烦恼吗?还在为各种组件间的消息传递烦恼吗?EventBus 3.0,专注于android的发布.订阅事件总线,让各组件间的消息传递更简单!完美替代Intent,Handl ...
- java8中lambda表达式的应用,以及一些泛型相关
语法部分就不写了,我们直接抛出一个实际问题,看看java8的这些新特性究竟能给我们带来哪些便利 顺带用到一些泛型编程,一切都是为了简化代码 场景: 一个数据类,用于记录职工信息 public clas ...
- Cesium简介以及离线部署运行
Cesium简介 cesium是国外一个基于JavaScript编写的使用WebGL的地图引擎,一款开源3DGIS的js库.cesium支持3D,2D,2.5D形式的地图展示,可以自行绘制图形,高亮区 ...
- BPM Domino集成解决方案
一.需求分析 Lotus Notes/Domino是IBM的协同办公平台,在国内有广泛的用户. 但由于推出年头较早.采用文档数据库等特点, 导致其流程集成能力弱.统计分析难.不支持移动办公等问题,很多 ...
- H3 BPM让天下没有难用的流程之技术特性
一.集成性 H3 BPM可以与其它系统进行多个层面的集成,满足企业的针对不同系统的集成需求. 图:多种集成维度 Ø 用户集成 可与企业现有系统进行组织架构同步或调用,也可以直接与AD 进行集成. ...