繁体   English   中英

如何在 Spring-boot 应用程序中获取上下文路径和 info.info?

[英]How can I get context-path and info.info in a Spring-boot application?

我的应用程序是一个 spring-boot 应用程序,我启用了信息端点。 我的 application.yml 内容是:

info:
  app:
    name: 'app'
    description: 'app desc'
    version: '1.0'
server:
  port: 8080
  servlet:
    context-path: '/app'

服务器启动时如何获取这些信息? 我想知道我的服务器的端口和上下文路径以及我的应用程序的名称等。

谢谢。

我们可以使用@Value注解将这些值注入到 Spring 组件中。 例子:

@Value("${server.servlet.context-path}")
private String contextPath;

或者我们可以有一个配置 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
}

这个 class 可以像任何其他 Spring bean 一样注入其他组件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM