我需要创建像共享类这样的东西,我可以通过以下方式使用它:
Shared.getProperty(key);
我尝试使用环境对象,但它始终为空。应该在哪里指定以及如何指定? 我使用 .xml 作为我的 bean 配置。 我还有 application.properties,我想从中检索数据。
最佳答案
// Shared.java
@Component
@ConfigurationProperties("prefix.for.application.properties")
public class Shared {
private String str;
// getters, setters
}
// application.properties
prefix.for.application.properties.str=STR
// other code
@Autovired
private Shared shared;
shared.getStr();
关于java - 如何使用 Spring 创建配置类并在整个代码中共享数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54389739/