春季启动:启动Webshere Application Server时会自动启动应用程序吗? [英] Spring boot: Start an application automatically when Webshere Application Server starts?

查看:180
本文介绍了春季启动:启动Webshere Application Server时会自动启动应用程序吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我已经将SpringBoot应用程序部署为Websphere Application Server(WAS)的WAR.此WAR包含一个守护程序,因此它必须在WAS启动时立即启动(并且只能启动一次).

Assume I have a SpringBoot Application deployed as a WAR to Websphere Application Server (WAS). This WAR contains a daemon, so it must start straight away when WAS starts (and only once).

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

However, I still need to activate the SpringBoot Servlet by doing a http request.

现在,我了解到servlet的概念是对http请求起作用,我仍然希望在appserver启动时自动启动它.这使我的守护程序可以从独立的jar/main移植到war/webapp.

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

I tried a ServletContextListener, but the contextInitalized also get only called at the first http request.

我没有web.xml(Servlet 3).

I do not have a 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

推荐答案

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

You can change the loadOnStartup by customize the spring dispatch servlet, here is the sample question and you can use the code

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

参考: 如何在Spring Boot启动时配置"dispatcherServlet"加载?

更新

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

Seems there is a more simple way, you can config it in application.properites

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

这篇关于春季启动:启动Webshere Application Server时会自动启动应用程序吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