SpringBoot获取配置文件内容的方式有哪些

其他教程   发布日期:2023年06月27日   浏览次数:490

这篇文章主要介绍“SpringBoot获取配置文件内容的方式有哪些”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“SpringBoot获取配置文件内容的方式有哪些”文章能帮助大家解决问题。

前言

现有配置文件如下,如何获取到配置文件的值呢?

  1. file:
  2. windows: D:file
  3. linux: /usr/local/file

方法1:@ConfigurationProperties

首先,可以标注到实体类上。

  1. @Data
  2. @Component
  3. @ConfigurationProperties(prefix = "file")
  4. public class FileProperties {
  5. private String windows;
  6. private String linux;
  7. }

标注在配置类上的方法上,同样是从配置文件中取值赋值到返回值的属性中。使用如下:

  1. @Bean
  2. @ConfigurationProperties(prefix = "userinfo")
  3. public FileProperties fileProperties() {
  4. return new FileProperties();
  5. }

使用方法:

  1. @Service
  2. public class Test {
  3. @Autowired
  4. private FileProperties fileProperties;
  5. @Override
  6. public void test() {
  7. System.out.println(fileProperties.getLinux());
  8. }
  9. }

总结

@ConfigurationProperties注解能够很轻松的从配置文件中取值,优点如下:

  • 支持批量的注入属性,只需要指定一个前缀 prefix

  • 支持复杂的数据类型,比如 List 、 Map

  • 对属性名匹配的要求较低,比如user-name,user_name,userName,USER_NAME 都可以取值

  • 支持JAVA的JSR303数据校验

方法2:@Value

  1. @Value("${file.windows}")
  2. private String windows;
  3. @Value("${file.linux}")
  4. private String linux;

如何从自定义配置文件中取值?

Spring Boot在启动的时候会自动加载 application.xxx 和 bootsrap.xxx ,但是为了区分,有时候需要自 定义一个配置文件,那么如何从自定义的配置文件中取值呢?

只需要在启动类上标注 @PropertySource 并指定你自定义的配置文件即可完成。

  1. @SpringBootApplication
  2. @PropertySource(value = {"classpath:custom.properties"})
  3. public class DemoApplication{ }

  1. value
属性是一个数组,可以指定多个配置文件同时引入。
  1. @PropertySource
默认加载
  1. xxx.properties
类型的配置文件,不能加载
  1. YML
格式的配置文件。

如何加载自定义YML格式的配置文件?

@PropertySource注解有一个属性 factory ,默认值是PropertySourceFactory.class,这个就是用来加 载properties格式的配置文件,我们可以自定义一个用来加载 YML 格式的配置文件,如下:

  1. import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
  2. import org.springframework.core.env.PropertiesPropertySource;
  3. import org.springframework.core.env.PropertySource;
  4. import org.springframework.core.io.support.DefaultPropertySourceFactory;
  5. import org.springframework.core.io.support.EncodedResource;
  6. import java.io.IOException;
  7. import java.util.Properties;
  8. public class YmlConfigFactory extends DefaultPropertySourceFactory {
  9. @Override
  10. public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws
  11. IOException {
  12. String sourceName = name != null ? name : resource.getResource().getFilename();
  13. if (!resource.getResource().exists()) {
  14. return new PropertiesPropertySource(sourceName, new Properties());
  15. } else if (sourceName.endsWith(".yml") || sourceName.endsWith(".yaml")) {
  16. Properties propertiesFromYaml = loadYml(resource);
  17. return new PropertiesPropertySource(sourceName, propertiesFromYaml);
  18. } else {
  19. return super.createPropertySource(name, resource);
  20. }
  21. }
  22. private Properties loadYml(EncodedResource resource) throws IOException {
  23. YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
  24. factory.setResources(resource.getResource());
  25. factory.afterPropertiesSet();
  26. return factory.getObject();
  27. }
  28. }

此时只需要将 factory 属性指定为 YmlConfigFactory 即可,如下:

  1. @SpringBootApplication
  2. @PropertySource(value = {"classpath:custom.yml"}, factory = YmlConfigFactory.class)
  3. public class DemoApplication { }

@PropertySource 指定加载自定义的配置文件,默认只能加载 properties 格式,但是可以指定 factory 属 性来加载 YML 格式的配置文件。

以上就是SpringBoot获取配置文件内容的方式有哪些的详细内容,更多关于SpringBoot获取配置文件内容的方式有哪些的资料请关注九品源码其它相关文章!