在春季启动的运行时识别启动类



我正在尝试确定一种方法来了解启动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作为起始类。

您应该能够在ApplicationContext中执行@Autowire,然后执行context.getBeansWithAnnotation(SpringBootApplication.class).values().toArray()[0].getClass().getName(),这将为您提供用@SpringBootApplication注释的上下文中的第一个(可能也是唯一一个(bean

最新更新