(一)yml配置文件:
pom.xml加入依赖:
org.springframework.boot spring-boot-configuration-processor${spring-boot.version}
在application.yml文件中加上:
#自定义的属性和值 myYml: simpleProp: simplePropValue arrayProps: 1,2,3,4,5 listProp1: - name: abc value: abcValue - name: efg value: efgValue listProp2: - config2Value1 - config2Vavlue2 mapProps: key1: value1 key2: value2
使用一个java类获取yml文件的内容:
package com.sun.configuration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component
//@ConfigurationProperties(locations = {"classpath:config/myProps.yml"},prefix = "myProps")
@ConfigurationProperties(prefix = "myYml")
public class YmlConfig {
String simpleProp;
private String[] arrayProps;
private List
通过依赖注入就可以获取该对象:
@Autowired private YmlConfig config;
方法内获取值:
ObjectMapper objectMapper = new ObjectMapper();
//测试加载yml文件
System.out.println("simpleProp: " + config.getSimpleProp());
System.out.println("arrayProps: " + objectMapper.writevalueAsString(config.getArrayProps()));
System.out.println("listProp1: " + objectMapper.writevalueAsString(config.getListProp1()));
System.out.println("listProp2: " + objectMapper.writevalueAsString(config.getListProp2()));
System.out.println("mapProps: " + objectMapper.writevalueAsString(config.getMapProps()));
(二)properties配置文件:
使用@PropertySource注解加载配置文件,该注解无法加载yml配置文件。使用@Value注解获得文件中的参数值
package com.sun.configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
@Configuration
@PropertySource(value = {"classpath:/config/propConfigs.properties","classpath:/config/abc.properties"},
ignoreResourceNotFound = true,encoding = "utf-8")
public class PropConfig {
// PropertySourcesPlaceholderConfigurer这个bean,
// 这个bean主要用于解决@value中使用的${…}占位符。
// 假如你不使用${…}占位符的话,可以不使用这个bean。
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
//获取properties文件参数值有两种方法,一种获得Environment 的对象,第二种就是@Value注解
@Autowired
private Environment env;
@Value("${age}")
String name;
@RequestMapping("/")
@ResponseBody
String home(HttpServletRequest req) throws JsonProcessingException, UnsupportedEncodingException {
logger.info("测试通过!!!");
ObjectMapper objectMapper = new ObjectMapper();
//测试加载yml文件
System.out.println("simpleProp: " + config.getSimpleProp());
System.out.println("arrayProps: " + objectMapper.writevalueAsString(config.getArrayProps()));
System.out.println("listProp1: " + objectMapper.writevalueAsString(config.getListProp1()));
System.out.println("listProp2: " + objectMapper.writevalueAsString(config.getListProp2()));
System.out.println("mapProps: " + objectMapper.writevalueAsString(config.getMapProps()));
//测试加载properties文件
System.out.println(env.getProperty("name"));//孙凯
System.out.println(env.getProperty("abc"));//null
System.out.println(name);//26
return "Hello World!";
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



