springboot 打成 war 包后注入 applicationContext

须要在 springboot  某个业务类中注入 applicationContext,解决办法以下spring

public static void main(String[] args) {
   ConfigurableApplicationContext configurableApplicationContext =SpringApplication.run(HongoneApplication.class, args);
   //解决WebSocket不能注入的问题
   WebSocketServer.setApplicationContext(configurableApplicationContext);
}

本地测试经过,可是打成 war 包后,发现注入失效,解决办法以下springboot

public class HongoneApplication  extends SpringBootServletInitializer{
   public static void main(String[] args) {
      ConfigurableApplicationContext configurableApplicationContext =SpringApplication.run(HongoneApplication.class, args);
      //解决WebSocket不能注入的问题
      WebSocketServer.setApplicationContext(configurableApplicationContext);
   }

   @Override
   protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
      return builder.sources(HongoneApplication.class);
   }

   @Override
   public void onStartup(ServletContext servletContext) throws ServletException {
      super.onStartup(servletContext);
      WebSocketServer.setApplicationContext(WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext));
   }

启动类中重写以上两个方法,问题解决app

相关文章
相关标签/搜索