【SpringBoot专题】@Enable*注解使用的几种姿势

在这里插入图片描述
@EnableAutoConfiguration的官方解释:The second class-level annotation is @EnableAutoConfiguration. This annotation tells Spring Boot to “guess” how you want to configure Spring, based on the jar dependencies that you have added. Since spring-boot-starter-web added Tomcat and Spring MVC, the auto-configuration assumes that you are developing a web application and sets up Spring accordingly.

大概意思是说springboot会将那些标有@Enable*注解的配置,自动的注入到spring容器中,今天要讨论的是@Enable注入的几种方式

一、直接使用Import注入配置

创建HelloWorldConfiguration注解,并且声明一个bean

public class HelloWorldConfiguration {

    @Bean
    public String helloWorld() {
        return "hello world";
    }
}

创建注解EnableHelloWorld,直接Import HelloWorldConfiguration

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(HelloWorldConfiguration.class)
//@Import(HelloWorldImportSelector.class)
public @interface EnableHelloWorld {
}

创建启动类EnableHelloWorldBootstrap ,获取helloWorld这个Bean

@EnableHelloWorld
public class EnableHelloWorldBootstrap {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(EnableHelloWorldBootstrap.class, args);
        String helloWorld = context.getBean("helloWorld", String.class);
        System.out.println("hello world bean:" + helloWorld);
    }
}

运行这个bootstrap,可以看到获取helloWorld这个bean
在这里插入图片描述

二、基于ImportSelector实现

ImportSelector Spring会把实现ImportSelector接口的类中的SelectImport方法返回的值注入到Spring容器中。这个方法的返回值必须是一个class的全类名的String[],创建HelloWorldImportSelector

/**
 *  HelloWorld {@link ImportSelector}
 */
public class HelloWorldImportSelector implements ImportSelector {
    @Override
    public String[] selectImports(AnnotationMetadata annotationMetadata) {
        return new String[]{HelloWorldConfiguration.class.getName()};
    }
}

修改EnableHelloWorld注解

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
//@Import(HelloWorldConfiguration.class)
@Import(HelloWorldImportSelector.class)
public @interface EnableHelloWorld {
}

再次运行bootstrap这个类,可以看到同样的结果
在这里插入图片描述

总结,两种方式都可以实现@Enable的模块化装配,如果有需求需要条件注入,第一种就需要配合@ConditionXXX或者@Profile实现;第二种方式可以通过编码的方式去实现

三、基于ImportBeanDefinitionRegistrar实现

ImportBeanDefinitionRegistrar registerBeanDefinitions方法的参数有一个BeanDefinitionRegistry, BeanDefinitionRegistry可以用来往spring容器中注入bean 如此,我们就可以在registerBeanDefinitions方法里面动态的注入bean

实现一个HelloWorldImportBeanDefinitionRegistrar

public class HelloWorldImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {

    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        BeanDefinitionBuilder bdb = BeanDefinitionBuilder.rootBeanDefinition(HelloWorldConfiguration.class);
        registry.registerBeanDefinition(HelloWorldConfiguration.class.getName(), bdb.getBeanDefinition());
    }
}

修改EnableHelloWorld注解

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
//@Import(HelloWorldConfiguration.class)
//@Import(HelloWorldImportSelector.class)
@Import(HelloWorldImportBeanDefinitionRegistrar.class)
public @interface EnableHelloWorld {
}

再次运行bootstrap这个类,可以看到同样的结果
在这里插入图片描述

github地址 https://github.com/fafeidou/fast-cloud-nacos/tree/master/fast-boot-examples/auto-configure
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值