springboot 通过 properties,yaml 注入字符串,对象,list,特殊字符
1、properties 文件student.properties
# string
student.name=admin
# map
student.map[id]=5
student.map[age]=22
student.map[birthday]='2222-02-22'
# list
student.subject='java','c','c++'
maps={'a':'aa','b':'bb','c':'cc'}
注入实体类
@Data // 必须提供 get/set 方法
@Configuration // 或者 @Component
// 指定引用哪个配置文件. 在 application.yml 中使用了 spring.profiles.include 引用配置文件,可以不写下面的注解
@PropertySource(value = "classpath:student.properties")
@ConfigurationProperties(prefix = "student")
public class StudentBean {
private String name;
// 注意:封装对象不能使用@Value,下面的是特例
// 注意:成员变量名要和配置文件中配置的参数名一致
// 注意:不能直接给静态变量赋值,静态变量可以在 set 方法上使用 @Value 注入
private Map map;
private List subject;
@Value("#{${maps}}") // 注意这里的 #
private Map maps;
}
测试(我的 SpringbootTest 测试跑不起来,暂时在 main 方法测试)
public static void main(String[] args) {
ConfigurableApplicationContext configurableApplicationContext = SpringApplication.run(SxdlSzhgdsApiDwsApplication.class, args);
StudentBean studentBean = configurableApplicationContext.getBean(StudentBean.class);
System.out.println(studentBean.getName()); // admin
System.out.println(studentBean.getMap().get("birthday")); // '2222-02-22'
System.out.println(studentBean.getSubject().get(0)); // 'java'
System.out.println(studentBean.getMaps().get("a")); // aa
}
2、yam 或 yaml 文件
为什么要区分 properties 和 yaml: 因为 @PropertySource 默认只能解析 properties,不解析 yaml
添加如下解析类,参考 https://blog.csdn.net/cockroach02/article/details/105188955
// 继承DefaultPropertySourceFactory
public class YamlAndPropertySourceFactory extends DefaultPropertySourceFactory {
@Override
public PropertySource> createPropertySource(String name, EncodedResource resource) throws IOException {
if (resource == null) {
return super.createPropertySource(name, resource);
}
Resource resourceResource = resource.getResource();
if (!resourceResource.exists()) {
return new PropertiesPropertySource(null, new Properties());
} else if (resourceResource.getFilename().endsWith(".yml") || resourceResource.getFilename().endsWith(".yaml")) {
List> sources = new YamlPropertySourceLoader().load(resourceResource.getFilename(), resourceResource);
return sources.get(0);
}
return super.createPropertySource(name, resource);
}
}
student.yaml
注意这个下面 * 就是特殊字符,如果不使用 [],则注入不进去,并且需要添加双引号
# string
student:
name: admin
# map
map:
id: 5
age: 22
birthday: '2222-02-22'
'[*]': '☀'
# list
subject: ['java','c','c++']
maps: {'a':'aa','b':'bb','c':'cc'} # 注意这个缩进, student.maps,和 properties 不同
注入实体类
@Data // 必须提供 get/set 方法
@Configuration // 或者 @Component
// 指定自定义的解析类,否则对象数组复杂类型注入不进去
// 如果将 student.yaml 配置添加到 application.yaml 可以注入,不需要添加解析类
@PropertySource(value = "classpath:student.yaml",factory = YamlAndPropertySourceFactory.class)
@ConfigurationProperties(prefix = "student")
public class StudentBean {
private String name;
private Map map;
private List subject;
private Map maps;
}
测试(我的 SpringbootTest 测试跑不起来,暂时在 main 方法测试)
public static void main(String[] args) {
ConfigurableApplicationContext configurableApplicationContext = SpringApplication.run(SxdlSzhgdsApiDwsApplication.class, args);
StudentBean studentBean = configurableApplicationContext.getBean(StudentBean.class);
System.out.println(studentBean.getName()); // admin
System.out.println(studentBean.getMap()); // {id=5, age=22, birthday=2222-02-22, *=☀}
System.out.println(studentBean.getMap().get("birthday")); // '2222-02-22'
System.out.println(studentBean.getSubject().get(0)); // 'java'
System.out.println(studentBean.getMaps().get("a")); // aa
}



