SpringBoot獲取配置文件內容的幾種方式總結
前言
自從用了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注解能夠很輕松的從配置文件中取值,優(yōu)點如下:
- 支持批量的注入屬性,只需要指定一個前綴 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 ,但是為了區(qū)分,有時候需要自 定義一個配置文件,那么如何從自定義的配置文件中取值呢?
只需要在啟動類上標注 @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 格式的配置文件。
總結
到此這篇關于SpringBoot獲取配置文件內容的幾種方式總結的文章就介紹到這了,更多相關SpringBoot獲取配置文件內容內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Spring Boot集成Quartz注入Spring管理的類的方法
本篇文章主要介紹了Spring Boot集成Quartz注入Spring管理的類的方法,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04
SpringBoot重寫addResourceHandlers映射文件路徑方式
這篇文章主要介紹了SpringBoot重寫addResourceHandlers映射文件路徑方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02
關于Java異常處理的幾條建議_動力節(jié)點Java學院整理
Java提供了拋出異常、捕捉異常和finally語句的使用來處理程序異常,下面就來具體看一下關于Java異常處理的幾條建議2017-06-06

