Java 将springboot打包为JAR时,ServletContextEvent.getServletContext返回null

Java 将springboot打包为JAR时,ServletContextEvent.getServletContext返回null,java,spring-boot,servlets,Java,Spring Boot,Servlets,这是我实现CommandLineRunner和ServletContextListener的代码, 当springboot启动时,我想获取ServletContext并将数据放入其中。 在正常情况下,这意味着不打包到jar,只需在Idea中启动springboot即可,但当我将其打包到jar时,sce.getServletContext()将为我返回NUll。如何解决这个问题? 谢谢 我用一种愚蠢的方法来解决这个问题,那就是 @Override public void run(String...

这是我实现CommandLineRunner和ServletContextListener的代码, 当springboot启动时,我想获取ServletContext并将数据放入其中。 在正常情况下,这意味着不打包到jar,只需在Idea中启动springboot即可,但当我将其打包到jar时,sce.getServletContext()将为我返回NUll。如何解决这个问题? 谢谢


我用一种愚蠢的方法来解决这个问题,那就是

@Override
public void run(String... args) throws Exception {
    if (application != null)
        this.loadData();
}
当使用mevan打包springboot时,我进行验证,此操作可以避免空点异常。谁有更优雅的解决方案

@Override
public void run(String... args) throws Exception {
    if (application != null)
        this.loadData();
}