SpringBoot中使用ServletContextListener初始化API接口信息 。产生了一个错误:(如下)
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
怎么使用?实现ServletContextListener接口中contexInitialized方法。在改方法中实现在Web应用启动时的初始化操作,我这里是初始化API接口。
@WebListener public class StartListener implements ServletContextListener { public void contextInitialized(ServletContextEvent sce) { new APIUrl(); } public void contextDestroyed(ServletContextEvent sce) { } }
解决上面的错误:在Springboot启动类中加上注解
@ServletComponentScan