[JAVA] SpringBoot配置文件加载方法详细讲解

1947 0
王子 2022-11-9 09:20:24 | 显示全部楼层 |阅读模式
目录

    配置文件的读取顺序多坏境的配置文件个性化配置自定义配置文件名称和路径加载yml文件


配置文件的读取顺序

    根目录/config/application.properties根目录/config/application.yml根目录/application.properties根目录/application.ymlclasspath目录/config/application.propertiesclasspath目录/config/application.ymlclasspath目录/application.propertiesclasspath目录/application.yml


默认可读取的配置文件全部都会被读取合并,按照顺序读取配置,相同的配置项按第一次读取的值为准,同一个目录下properties文件比yml优先读取,通常会把配置文件放到classpath下,一般是resources里;

多坏境的配置文件

通常可以使用4个配置文件:(yml也同理)
    application.properties:默认配置文件application-dev.properties:开发环境配置文件application-prod.properties:生产环境配置文件application-test.properties:测试环境配置文件
在application.properties里配置spring.profiles.active以指定使用哪个配置文件,可以配置dev、prod、test分别对应以-dev、-prod、-test结尾的配置文件;(yml配置文件也是同理)
也可以在命令行使用spring.profiles.active指定,例如:java -jarxxxxxx.jar--spring.profiles.active=dev;

个性化配置

对于更特殊的个性化配置可以使用@Profile注解指定;
@Profile标签可以用在@Component或者@Configuration修饰的类上,可以标记类和方法,用来指定配置名字,然后使用spring.profiles.active指定该配置名字就可生效;
就像这样:
  1. package testspringboot.test2;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.context.annotation.Profile;
  5. @Configuration
  6. @Profile("myconfig")
  7. public class MyConfig {
  8.         @Bean("Tom")
  9.         @Profile("A")
  10.         public String a() {
  11.                 return "tomtom";
  12.         }
  13.         @Bean("Tom")
  14.         @Profile("B")
  15.         public String b() {
  16.                 return "TOMTOM";
  17.         }
  18.         @Bean("Tom")
  19.         public String c() {
  20.                 return "ttoomm";
  21.         }
  22. }
复制代码
然后写一个controller类:
  1. package testspringboot.test2;
  2. import javax.annotation.Resource;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.RestController;
  5. @RestController
  6. @RequestMapping("/test2controller")
  7. public class Test2Controller {
  8.         @Resource(name = "Tom")
  9.         public String t;
  10.         @RequestMapping("/test2")
  11.         public String test2() {
  12.                 System.out.println(t);
  13.                 return "TEST2" + t;
  14.         }
  15. }
复制代码
启动类:
  1. package testspringboot.test2;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class Test2Main {
  6.         /**
  7.          * @param args
  8.          */
  9.         public static void main(String[] args) {
  10.                 SpringApplication.run(Test2Main.class, args);
  11.         }
  12. }
复制代码
配置文件里配置:
server.port=8888
server.servlet.context-path=/testspringboot
spring.profiles.active=myconfig
只指定myconfig配置,则MyConfig类里c()的bean生效,访问结果是:


修改spring.profiles.active=myconfig,A,则MyConfig类里标记@Profile("A")的bean生效:


修改spring.profiles.active=myconfig,B,则标记@Profile("B")的bean生效:


如果去掉spring.profiles.active配置,则就找不到MyConfig里的配置了,启动失败:



自定义配置文件名称和路径

可以使用@PropertySource标签指定自定义的配置文件名称和路径;(默认能加载到的配置文件也会先被加载)


