如何从组件访问标头变量?

时间:2018-12-22 18:50:46

标签: spring-boot

具有包含多个变量的banner.txt,例如${application.formatted-version}。如何从Java组件访问相同的变量?

我尝试像这样使用@Value

@Value("${application.formatted-version}")
private String applicationVersion;

但是编译器说:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'MainComponent': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'application.formatted-version' in value "${application.formatted-version}"

1 个答案:

答案 0 :(得分:0)

您的问题需要更多详细信息。在Spring中,如果要从配置文件访问变量,则需要将该变量移至application.properties,在那里必须设置其值,然后可以使用@Value批注从Component对其进行访问。 。 有关如何在https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

下的Spring Boot中执行此操作的详细信息