SpringBoot使用@Value注入静态属性
作者:互联网
说明:SpringBoot中使用yml文件配置自定义属性,读取配置文件属性注入到实体类中,属性值都为静态属性
配置文件:yml
#用户信息 userMag: userName: "王小波" userAge: "21"
工具实体类:UserConstant
@Component public class UserConstant { private static String userName; @Value("${userMag.userName}") private void setUserName(String userName) {//注入值 UserConstant.userName = userName; } public static String getUserName() {//获取值 return userName; } private static String userAge; @Value("${userMag.userAge}") private void setUserAge(String userAge) {//注入值 UserConstant.userAge = userAge; } public static String getUserAge() {//获取值 return userAge; } }
项目启动,属性调用
控制台结果输出
标签:userName,UserConstant,SpringBoot,静态,Value,userAge,static,private,String 来源: https://www.cnblogs.com/bgyb/p/14945489.html