通常只会用到设置配置文件的名字,并且配置文件的名字可以随便定义,可以叫xxxx.properties、a.txt、b.abc等等,但是内容格式需要跟.properties一致,即kv格式,所以不能直接加载yml格式的配置文件;
@PropertySource默认加载路径是classpath下,可以使用classpath:xxxx/xxxx/xxxx.properties指定目录和文件,如果使用根目录则需要使用file:xxxx/xxxx/xxxx.properties;
可以使用@PropertySource为启动类指定springboot的配置文件,能够做到使用一个main方法启动两个springboot实例,并各自使用不同的配置文件:
  1. @SpringBootApplication
  2. @PropertySource("classpath:a.properties")
  3. @PropertySource(value = "file:a.properties", ignoreResourceNotFound = true)
  4. public class Test2Main {
  5.         /**
  6.          * @param args
  7.          */
  8.         public static void main(String[] args) {
  9.                 SpringApplication.run(Test2Main.class, args);
  10.         }
  11. }
复制代码
也可以使用@PropertySource配置bean,在使用@Component和@ConfigurationProperties时也可给bean指定特定配置文件:
放在resources下的配置文件tom.abc:
mybean.name=Tom
mybean.age=12
bean类ABC,配置tom.abc文件注入:
  1. package testspringboot.test2;
  2. import org.springframework.boot.context.properties.ConfigurationProperties;
  3. import org.springframework.context.annotation.PropertySource;
  4. import org.springframework.stereotype.Component;
  5. @Component
  6. @ConfigurationProperties("mybean")
  7. @PropertySource(value = "classpath:tom.abc")
  8. public class ABC {
  9.         public String name;
  10.         public int age;
  11.         public String getName() {
  12.                 return name;
  13.         }
  14.         public void setName(String name) {
  15.                 this.name = name;
  16.         }
  17.         public int getAge() {
  18.                 return age;
  19.         }
  20.         public void setAge(int age) {
  21.                 this.age = age;
  22.         }
  23.         @Override
  24.         public String toString() {
  25.                 return "ABC [name=" + name + ", age=" + age + "]";
  26.         }
  27. }
复制代码
启动类可以直接获得bean:
  1. package testspringboot.test2;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.context.ConfigurableApplicationContext;
  5. import org.springframework.context.annotation.PropertySource;
  6. @SpringBootApplication
  7. @PropertySource("classpath:a.properties")
  8. @PropertySource(value = "file:a.properties", ignoreResourceNotFound = true)
  9. public class Test2Main {
  10.         /**
  11.          * @param args
  12.          */
  13.         public static void main(String[] args) {
  14.                 ConfigurableApplicationContext ctx = SpringApplication.run(Test2Main.class, args);
  15.                 System.out.println(ctx.getBean(ABC.class));
  16.         }
  17. }
复制代码
启动结果:


可以直接获得配置的bean,也可以在代码里使用@Resource或者@Autowired获得;

加载yml文件

如果使用@PropertySource配置yml,则需要自定义一个factory实现:
  1. package testspringboot.test2;
  2. import java.io.IOException;
  3. import java.util.Properties;
  4. import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
  5. import org.springframework.core.env.PropertiesPropertySource;
  6. import org.springframework.core.env.PropertySource;
  7. import org.springframework.core.io.support.EncodedResource;
  8. import org.springframework.core.io.support.PropertySourceFactory;
  9. public class YmlPropertiesFactory implements PropertySourceFactory {
  10.         @Override
  11.         public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
  12.                 YamlPropertiesFactoryBean factoryBean = new YamlPropertiesFactoryBean();
  13.                 factoryBean.setResources(resource.getResource());
  14.                 factoryBean.afterPropertiesSet();
  15.                 Properties source = factoryBean.getObject();
  16.                 return new PropertiesPropertySource("myyml", source);
  17.         }
  18. }
复制代码
然后在@PropertySource里配置factory和yml文件:@PropertySource(value = "myapplication.yml", factory = YmlPropertiesFactory.class),就可以加载yml配置文件了;
到此这篇关于SpringBoot配置文件加载方法详细讲解的文章就介绍到这了,更多相关SpringBoot配置文件内容请搜索中国红客联盟以前的文章或继续浏览下面的相关文章希望大家以后多多支持中国红客联盟!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行