如何以编程方式访问 Spring Boot 应用程序的名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31429761/
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 to access a Spring Boot Application's name programmatically?
提问by yathirigan
I've defined an application name using the bootstrap.yml file in my spring boot application.
我在 Spring Boot 应用程序中使用 bootstrap.yml 文件定义了一个应用程序名称。
spring:
application:
name: abc
How can i get this application name during runtime/programmatically ?
如何在运行时/以编程方式获取此应用程序名称?
回答by Daniel Cottone
You should be able to use the @Value annotation to access any property you set in a properties/YAML file:
您应该能够使用 @Value 注释来访问您在 properties/YAML 文件中设置的任何属性:
@Value("${spring.application.name}")
private String appName;
回答by Artem Bilan
@Autowired
private ApplicationContext applicationContext;
...
this.applicationContext.getId();
Please, find this:
请找到这个:
# IDENTITY (ContextIdApplicationContextInitializer)
spring.application.name=
spring.application.index=
In Spring Boot Reference Manual.
在 Spring Boot参考手册中。
And follow with source code for that ContextIdApplicationContextInitializer
class:
并遵循ContextIdApplicationContextInitializer
该类的源代码:
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
applicationContext.setId(getApplicationId(applicationContext.getEnvironment()));
}
Where the default behavior is with this:
默认行为是这样的:
/**
* Placeholder pattern to resolve for application name
*/
private static final String NAME_PATTERN = "${vcap.application.name:${spring.application.name:${spring.config.name:application}}}";
回答by Roland Roos
Note! If your using a SpringBootTest, you need to suplly the properties/yml. Otherwise, the environment/appcontext does not load the config files. The, your app name is not set. Like so:
笔记!如果您使用 SpringBootTest,则需要提供 properties/yml。否则,环境/应用程序上下文不会加载配置文件。未设置您的应用程序名称。像这样:
@PropertySource("classpath:application.properties")
@RunWith(SpringRunner.class)
@SpringBootTest
....
回答by loesak
Since the @Value
annotation is discouraged in Spring Boot when referencing configuration properties, and because applicationContext.getId();
doesn't always return the value of spring.application.name
another way is to get the value from the Environment directly
由于@Value
在 Spring Boot 中不鼓励在引用配置属性时使用注解,并且因为applicationContext.getId();
不总是返回值的spring.application.name
另一种方式是直接从 Environment 获取值
private final Environment environment;
...
public MyBean(final Environment environment) {
this.environment = environment;
}
...
private getApplicationName() {
return this.environment.get("spring.application.name");
}
Another possible way would be to create your own ConfigurationProperties class to get access to the value.
另一种可能的方法是创建您自己的 ConfigurationProperties 类来访问该值。
I'm not saying these are the best ways, and I hope/wish that there is a better way, but it is a way.
我并不是说这些是最好的方法,我希望/希望有更好的方法,但这是一种方法。