SpringBoot学习:多模块下自动配置与Yaml

多模块开发中每个模块都有自己的配置文件和配置类,可以通过以下几种方式引入:

  • yml引入
  1. 配置文件方式:(每个springboot项目中都需要引入,对于springboot源配置并没有侵入性)

    spring:
    	  profiles:
    	    include: es,db,kafkaconsumer
  2. 配置类方式:(以下方式可以在导入依赖自动加载,不需在配置文件中引入,但是侵入了springboot默认加载application文件的顺序,尤其是项目上线时,application配置文件外置自动加载无效)

    /** * @author: hs * @Date: 2019/4/25 19:11 * @Description: 无需使用 include引入多个配置,缺点打包发布时,并不会按照springboot默认加载文件顺序来读取文件。 */
    @Configuration
    @AutoConfigureOrder(1)
    public class YamlAutoConfig {
        @Bean
        public static PropertySourcesPlaceholderConfigurer properties() {
            PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
            YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
            ClassPathResource dbYaml = new ClassPathResource("application-db.yml");
            ClassPathResource esYaml = new ClassPathResource("application-es.yml");
            ClassPathResource producerYaml = new ClassPathResource("application-kafkaproducer.yml");
            ClassPathResource consumerYaml = new ClassPathResource("application-kafkaconsumer.yml");
            ClassPathResource defaultYaml = new ClassPathResource("application.yml");
            yaml.setResources(
                    dbYaml.exists() ? dbYaml : defaultYaml,
                    esYaml.exists() ? esYaml : defaultYaml,
                    producerYaml.exists() ? producerYaml : defaultYaml,
                    consumerYaml.exists() ? consumerYaml : defaultYaml
            );
            configurer.setProperties(yaml.getObject());
            return configurer;
        }
    
    }
  3. @PropertySource(value = "classpath:default.properties"):(在引入别的模块中的properties,不要指定为application开头)

  • 配置类的自动配置问题
  1. @ComponentScan(basePackages = {"com.staryea.pinpoint"}):(@SpringBootApplication加载的配置文件是其所在目录或子目录的文件,多模块情况下可以通过@ComponentScan进行扫描)

  2. @Import({XXXXConfig.class}):(此注解也可以引入指定的配置,更好的使用是自定义注解的方式,如下在springboot入口类上写入此注解即可)

    /** * Cors 跨域过滤 * @author hs */
    @Target({ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Inherited
    @Import({CorsFilterConfig.class})
    public @interface EnableCorsFilter {
    
    }
  3. 利用@EnableAutoConfiguration的自动配置:(resources下新建META-INF,META-INF中新建spring.factories,具体配置如下)(推荐使用)

    org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
      com.staryea.invocate.config.DruidDataSourceConfig,\-->此处引入自己的配置类
      com.staryea.invocate.config.CmdbConfig,\
      com.staryea.invocate.dao.impl.CmdbClassDaoImpl

    这里可能会在target/classes/META-INF下生成名字为spring-configuration-metadata.json的文件,此文件只在@ConfigurationProperties(prefix = "cmdb")注解引入配置文件配置的时候会生成,@Value("${spring.datasource.url}")注解引入的配置是不会生成的,文件内容如下:

    {
      "hints": [],
      "groups": [
        {
          "sourceType": "com.staryea.invocate.config.CmdbConfig",
          "name": "cmdb",
          "type": "com.staryea.invocate.config.CmdbConfig"
        }
      ],
      "properties": [
        {
          "sourceType": "com.staryea.invocate.config.CmdbConfig",
          "name": "cmdb.api",
          "type": "java.lang.String"
        },
        {
          "sourceType": "com.staryea.invocate.config.CmdbConfig",
          "name": "cmdb.password",
          "type": "java.lang.String"
        },
        {
          "sourceType": "com.staryea.invocate.config.CmdbConfig",
          "name": "cmdb.username",
          "type": "java.lang.String"
        }
      ]
    }

    在这里插入图片描述
    结构:
    在这里插入图片描述 以上是本人多模块配置心得,如有不对,还请见谅。