我正在尝试找到一种方法来知道启动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
作为入门班。
答案 0 :(得分:0)
您应该能够在@Autowire
中ApplicationContext
,然后执行context.getBeansWithAnnotation(SpringBootApplication.class).values().toArray()[0].getClass().getName()
,这将give you the first (and presumably only) bean in the context annotated with @SpringBootApplication