我这段代码是放到一个工程中,然后发布到仓库,供其它项目依赖使用的,使用常规的方法获取不到main函数的启动类,其它方案都试过了,都不行,只有这个可以。
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.context.ApplicationListener;
public class StorageStartAppClassNameListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
String StartAppClassName = event.getSpringApplication().getMainApplicationClass().getName();
System.setProperty("StartAppClassName", StartAppClassName);
System.out.println(System.getProperty("StartAppClassName"));
}
}