您不能/不应该更改 Java 中枚举的值。尝试改用一个类:
public class MyCustomProperty {
// can't change this in application.properties
private final String lowerCase;
// can change this in application.properties
private String atitude;
private long someNumber;
public MyCustomProperty (String lowerCase, String atitude, long someNumber) {
this.lowerCase = lowerCase;
this.atitude = atitude;
this.someNumber = someNumber;
}
// getter and Setters
}
比创建自定义ConfigurationProperties:
@ConfigurationProperties(prefix="my.config")
public class MyConfigConfigurationProperties {
MyCustomProperty name = new MyCustomProperty("name", "good", 100);
MyCustomProperty fame = new MyCustomProperty("fame", "good", 100);
// getter and Setters
// You can also embed the class MyCustomProperty here as a static class.
// For details/example look at the linked SpringBoot Documentation
}
现在您可以在 application.properties 文件中更改 my.config.name.someNumber 和 my.config.fame.someNumber 的值。如果您想禁止更改小写/态度,请将它们设为最终版本。
在使用它之前,您必须使用 @EnableConfigurationProperties(MyConfigConfigurationProperties.class) 注释 @Configuration 类。还要添加 org.springframework.boot:spring-boot-configuration-processor 作为可选依赖项以获得更好的 IDE 支持。
如果你想访问这些值:
@Autowired
MyConfigConfigurationProperties config;
...
config.getName().getSumeNumber();