Spring Boot读取配置文件的方式有哪些

发布时间:2021-12-17 13:44:59 作者:iii
来源:亿速云 阅读:154

这篇文章主要介绍“Spring Boot读取配置文件的方式有哪些”,在日常操作中,相信很多人在Spring Boot读取配置文件的方式有哪些问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Spring Boot读取配置文件的方式有哪些”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

Spring Boot获取文件总的来说有三种方式,分别是@Value注解,@ConfigurationProperties注解和Environment接口。这三种注解可以配合着@PropertySource来使用,@PropertySource主要是用来指定具体的配置文件。

@PropertySource解析

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(PropertySources.class)
public @interface PropertySource {
   
String name() default "";

String[] value();

boolean ignoreResourceNotFound() default false;

String encoding() default "";

Class<? extends PropertySourceFactory> factory() default PropertySourceFactory.class;
}

Spring Boot读取配置文件的方式有哪些

一、@Value注解读取文件

新建两个配置文件config.properties和configs.properties,分别写入如下内容:

zhbin.config.web-configs.name=Java旅途
zhbin.config.web-configs.age=22
zhbin.config.web-configs.name=Java旅途
zhbin.config.web-configs.age=18

新增一个类用来读取配置文件

@Configuration
@PropertySource(value = {"classpath:config.properties"},encoding="gbk")
public class GetProperties {

   @Value("${zhbin.config.web-configs.name}")
   private String name;
   @Value("${zhbin.config.web-configs.age}")
   private String age;

   public String getConfig() {
       return name+"-----"+age;
   }
}

如果想要读取yml文件,则我们需要重写DefaultPropertySourceFactory,让其加载yml文件,然后在注解

@PropertySource上自定factory。代码如下:

public class YmlConfigFactory extends DefaultPropertySourceFactory {
   @Override
   public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
       String sourceName = name != null ? name : resource.getResource().getFilename();
       if (!resource.getResource().exists()) {
           return new PropertiesPropertySource(sourceName, new Properties());
       } else if (sourceName.endsWith(".yml") || sourceName.endsWith(".yaml")) {
           Properties propertiesFromYaml = loadYml(resource);
           return new PropertiesPropertySource(sourceName, propertiesFromYaml);
       } else {
           return super.createPropertySource(name, resource);
       }
   }

   private Properties loadYml(EncodedResource resource) throws IOException {
       YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
       factory.setResources(resource.getResource());
       factory.afterPropertiesSet();
       return factory.getObject();
   }
}
@PropertySource(value = {"classpath:config.properties"},encoding="gbk",factory = YmlConfigFactory.class)

二、Environment读取文件

配置文件我们继续用上面的两个,定义一个类去读取配置文件

@Configuration
@PropertySource(value = {"classpath:config.properties"},encoding="gbk")
public class GetProperties {

   @Autowired
   Environment environment;

   public String getEnvConfig(){
       String name = environment.getProperty("zhbin.config.web-configs.name");
       String age = environment.getProperty("zhbin.config.web-configs.age");
       return name+"-----"+age;
   }
}

三、@ConfigurationProperties读取配置文件

@ConfigurationProperties可以将配置文件直接映射成一个实体类,然后我们可以直接操作实体类来获取配置文件相关数据。

新建一个yml文件,当然properties文件也没问题

zhbin:
 config:
   web-configs:
     name: Java旅途
     age: 20

新建实体类用来映射该配置

@Component
@ConfigurationProperties(prefix = "zhbin.config")
@Data
public class StudentYml {

   // webConfigs务必与配置文件相对应,写为驼峰命名方式
   private WebConfigs webConfigs = new WebConfigs();

   @Data
   public static class WebConfigs {
       private String name;
       private String age;
   }
}

如果需要获取list集合,则做以下修改即可。

zhbin:
 config:
   web-configs:
     - name: Java旅途
       age: 20
     - name: Java旅途2
       age: 202
@Component
@ConfigurationProperties(prefix = "zhbin.config")
@Data
public class StudentYml {

   private List<WebConfigs> webConfigs = new ArrayList<>();

   @Data
   public static class WebConfigs {

       private String name;
       private String age;
   }
}

经验与坑

到此,关于“Spring Boot读取配置文件的方式有哪些”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. spring cloud config将配置存储在数据库中
  2. 开启Spring Boot的方式有哪些

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

spring boot

上一篇:@ApiImplicitParam对@RequestParam的required属性侵入怎么解决

下一篇:如何进行springboot配置templates直接访问的实现

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》