int Activation 和 int ForgotPassword 效果很好,但字符串变量返回 null 我需要 AppSettings 的静态类
@PropertySource("classpath:appSettings.properties")
public class AppSettings {
@Value("${Activation}")
private static int Activation;
@Value("${ForgotPassword}")
private static int ForgotPassword;
@Value("${CryptoSplit}")
private static String CryptoSplit;
@Value("${CryptoKey}")
private static String CryptoKey;
public static String getCryptoSplit() {
return CryptoSplit;
}
public static String getCryptoKey() {
return CryptoKey;
}
public static int getActivation() {
return Activation;
}
public static int getForgotPassword() {
return ForgotPassword;
}
}
.属性
Activation=0
ForgotPassword=1
CryptoSplit=:OSK:
CryptoKey=TheBestSecretKey
最佳答案
Spring 不支持在静态字段上注入(inject)@Value
。
您确定您确实需要“AppSettings 的静态类”吗?我怀疑这可能代表了对 Spring 单例工作方式的误解。
但是,如果您确实需要“AppSettings 的静态类”,那么您可以按如下方式实现:
@Value("${CryptoKey}")
public void setCryptoKey(String cryptoKey) {
AppSettings.CryptoKey = CryptoKey;
}
关于java - @PropertySource @Value 静态字符串返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46219065/