如何优雅的获取properties文件中的数据(@Value、Environment、@ConfigurationProperties)

如何获取properties文件中的数据

在工作中,无论是Spring MVC还是Spring Boot我们都没办法避免与properties打交道,我们总是将一些配置式的代码写在properties文件中,然后通过@Value 去获取

@Value

例:
application.properties 文件:

data.jerry.name: testName

java代码:

	@Value("${data.jerry.name}")
	private String jerryName;
	@Value("${data.jerry.friends}")
	private List<String> friends;

	@ResponseBody
	@RequestMapping("/test")
	public void printUser() {
		System.out.println("userName: " + jerryName + "#friends: " + JSONObject.toJSON(friends));
	}

此时打印为 userName: testName#friends: [“zhangsan,lisi,wangwu”]

为了获取application.properties文件中的data.jerry.name和friends属性,我们通过@Value来获取,这样的获取方式在日常的工作中是完全够用的! 只是当需要获取的属性变的更多的时候,我们就会被迫的写很多类似的,很长一串"没营养"的代码.并且当data.jerry.name在配置文件不存在的时候,启动还会报错! 这个时候我们可以使用Environment类

Environment

例:

	@Autowired
	private Environment environment;

	@ResponseBody
	@RequestMapping("/test")
	public void printUser() {
		System.out.println("userName:" + environment.getProperty("data.jerry.name", "undefined") + "#friends:"
				+ environment.getProperty("data.jerry.friends", new ArrayList<String>().getClass()));
	}

此时打印为 userName:testName#friends:[zhangsan, lisi, wangwu], 如果找不到data.jerry.name,则打印undefined

通过Environment类获取还可以设置默认值,当找不到的时候返回默认值,也是很好用的啊! 但是如果还嫌麻烦呢? 毕竟每次使用,我们都需要重新获取啊,而且每次获取的重复代码也不少呢, 这个时候我们可以使用SpringBoot的@ConfigurationProperties注解

@ConfigurationProperties

例:
applicationproperties文件:

data.jerry.name: testName
data.jerry.passwork: testPasswork
data.jerry.birth: 1994/10/9
data.jerry.friends: zhangsan,lisi,wangwu
data.jerry.cat.name: blackCat
data.jerry.cat.age: 1
data.jerry.girl-friend.name: xiaohong
data.jerry.girl-friend.age:18
data.jerry.families[0].name: father
data.jerry.families[0].age: 53
data.jerry.families[1].name: mother
data.jerry.families[1].age: 51

如果我们要要用@Value的方式去获取的话,将会有很多重复的代码!!! 并且这些代码都是没有意义的,而通过Environment获取,我们不封装一下的话,获取的时候也比较麻烦,这个时候我们可以使用ConfigurationProperties,一个SpringBoot已经帮我们封装的好的注解

Jerry类:

@Component
@ConfigurationProperties(prefix = "data.jerry")
public class Jerry {

	private String name;
	private String passwork;
	private Date birth;
	private List<String> friends;
	private Map<String, Object> cat;
	private GirlFriend girlFriend;
	private List<Family> families;
	get/set省略...

GirlFriend类:

public class GirlFriend {

	private String name;
	private int age;
	get/set省略...

Family类:

public class Family{

	private String name;
	private int age;
	get/set省略...

最后,我们在使用的使用,直接注入Jerry类就可以获取到application.properties文件中符合的映射属性

	@Autowired
	private Environment environment;
	@Autowired
	private Jerry user;

	@ResponseBody
	@RequestMapping("/test")
	public void printUser() {
		System.out.println(JSONObject.toJSON(user));
	}

最后打印为 :
{“name”:“testName”,“birth”:781632000000,“passwork”:“testPasswork”,
“girlFriend”:{“name”:“xiaohong”,“age”:18},
“cat”:{“name”:“blackCat”,“age”:“1”},
“families”:[{“name”:“father”,“age”:53},{“name”:“mother”,“age”:51}],
“friends”:[“zhangsan”,“lisi”,“wangwu”]}

是不是很好用? 而且@ConfigurationProperties 还可以通过@NotNull、@Min、@Max限制属性的最大最小值和不能为空,这个很值得强推啊!!! 其实说一千道一万,这些方式都能获取application.properties文件中的值,我们只需要挑选其中最适合我们的一种方式,这样才不至于,当你看着这些曾经自己写的代码时会看不下去了~

最后:

1、只有当前组件是容器中的组件,才能使用容器提供的@ConfigurationPropeities功能

2、使用@ConfigurationProperties,被注入字段必须要有set 、get 方法

3、prefix表达式中可以使用 点来表示yml中的层级

4、通常需要通过配置文件注入复杂类型,如对象、List 、Map是优先考虑使用@ConfigurationProperties ,只是在某个业务逻辑找那个获取一下配置文件中的某项值就直接用@Value就可以了

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值