手写Spring Boot@Enablexxx模块驱动

在spring框架中,我们可以看到有许多的@Enablexxx注解,如spring的Cachine模块@EnableCaching,在springboot中有自动装配模块@EnableAutoConfiguration、OAuth2单点登录@EnableOAuth2Sso,在springcloud中还有Eureka服务器模块@EnableEurekaServer、Feign客户端模块@EnableFeignClients等@Enable注解。@Enablexx注解能够简化装配步骤,实现按需装配,同时屏蔽组件装配细节,不过要使用@Enable模块也必须手动触发,加注解在某个配置的bean上。

  • 手写@Enable模块分两类实现,一种是“注解驱动”,另一种是“接口编程”,后者更难,也可以再细分为两种实现方式。在手写@Enable模块的时候,也可以看看spring框架已定义好的@Enable相关注解,这样可以有助于我们理解该模块的设计思想。

  • 实现@Enable模块的核心注解是@Import注解,其职责在于装载导入类(Importing Class),将其定义为Srping Bean,导入类主要为@Configuration Class、ImportSelector实现类以及ImportBeanDefinitionRegistrar实现类三种,但@Import注解的功能不仅仅是为了实现@Enable模块,这点需要注意到,在了解springboot starter自动装配过程中就可以看到很多@Import注解的身影。

  • @Import注解源码,看value方法注释可值,所“@Import”的value值有三种类类型,@Configuration注解修饰类、实现ImportSelector接口类、实现ImportBeanDefinitionRegistrar接口类,“ or regular component classes to import” 或者有规则的被@component注解修饰的类,因为@Configuration也是由@component注解修饰,故也合理。

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Import {

    /**
     * {@link Configuration @Configuration}, {@link ImportSelector},
     * {@link ImportBeanDefinitionRegistrar}, or regular component classes to import.
     */
    Class<?>[] value();

}
  • 本文代码项目目录结构如图所示,在spring-boot项目的spring-boot-enable模块:


    spring-boot-enable代码目录结构.png
  • springboot项目默认扫描引导类目录下有@Componet注解的类或@Componet注解的派生注解,如@Configuration、@Service等注解,而@Import注解的功能是装载导入类,为了演示@Import注解的功能实现@Enable模块,示例代码没写在引导类SpringBootEnableApplication的com.dayue.springbootenable包目录下,避免被springboot扫描并自动装载,故写在了com.dayue.enable.xxxx包目录下

(1)方式一:“注解驱动”,@Import导入xxxConfiguration类
  • domain Student类
@Data
public class Student {

    private String name;

    private Integer age;
}
  • domain Teacher类
@Data
public class Teacher {

    private String name;

    private Integer age;
}
  • HelloWorldConfiguration类
@Configuration
@Slf4j
public class HelloWorldConfiguration {

    @Bean
    public Student student() {
        log.info("初始化student");
        return new Student();
    }
}
  • @EnableHelloWorld注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(HelloWorldConfiguration.class)
public @interface EnableHelloWorld {
}

启动项目,我们会发现Student bean并没有被springboot扫描并装载,然后在引导类SpringBootEnableApplication上面加注解@EnableHelloWorld,再次启动项目,通过日志可以看到Student bean已经被初始化了。

@SpringBootApplication
@EnableHelloWorld
public class SpringBootEnableApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootEnableApplication.class, args);
    }

}
方式一注解驱动,启动项目.png
(2)方式二:接口编程,@Import导入实现ImportSelector类

使用接口编程方式实现@Enable模块,需要实现ImportSelector类或ImportBeanDefinitionRegistrar类,相对于方式一,导入xxxConfiguration类,实现ImportSelector类和实现ImportBeanDefinitionRegistrar类的方式弹性更大,可以动态地选择一个或者多个@Componet类进行导入,使用的是Spring注解元信息AnnotationMetadata作为方法参数。

示例代码通过一个接口,两个接口实现类,在@Enable模块中通过传入枚举值实现动态选择其中一个实现类注册为Spring Bean供controller层来使用。

  • ActionType枚举类
public enum ActionType {

    /**
     * 老师
     */
    TEACHER,
    /**
     * 学生
     */
    STUDENT
}
  • @EnableActionServer注解,后面实现ImportSelector类通过枚举值来动态选择实现类,老师上课或者学生上课
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(ServerImportSelector.class)
public @interface EnableActionServer {

    /**
     * 设置动作类型,默认为老师上课
     */
    ActionType serverType() default ActionType.TEACHER;
}

  • ActionServer接口类
public interface ActionServer {

    /**
     * 动作
     */
    String action();
}
  • StudentActionServerImpl实现类
@Slf4j
@Service
public class StudentActionServerImpl implements ActionServer {

    @Bean
    public Student studentone() {
        log.info("初始化student bean");
        return new Student();
    }

    @Override
    public String action() {
        log.info("学生上课");
        return "学生上课";
    }

}
  • TeacherActionServerImpl实现类
@Slf4j
@Service
public class TeacherActionServerImpl implements ActionServer {

    @Bean
    public Teacher teacherone() {
        log.info("初始化teacher bean");
        return new Teacher();
    }

    @Override
    public String action() {
        log.info("老师上课");
        return "老师上课";
    }
}
  • ServerImportSelector实现类,实现ImportSelector的selectImports方法,方法参数是Spring注解元信息AnnotationMetadata,通过该参数可以获取注解@EnableActionServer相关元信息,如@EnableActionServer注解的属性方法serverType(),返回其定义好的值,代码通过判断定义的ActionType来选择返回不同的类名。
@Slf4j
public class ServerImportSelector implements ImportSelector {

    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        //获取注解@EnableActionServer元信息
        Map<String, Object> annotationAttributes = importingClassMetadata.getAnnotationAttributes(EnableActionServer.class.getName());
        assert annotationAttributes != null;
        //通过获取的注解@EnableActionServer元信息,取定义好的方法属性,并把对象Object强制转换为ActionType枚举类型
        ActionType serverType = (ActionType) annotationAttributes.get("serverType");
        log.info("serverType:{},getServerType(serverType):{}", serverType, getServerType(serverType));
        //最后返回要选择的类名,注册为spring bean组件
        return new String[]{getServerType(serverType)};
    }
    
    //封装一层,通过枚举值返回不同的类名
    public String getServerType(ActionType serverType) {
        Map<ActionType, String> serverClassMap; serverClassMap = new HashMap<>(2);
        serverClassMap.put(ActionType.TEACHER, TeacherActionServerImpl.class.getName());
        serverClassMap.put(ActionType.STUDENT, StudentActionServerImpl.class.getName());
        return serverClassMap.get(serverType);
    }

}

在controller层注入ActionServer,然后启动项目,可以发现报错了。报错是因为没有定义好Spring Bean,题外话:当然可以通过@Bean的方式去注入某个实现类或者在实现类上加@ Server注解再选择其中之一的实现类去注入。

  • ActionServerController类
@RestController
@RequestMapping("/api/actionServer")
public class ActionServerController {

    @Autowired
    private ActionServer actionServer;

    @GetMapping("/actionType")
    public String actionType() {
        return actionServer.action();
    }
}
注入ActionServer类报错.png

最后,我们也用实现ImportSelector接口类的方式去实现@Enable模块,那么在引导类上加上注解@EnableActionServer(serverType = ActionType.STUDENT)即可,再次启动项目

@SpringBootApplication
@EnableActionServer(serverType = ActionType.STUDENT)
public class SpringBootEnableApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootEnableApplication.class, args);
    }

}
方式二启动项目.png
浏览器访问接口.png
(3)方式三:接口编程,@Import导入实现ImportBeanDefinitionRegistrar类

ImportBeanDefinitionRegistrar相对于ImportSelector而言,编程复杂度更高,除了注解元信息AnnotationMetadata作为入参外,接口将定义Bean的注册交给开发人员。示例代码复用方式二的选择实现类逻辑,故直接通过new ServerImportSelector()的方式来复用实现好的selectImports方法,入参即为注解元信息AnnotationMetadata,返回类名,最后完成注册返回类名的类为Spring Bean。

  • ServerImportBeanDefinitionRegistrar实现ImportBeanDefinitionRegistrar类
public class ServerImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {

    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        //复用前面实现好的ImportSelector
        ImportSelector importSelector = new ServerImportSelector();
        //筛选ClassNames集合
        String[] selectClassNames = importSelector.selectImports(importingClassMetadata);
        Stream.of(selectClassNames)
                //转化为 BeanDefinitionBuilder 对象
                .map(BeanDefinitionBuilder::genericBeanDefinition)
                //转化为 BeanDefinition 对象
                .map(BeanDefinitionBuilder::getBeanDefinition)
                //注册 BeanDefinition 到 BeanDefinitionRegistry
                .forEach(beanDefinition -> BeanDefinitionReaderUtils.registerWithGeneratedName(beanDefinition, registry));
    }
}

接着修改前面写好的注解@EnableActionServer注解的@Import导入类,修改为@Import(ServerImportBeanDefinitionRegistrar.class)

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
//@Import(ServerImportSelector.class)
@Import(ServerImportBeanDefinitionRegistrar.class)
public @interface EnableActionServer {

    /**
     * 设置动作类型,默认为老师上课
     */
    ActionType serverType() default ActionType.TEACHER;
}

再次启动项目即可观察结果。

参考资料:
《Spring Boot编程思想》

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,319评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,801评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,567评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,156评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,019评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,090评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,500评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,192评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,474评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,566评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,338评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,212评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,572评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,890评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,169评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,478评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,661评论 2 335