[英]How can I get context-path and info.info in a Spring-boot application?
My application is a spring-boot application and I enable the info endpoint.我的应用程序是一个 spring-boot 应用程序,我启用了信息端点。 My application.yml content is:
我的 application.yml 内容是:
info:
app:
name: 'app'
description: 'app desc'
version: '1.0'
server:
port: 8080
servlet:
context-path: '/app'
How can I get those information when my server is starting?服务器启动时如何获取这些信息? I'd like to know the port and context-path of my server and the name of my app, etc..
我想知道我的服务器的端口和上下文路径以及我的应用程序的名称等。
Thanks.谢谢。
We can inject those values into Spring components by using @Value
annotation.我们可以使用
@Value
注解将这些值注入到 Spring 组件中。 Example:例子:
@Value("${server.servlet.context-path}")
private String contextPath;
Or we can have a configuration class, where we specify a prefix if we want to read part of the file:或者我们可以有一个配置 class,如果我们想读取文件的一部分,我们可以在其中指定一个前缀:
@Configuration
@ConfigurationProperties(prefix = "info.app")
public class AppConfig {
private String name;
private String description;
private String version;
public String getName() {
return name;
}
// Other getters omitted
}
This class can be injected in other components just like any other Spring bean.这个 class 可以像任何其他 Spring bean 一样注入其他组件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.