Announcement Announcement Module
Collapse
No announcement yet.
problem loading beans from servlet Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • problem loading beans from servlet

    I have a servlet which in init I use the following code to load some beans defined in my applicationContext.xml. Doing so hangs my tomcat startup script. What is the recommended approach for loading beans from within a servlet. thanks

    Scott


    public void init(ServletConfig config) throws ServletException {
    super.init(config);
    try{


    ApplicationContext ctx =
    WebApplicationContextUtils.getWebApplicationContex t(this.getServletContext());
    if(ctx.containsBean("orderManager")){
    log.debug("attempting to initialize Order Service Listener");
    listener=new OrderManagerListener((OrderManager)ctx.getBean("or derManager"));
    listener.run();
    log.debug("Successfully initialized Order Service Listener");
    }
    else{
    log.error("Error initializing Order Service Listener");
    }
    }
    catch(Exception ce){
    log.error("error initializing order service listener :"+ce);
    }

    }

  • #2
    What does the servlet do? Any reason that servlet cannot be a spring managed servlet (i.e. AbstractController)?

    Comment


    • #3
      servlet usage

      I just use the servlet to start a jms listener. Do you recommend using the abstractcontroller or one of its subclasses? thanks

      Scott

      Comment


      • #4
        So you need to start a service on web app deployment, is that right?

        If so (and you are using servlet spec 2.4), then look at org.springframework.web.context.ContextLoaderListe ner.

        Alternatively you could register an "ApplicationContextListener" and then listen for the ContextRefreshedEvent. In this event you could start of your services.

        To be honest I am kinda guessing Never had to do anything like this.

        Comment


        • #5
          Try

          Code:
          public class IniterServlet extends HttpServlet {
          
          public void init(ServletConfig config) throws ServletException {
               WebApplicationContext wac = WebApplicationContextUtils
                     .getRequiredWebApplicationContext(config.getServletContext());
               MessageSource messageSource = (MessageSource)wac.getBean("messageSource");
          }
          
          .......
          
          }
          and make sure in web.xml
          Code:
          <servlet>
              <servlet-name>context</servlet-name>
              <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
              <load-on-startup>1</load-on-startup>
          </servlet>
              
          <servlet>
              <servlet-name>applicationIniter</servlet-name>
              <servlet-class>IniterServlet</servlet-class>
              <load-on-startup>2</load-on-startup>
          </servlet>
          it worked for me

          ps: i hope listener.run() doesn't have something like while(true) in it

          Comment

          Working...
          X