SpringBoot自动配置实现平台核心服务自动加载

什么是自动配置

    看下官方原文咋说的,Spring Boot auto-configuration attempts to automatically configure your Spring application based on the jar dependencies that you have added. For example, if HSQLDB is on your classpath, and you have not manually configured any database connection beans, then Spring Boot auto-configures an in-memory database.

    意思就是SpringBoot可以根据我们所添加的包依赖来自动的配置,就比如要使用HSQLDB 的时候,我们添加了HSQLDB 的依赖,SpringBoot会自动配置对应的数据库连接bean

如何实现自动配置

1. @SpringBootApplication -->@EnableAutoConfiguration-->AutoConfigurationImportSelector通过SpringFactoriesLoader.loadFactoryNames方法加载META-INF/spring.factories文件中的配置信息,这里面列出了自动化配置相关的类。springboot会扫描这些配置,然后按照清单去尝试是否需要配置。其实各配置类上有使用@Configuration注解,但是该注解只有在@ComponentScan注解扫描到的情况加才会进行尝试配置,但是由于这些类在springboot的jar保内,基本不会被扫描到,因此需要配置到spring.factories中进行扫描加载;

2. 配置类通过@EnableConfigurationProperties将对应的配置信息类实例化,其作用同META-INF/spring.factories文件,因为配置信息类由springboot维护的,很可能扫描不到;配置类根据条件确定是否需要实例化对应的bean,扫描spring.factories,将其文件包装成Properties对象,将初始化的bean添加到容器里边。

下面来举个小栗子:

1、新建maven工程,引入必要依赖
    <groupId>fun.codenow</groupId>
    <artifactId>hello-spring-boot-starter</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <description>codenow.fun 平台自定义 hello starter</description>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.2.6.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>


    具体的依赖

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure-processor</artifactId>
            <scope>compile</scope>
            <optional>true</optional>
        </dependency>

    </dependencies>

2、自定义配置类数据模型:

 @ConfigurationProperties(prefix = "codenow.config.hello")
 public class HelloProperties {
    private String configname;
    private String testvalue;

    public String getConfigname() {
        return configname;
    }

    public void setConfigname(String configname) {
        this.configname = configname;
    }

    public String getTestvalue() {
        return testvalue;
    }

    public void setTestvalue(String testvalue) {
        this.testvalue = testvalue;
    }
 }

3、实现核心服务

public class HelloService {
    private HelloProperties helloProperties;
    public HelloService(){

    }
    public HelloService(HelloProperties helloProperties){
        this.helloProperties=helloProperties;
    }
    public void queryConfigure(){
        System.out.println("configname"+helloProperties.getConfigname()+",testvalue"+helloProperties.getTestvalue());
    }
}

4、自定义自动配置

@Configuration
@EnableConfigurationProperties({HelloProperties.class})
@ConditionalOnClass(HelloProperties.class)
public class HelloServiceAutoConfiguration {
    @Autowired
    private HelloProperties helloProperties;

    @Bean
    @ConditionalOnMissingBean(HelloProperties.class)
    public HelloService helloService(){
        HelloService helloService=new HelloService(helloProperties);
        return helloService;
    }
}

5、制定自动配置类的加载路径 META-INF/spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  fun.codenow.hello.autoconfigure.HelloServiceAutoConfiguration

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值