Spring启动:Webshere Application Server启动时自动启动应用程序?

时间:2016-09-02 08:57:20

标签: spring spring-boot websphere servlet-3.0 servletcontextlistener

假设我将一个SpringBoot应用程序部署为对Websphere Application Server(WAS)的WAR。这个WAR包含一个守护进程,所以必须在WAS启动时立即开始(并且只有一次)。

但是,我仍然需要通过执行http请求来激活SpringBoot Servlet。

现在我明白servlet的概念是对http请求采取行动,我仍然希望在appserver启动时自动启动它。这使得我的守护进程可以从独立的jar / main移植到war / webapp。

我尝试了ServletContextListener,但contextInitalized也仅在第一个http请求时调用。

我没有web.xml(servlet 3)。

代码:

@SpringBootApplication
@WebListener
public class DemoApplication extends SpringBootServletInitializer implements ServletContextListener {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        System.err.println("ONSTARTUP"); 
        super.onStartup(servletContext);
    }

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

   @Override
   protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
      return application.sources(DemoApplication.class);
   }


    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.err.println("contextInitialized"); 
    }

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        //
    }
}

@Component
public class DemoRunner implements ApplicationRunner {

    @Override
    public void run(ApplicationArguments arg0) throws Exception {
        System.err.println("I AM RUNNING");
    }

}

当我开始使用时,我首先得到这个:

Launching defaultServer (WebSphere Application Server
16.0.0.2/wlp-1.0.13.cl160220160526-2258) on Java HotSpot(TM) 64-Bit Server VM, version 1.7.0_79-b15 (en_US) 
[...]  
[AUDIT   ] CWWKT0016I: Web application available (default_host): http://localhost:9080/demo/ 
[AUDIT   ] CWWKZ0001I: Application test started in 17,282 seconds.

要启动我的Spring Boot应用程序,我首先需要访问此链接(http:/ localhost:9080 / demo /)。然后它开始滚动,从启动方法开始,如您在日志中看到的那样。但是如何在不执行http请求的情况下启动此操作?

[err] ONSTARTUP
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.4.0.RELEASE)
2016-09-02 10:45:52.670  INFO 23716 --- [dPool-thread-48] com.example.DemoApplication              : Starting DemoApplication on [...]
2016-09-02 10:45:58.019  INFO 23716 --- [dPool-thread-48] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 0
[...]
[err] I AM RUNNING
[...]
2016-09-02 10:45:58.093  INFO 23716 --- [dPool-thread-48] com.example.DemoApplication              : Started DemoApplication in 6.372 seconds (JVM running for 31.549)
[...]
[err] contextInitialized
[err] contextInitialized

1 个答案:

答案 0 :(得分:1)

您可以通过自定义spring dispatch servlet来更改loadOnStartup,这是示例问题,您可以使用代码

@Bean
public static BeanFactoryPostProcessor beanFactoryPostProcessor() {
    return new BeanFactoryPostProcessor() {

        @Override
        public void postProcessBeanFactory(
                ConfigurableListableBeanFactory beanFactory) throws BeansException {
            BeanDefinition bean = beanFactory.getBeanDefinition(
                    DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME);

            bean.getPropertyValues().add("loadOnStartup", 1);
        }
    };
}

参考: how to configure 'dispatcherServlet' load on startup by spring boot?

<强> Upate

似乎有一种更简单的方法,您可以在application.properites

中进行配置

spring.mvc.servlet.load-on-startup=1