1. 程式人生 > >Spring Boot讀取配置檔案的幾種方式

Spring Boot讀取配置檔案的幾種方式

Spring Boot獲取檔案總的來說有三種方式,分別是@Value註解,@ConfigurationProperties註解和Environment介面。這三種註解可以配合著@PropertySource來使用,@PropertySource主要是用來指定具體的配置檔案。 ## @PropertySource解析 ```java @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 factory() default PropertySourceFactory.class; } ``` - value():指定配置檔案 - encoding():指定編碼,因為properties檔案的編碼預設是ios8859-1,讀取出來是亂碼 - factory():自定義解析檔案型別,因為該註解預設只會載入properties檔案,如果想要指定yml等其他格式的檔案需要自定義實現。 ## 一、@Value註解讀取檔案 新建兩個配置檔案config.properties和configs.properties,分別寫入如下內容: ```properties zhbin.config.web-configs.name=Java旅途 zhbin.config.web-configs.age=22 ``` ```properties zhbin.config.web-configs.name=Java旅途 zhbin.config.web-configs.age=18 ``` 新增一個類用來讀取配置檔案 ```java @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。程式碼如下: ```java 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(); } } ``` ```java @PropertySource(value = {"classpath:config.properties"},encoding="gbk",factory = YmlConfigFactory.class) ``` ## 二、Environment讀取檔案 配置檔案我們繼續用上面的兩個,定義一個類去讀取配置檔案 ```java @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檔案也沒問題 ```yml zhbin: config: web-configs: name: Java旅途 age: 20 ``` 新建實體類用來對映該配置 ```java @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; } } ``` - **prefix = "zhbin.config"用來指定配置檔案字首** 如果需要獲取list集合,則做以下修改即可。 ```yml zhbin: config: web-configs: - name: Java旅途 age: 20 - name: Java旅途2 age: 202 ``` ```java @Component @ConfigurationProperties(prefix = "zhbin.config") @Data public class StudentYml { priv