spring boot @value @ConfigurationProperties 注释静态配置文件变量引用
1.@value
错误用法:
@Value(value = "${user.userName}")
private static String userName;
正确用法:
标签加在set方法上面,注意set方法是非静态
private static String userName;
@Value(value = "${user.userName}")
public void setUserName(String userName) {
HelloWorldServiceImpl.userName = userName;
}
2.@ConfigurationProperties
错误用法:
@ConfigurationProperties(prefix = "author")
public class MyConfig {
private static String name;
}
正确用法:
建立非静态set方法,get方法位静态
@ConfigurationProperties(prefix = "author")
public class MyConfig {
private static String name;
private static String height;
public static String getName() {
return name;
}
public MyConfig setName(String name) {
this.name = name;
return this;
}
}