我有以下配置类:

@Configuration
public class AuthenticationFilterConfiguration extends SpringBootServletInitializer {

    private static final Logger LOGGER = LoggerFactory.getLogger(AuthenticationFilterConfiguration.class);

    public AuthenticationFilterConfiguration(){
        LOGGER.info("Setting up authentication filter");
    }

    @Override
    public void onStartup(ServletContext context) throws ServletException {
        super.onStartup(context);
        LOGGER.info("Authentication filter configured");
    }

}


这与主应用程序位于同一软件包中:

@SpringBootApplication
public class MainApplication {
    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class, args);
    }
}


在我的日志中-我看到:


  
    2018-04-10 AuthenticationFilterConfiguration:设置身份验证过滤器
  


但是,我从未看到onStartup(...)方法显示Authentication filter configured的日志消息。我的其他@Configuration类正在加载(它们创建了许多@Bean

为什么不调用onStartup方法?其他一切正常,我的RESTful端点正常工作,等等。

最佳答案

原来我需要实现ServletContextInitializer而不是Spring Boot类。这很好用:

@Configuration
public class AuthenticationFilterConfiguration implements ServletContextInitializer {

    private static final Logger LOGGER = LoggerFactory.getLogger(AuthenticationFilterConfiguration.class);

    public AuthenticationFilterConfiguration(){
        LOGGER.info("Setting up authentication filter");
    }

    @Override
    public void onStartup(ServletContext context) throws ServletException {
        LOGGER.info("Authentication filter configured");
    }

}

10-02 21:13