在Spring Boot的运行时识别Start-Class

时间:2018-12-12 17:08:16

标签: spring spring-boot

我正在尝试找到一种方法来知道启动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作为入门班。

1 个答案:

答案 0 :(得分:0)

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