【问题标题】:Identifying Start-Class during runtime in spring boot在 Spring Boot 运行时识别 Start-Class
【发布时间】:2018-12-12 17:08:16
【问题描述】:

我正在尝试找出一种方法来了解启动 SpringBoot 的 Main-Class 的名称。 例如

@SpringBootApplication
public class SampleApplication {
    public static void main(String[] args) {
        SpringApplication.run(SampleApplication.class, args);
    }


@RestController
public class SampleController
{
   @GetMapping("/hello")
   pubic String sayHello()
   {
      System.out.println("Need start class name: "+System.getProperty("sun.java.command"));
      return "hello";
   }

}

}

当我使用 java -jar myappname.jar 运行 springboot 时,System.getProperty("sun.java.command") 返回 org.springframework.boot.loader.JarLauncher

任何人都可以建议,我怎样才能得到实际运行类的名称。我尝试在 manifest.mf 中指定 start-class 属性。它仍然给了我org.springframework.boot.loader.JarLauncher 作为入门级。

【问题讨论】:

标签: spring spring-boot


【解决方案1】:

您应该可以在ApplicationContext@Autowire 然后执行context.getBeansWithAnnotation(SpringBootApplication.class).values().toArray()[0].getClass().getName(),这将是give you the first (and presumably only) bean in the context annotated with @SpringBootApplication

【讨论】:

  • 感谢瑞恩的回复。如果我的 Bean 不是 Spring-Bean 而是简单的 Java 类,我如何获得这个名称?此外,我的部分代码在显示弹簧横幅之前被执行事件。我当时没有上下文。
  • 或许我们可以聊聊这个——chat.stackoverflow.com/rooms/185157/…
猜你喜欢
  • 1970-01-01
  • 2016-06-20
  • 1970-01-01
  • 2021-09-07
  • 1970-01-01
  • 1970-01-01
  • 2018-06-05
  • 2015-09-25
相关资源
最近更新 更多