环境:springboot2.2.13SpringBoot有以下两个配置文件bootstrap(.yml或.properties)application(.yml或.properties)下面说说这两个配置文件的区别吧!bootstrap/application的区别bootstrap.yml(bootstrap.properties)先加载application.yml(application.properties),然后再加载bootstrap.yml,用于应用上下文的bootstrap阶段,由父SpringApplicationContext加载。在使用application.yml之前加载父ApplicationContext。SpringBoot中有两个上下文,一个是bootstrap,一个是application,bootstrap是应用的父上下文,也就是说bootstrap加载优先于applicationn。Bootstrap主要用于加载额外资源的配置信息,也可以解密本地外部配置文件中的属性。这两个上下文共享一个环境,它是任何Spring应用程序的外部属性的来源。bootstrap中的properties会被优先加载,默认情况下不能被同一个本地配置覆盖,也就是说bootstrap中的配置不能被覆盖。bootstrap/application的应用场景应用配置文件简单易懂,主要用于SpringBoot项目的自动配置。bootstrap配置文件有以下应用场景。使用SpringCloudConfig配置中心时,需要在bootstrap配置文件中添加连接配置中心的配置属性,以加载外部配置中心的配置信息;一些不可覆盖的固定属性和一些加密/解密场景;thefollowingare来自官方对bootstrap.[yml/properties]的一个说明:ASpringCloudapplicationoperatesbycreatinga“bootstrap”context,whichisaparentcontextforthemainapplication.Thiscontextisresponsibleforloadingconfigurationpropertiesfromtheexternalsourcesandfordecryptingpropertiesinthelocalexternalconfigurationfiles.ThetwocontextsshareanEnvironment,whichisthesourceofexternalpropertiesforanySpringapplication.Bydefault,bootstrapproperties(notbootstrap.propertiesbutpropertiesthatareloadedduringthebootstrapphase)areaddedwithhighprecedence,sotheycannotbeoverriddenbylocalconfiguration.Thebootstrapcontextusesadifferentconventionforlocatingexternalconfigurationthanthemainapplicationcontext.Insteadofapplication.yml(or.properties),你可以使用bootstrap.yml,keepingtheexternalconfigurationforbootstrapandmaincontextnicelyseparatecustomconfigurationfile@Configuration@ConfigurationProperties(prefix="pack")@PropertySource(value="classpath:config.properties")publicclassPackProperties{privateStringname;privateIntegerage;}pack.name=xxxxpack.age=10注意:@PropertySource只能加载properties文件,不能加载yml文件导入SpringXML文件@Configuration@ImportResource(locations={"classpath:application-jdbc.xml","classpath:application-aop.xml"})publicclassResourceConfig{}application-jdbc.xml,application-aop.xml两个配置文件必须是spring配置文件。配置文件的默认值有如下代码:@Value("${pack.name}")privateStringname;当配置文件中没有配置pack.name时,服务启动时会报错。这时候我们可以通过设置默认值错误来防止。@Value("${pack.name:xx}")privateStringname;其中冒号“:”后面是默认值,这里也可以什么都不写${pack.name:}。加载指定环境的配置文件。一般我们会针对测试环境、开发环境、生产环境设置不同的配置文件,针对不同的环境加载不同的配置文件。有以下配置文件:生产环境加载prod文件,测试环境加载test文件,开发环境加载dev文件。只需在application.yml配置文件中添加如下配置即可。spring:profiles:active:-dev这个是硬编码在配置文件中的,我们也可以在命令行中制定java-jarxxxxx.jar--spring.profiles.active=dev通过include包含多个配置文件,即与环境相关不相关(即不管环境都会加载)spring:profiles:active:-devinclude:-cloud-secret配置文件加载顺序查看ConfigFileApplicationListener.java源码,里面定义了在哪里加载配置文件.加载顺序:file:./config/file:./config/*/file:./classpath:config/classpath:优先级从高到低,高优先于低。默认加载application.yml或application.properties文件。启动应用时,可以指定配置文件的名称java-jarxxxxx.jar--spring.config.name=myapp源码:通过代码读取配置文件@SpringBootApplicationpublicclassSpringBootJwtApplicationimplementsApplicationRunner{publicstaticvoidmain(String[]args){SpringApplication.run(SpringBootJwtApplication.class,args);}@Overridepublicvoidrun(ApplicationArgumentsargs)throwsException{ClassPathResourceresource=newClassPathResource("config.properties");try{Propertiesproperties=PropertiesLoaderUtils.loadProperties(resource);System.out.println(properties);}catch(IOExceptione){e.printStackTrace();}}}这里通过PropertiesLoaderUtils工具类加载资源!!!
