2018-03-08
springboot-自动配置理解
springboot 评论:0 浏览:139

转载请注明出处:https://oldnoop.tech/c/165.html

为了更好的理解,以druid-spring-boot-starter为例

查看这个jar包的源代码,结构如下

这里实现自动配置的几个核心文件

spring.factories,
additional-spring-configuration-metadata.json
spring-configuration-metadata.json
com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure.class

spring.factories

设置开启自动配置的配置类

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure

DruidDataSourceAutoConfigure.class

@Configuration

表示配置类

@ConditionalOnClass(com.alibaba.druid.pool.DruidDataSource.class)

表示当类路径

存在这个类DruidDataSource时才会加载这个配置类DruidDataSourceAutoConfigure ,
否则跳过,这个很有用比如不同jar包间类依赖,依赖的类不存在直接跳过,不会报错

@AutoConfigureBefore(DataSourceAutoConfiguration.class)

表示配置数据的封装对象类

@Configuration
@ConditionalOnClass(com.alibaba.druid.pool.DruidDataSource.class)
@AutoConfigureBefore(DataSourceAutoConfiguration.class)
@EnableConfigurationProperties({DruidStatProperties.class, DataSourceProperties.class})
@Import({DruidSpringAopConfiguration.class,
        DruidStatViewServletConfiguration.class,
        DruidWebStatFilterConfiguration.class,
        DruidFilterConfiguration.class})
public class DruidDataSourceAutoConfigure {

    @Bean
    @ConditionalOnMissingBean
    public DataSource dataSource() {
        return new DruidDataSourceWrapper();
    }

    /**
     * Register the {@link DataSourcePoolMetadataProvider} instances to support DataSource metrics.
     *
     * @see DataSourcePoolMetadataProvidersConfiguration
     */
    @Bean
    public DataSourcePoolMetadataProvider druidDataSourcePoolMetadataProvider() {
        return new DataSourcePoolMetadataProvider() {
            @Override
            public DataSourcePoolMetadata getDataSourcePoolMetadata(DataSource dataSource) {
                if (dataSource instanceof DruidDataSource) {
                    return new DruidDataSourcePoolMetadata((DruidDataSource) dataSource);
                }
                return null;
            }
        };
    }
}

 

spring-configuration-metadata.json

springboot工程在修改application.properties,或者application.yml文件时,能够自动提示,

这个文件就是来描述可以配置的属性信息的 元数据信息

{
  "hints": [
    {
      "values": [
        {
          "value": "db2"
        },
        {
          "value": "postgresql"
        },
        {
          "value": "sqlserver"
        },
        {
          "value": "oracle"
        },
        {
          "value": "AliOracle"
        },
        {
          "value": "mysql"
        },

        ]

        //省略

"name": "spring.datasource.druid.filter.stat.db-type"

当修改application.properties,自动提示截图

其他部分,定义属性名称,以及对应的属性封装对象类

比如druid监控统计(stat)模块的属性设置

"groups": [
   {
      "sourceType": "com.alibaba.druid.spring.boot.autoconfigure.stat.DruidFilterConfiguration",
      "name": "spring.datasource.druid.filter.stat",
      "sourceMethod": "statFilter()",
      "type": "com.alibaba.druid.filter.stat.StatFilter"
    },

 

 



  • 转载请注明出处:https://oldnoop.tech/c/165.html
验证码

Copyright © 2018 oldnoop.tech. All Rights Reserved

鄂ICP备18003018号