java 在 bootstrap.properties 中定义时如何访问“spring.application.name”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41155203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I access 'spring.application.name' when defined in bootstrap.properties?
提问by Israel Fernández
I have the following spring-boot 1.4.2.RELEASE sample app
我有以下 spring-boot 1.4.2.RELEASE 示例应用程序
@SpringBootApplication
public class Application {
@Value("${spring.application.name}")
private String applicationName;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
And I have the following configuration defined in bootstrap.properties:
我在 bootstrap.properties 中定义了以下配置:
spring.application.name=sample-app
When run it I get the following error:
运行它时,我收到以下错误:
java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.application.name' in string value "${spring.application.name}"
Any hint on why it fails to inject 'spring.application.name'? Need to define it there to support other spring boot cloud.
关于为什么它无法注入“spring.application.name”的任何提示?需要在那里定义它以支持其他 spring boot 云。
采纳答案by DannielWhatever
The first answer is correct. The default properties file is application.properties or application.yml.
第一个答案是正确的。默认属性文件是 application.properties 或 application.yml。
The bootstrap file is properly for Spring Cloud.
引导文件适用于 Spring Cloud。
See http://projects.spring.io/spring-cloud/spring-cloud.html#_the_bootstrap_application_context
见http://projects.spring.io/spring-cloud/spring-cloud.html#_the_bootstrap_application_context
If you are using spring cloud, and the bootstrap file is not working, you need to enable the "cloud" Spring profile.
如果您使用的是 spring cloud,并且引导文件不起作用,则需要启用“cloud”Spring 配置文件。
For example using:
例如使用:
./gradlew -Dspring.profiles.active=cloud bootrun
or
或者
./mvnw spring-boot:run -Dspring.profiles.active=cloud
回答by Duy Nguyen
By default, if you don't specify any properties source, spring boot will lookup for your property in the file application.properties. Therefore, you should rename your property file to that default name or manually specify a properties source for your bootstrap.properties
默认情况下,如果您没有指定任何属性源,spring boot 将在文件中查找您的属性application.properties。因此,您应该将属性文件重命名为该默认名称或手动为 bootstrap.properties 指定属性源

