[英]How to access the properties from application.properties to another custom class?
我正在尝试在 java 中的 application.properties 文件中定义一个属性,并尝试在我创建的另一个自定义类中访问它,但它没有获取属性。 我使用了 @ConfigurationProperties("xxx") 但它不起作用。
您可以使用Value注释而不是CofigurationProperties
。
应用程序属性
app.name = HelloWorld
使用Value
注释将属性注入到 bean 中:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class HelloWorldService {
@Value("${app.name}")
private String applicationName;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.