Search code examples
javaspringspring-mvcwebspherestartup

Spring *-servlet.xml not getting Loaded on Websphere Server Startup


I have been trying to run an a method as soon as server starts . (As per my understanding Issue could be Context not getting loaded at Server start or restart). This below Works on Tomcat It feels Like Websphere is doing a lazy initialization which it should not

When i Deploy the component on Websphere or restart it the context is not loaded unless i hit the URL eg "http://localhost/myapp/"

I have tried 3 Methods to run the method

  1. Scheduling
@EnableScheduling
public class MyClass{

MyClass(){
}

@Scheduled(cron = "2 * * * * *")
public void myMethod() {
}
  1. Init
<bean id="myClass" class="com.abc.abc.billing.myClass"   init-method="readConfigScheduler"/>
  1. ApplicationListener
@Component
public class StartUp implements ApplicationListener<ContextRefreshedEvent>
    {
        @Autowired
        private MyClass cls;
        @Override
        public void onApplicationEvent(ContextRefreshedEvent arg0) 
        {
            System.out.println("ContextLoaded");
            cls.MyMethod();
        }
    }

As soon as i hit the URL "http://localhost/myapp/" all the above three method Works.

i have already tried the below links for help but i am not sable to make it work.

Execute method on startup in spring

applicationContext.xml is not getting loaded when I have kept the spring-servlet.xml in Web application

Also i talked to few people which said its working when they tried but on later investigation i found out that when u deploy the app through eclipse , it opens the url for app automatically which loads the context.

So when we restarted the app it didnt work

My Web.xml looks something like this

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
              http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
<servlet>
    <servlet-name>MyApp</servlet-name>
     <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
     </servlet-class>
     <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/MyApp-servlet.xml</param-value>
     </init-param>
     <load-on-startup>1</load-on-startup>
</servlet>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<session-config>
    <session-timeout>60</session-timeout>
</session-config>

I am quite new to Spring so any help would be good


Solution

  • Seems you get the same problem which describe in this question

    By default, websphere optimizes server start time and memory use by not starting a servlet until a request is received for the web application, if you want to load servlets when a web application is installed, add the following line to the server.xml configuration file or a file that it includes:

    <webContainer deferServletLoad="false"/>