SpringBoot获取配置文件内容的几种方式总结
作者:CV大魔王 时间:2023-11-24 18:10:48
前言
自从用了SpringBoot,个人最喜欢的就是SpringBoot的配置文件了,和Spring比起SpringBoot更加灵活,修改的某些配置也是更加得心应手。 SpringBoot官方提供了两种常用的配置文件格式,分别是properties和YML格式。相比于properties来说,YML更加年轻,层级也是更加分明,不过本篇文章的重点是如何获取配置文件的内容。
现有配置文件如下,如何获取到配置文件的值呢?
file:
windows: D:\file
linux: /usr/local/file
方法1:@ConfigurationProperties
首先,可以标注到实体类上。
@Data
@Component
@ConfigurationProperties(prefix = "file")
public class FileProperties {
private String windows;
private String linux;
}
标注在配置类上的方法上,同样是从配置文件中取值赋值到返回值的属性中。使用如下:
@Bean
@ConfigurationProperties(prefix = "userinfo")
public FileProperties fileProperties() {
return new FileProperties();
}
使用方法:
@Service
public class Test {
@Autowired
private FileProperties fileProperties;
@Override
public void test() {
System.out.println(fileProperties.getLinux());
}
}
总结
@ConfigurationProperties注解能够很轻松的从配置文件中取值,优点如下:
支持批量的注入属性,只需要指定一个前缀 prefix
支持复杂的数据类型,比如 List 、 Map
对属性名匹配的要求较低,比如user-name,user_name,userName,USER_NAME 都可以取值
支持JAVA的JSR303数据校验
方法2:@Value
@Value("${file.windows}")
private String windows;
@Value("${file.linux}")
private String linux;
如何从自定义配置文件中取值?
Spring Boot在启动的时候会自动加载 application.xxx 和 bootsrap.xxx ,但是为了区分,有时候需要自 定义一个配置文件,那么如何从自定义的配置文件中取值呢?
只需要在启动类上标注 @PropertySource 并指定你自定义的配置文件即可完成。
@SpringBootApplication
@PropertySource(value = {"classpath:custom.properties"})
public class DemoApplication{ }
value
属性是一个数组,可以指定多个配置文件同时引入。@PropertySource
默认加载xxx.properties
类型的配置文件,不能加载YML
格式的配置文件。
如何加载自定义YML格式的配置文件?
@PropertySource注解有一个属性 factory ,默认值是PropertySourceFactory.class,这个就是用来加 载properties格式的配置文件,我们可以自定义一个用来加载 YML 格式的配置文件,如下:
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.DefaultPropertySourceFactory;
import org.springframework.core.io.support.EncodedResource;
import java.io.IOException;
import java.util.Properties;
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();
}
}
此时只需要将 factory 属性指定为 YmlConfigFactory 即可,如下:
@SpringBootApplication
@PropertySource(value = {"classpath:custom.yml"}, factory = YmlConfigFactory.class)
public class DemoApplication { }
@PropertySource 指定加载自定义的配置文件,默认只能加载 properties 格式,但是可以指定 factory 属 性来加载 YML 格式的配置文件。
总结
来源:https://blog.csdn.net/qq_45740561/article/details/129132933
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Springboot 全局时间格式化操作
![](https://img.aspxhome.com/file/2023/2/94332_0s.jpg)
Android系统中的蓝牙连接程序编写实例教程
Android自定义有限制区域的图例角度自识别涂鸦工具类完结篇
![](https://img.aspxhome.com/file/2023/9/138509_0s.jpg)
解决spring boot启动扫描不到自定义注解的问题
C#短时间内产生大量不重复的随机数
Java中try catch处理异常示例
Android实现清除单个域名的cookie
android中Fragment+RadioButton实现底部导航栏
![](https://img.aspxhome.com/file/2023/3/139633_0s.png)
Java 数据结构深入理解ArrayList与顺序表
![](https://img.aspxhome.com/file/2023/1/62501_0s.png)
IntelliJ IDEA修改新建文件自动生成注释的user名
![](https://img.aspxhome.com/file/2023/8/69308_0s.png)
IDEA中设置代码自动提示为Alt+/的具体做法
![](https://img.aspxhome.com/file/2023/9/69889_0s.jpg)
通过spring boot 设置tomcat解决 post参数限制问题
Java 六类运算符详解
解析Java的设计模式编程之解释器模式的运用
![](https://img.aspxhome.com/file/2023/5/63755_0s.jpg)
Android拼图游戏 玩转从基础到应用手势变化
![](https://img.aspxhome.com/file/2023/7/95967_0s.gif)
Android应用中Back键的监听及处理实例
Java基础之堆内存溢出的解决
![](https://img.aspxhome.com/file/2023/8/59338_0s.png)
Android中TelephonyManager类的方法实例分析
关于IDEA配置文件字符集的问题
![](https://img.aspxhome.com/file/2023/2/85502_0s.png)