1. 程式人生 > >springboot 打成 war 包後注入 applicationContext

springboot 打成 war 包後注入 applicationContext

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

本地測試通過,但是打成 war 包後,發現注入失效,解決辦法如下

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));
   }

啟動類中重寫以上兩個