assume have springboot application deployed war websphere application server (was). war contains daemon, must start straight away when starts (and once).
however, still need activate springboot servlet doing http request.
now understand concept of servlets act on http requests, still want auto started on appserver start. makes daemon portable standalone jar/main war/webapp.
i tried servletcontextlistener
, contextinitalized
called @ first http request.
i not have web.xml (servlet 3).
code:
@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) { // } }
and:
@component public class demorunner implements applicationrunner { @override public void run(applicationarguments arg0) throws exception { system.err.println("i running"); } }
when start first this:
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.
to spring boot application starting, first need visit link (http:/localhost:9080/demo/). starts rolling, starting startup method can see in log. how can starting without doing http request?
[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] running [...] 2016-09-02 10:45:58.093 info 23716 --- [dpool-thread-48] com.example.demoapplication : started demoapplication in 6.372 seconds (jvm running 31.549) [...] [err] contextinitialized [err] contextinitialized
you can change loadonstartup
customize spring dispatch servlet, here sample question , can use 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); } }; }
reference: how configure 'dispatcherservlet' load on startup spring boot?
upate
seems there more simple way, can config in application.properites
spring.mvc.servlet.load-on-startup=1
Comments
Post a Comment