本文最后更新于 1 年前 ,文中信息可能已经过时。如有问题请在评论区留言。

获取 Spring Boot 启动类信息:

java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

@Component
public class StorageStartApplicationClassListener implements ApplicationListener<ApplicationStartedEvent> {
    @Override
    public void onApplicationEvent(ApplicationStartedEvent event) {
        Class<?> mainApplicationClass = event.getSpringApplication().getMainApplicationClass();
    }
}

上述示例使用 ApplicationStartedEvent 事件,也可以使用 ApplicationReadyEvent 事件。