Announcement Announcement Module
Collapse
No announcement yet.
load servlet on startup Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • load servlet on startup

    Hi, Im really new to the Spring Framework, so I might be making some very silly questions, but here goes:

    Im working on an existing application that uses the Spring Framework. So heres the thing, I would like to read some data from the db and perform some alterations to some of the data in it, but I wanted to do this when the web application was loaded.

    I was thinking of creating a servlet that would deal with the alterations to the db and then in the web.xml define the "load-on-startup" and putting a smaller value than the value indicated to load the dispatcher servlet. So heres my problem, I wont be able to access the db before the dispatcher servlet because then I wont be able to access the db, because all the hibernate configurations, datasource configuration are all done in the dispatcher serlvet.

  • #2
    I know this reply is coming a month late, so I hope it's still of some use to you or anyone who comes along with the same question...

    You could create a bean that handles your startup actions, then define that bean in your servlet config. Off the top of my head, it'd be something like:

    Code:
    <bean id="appInit" class="myapp.AppInit" scope="singleton" lazy-init="false" init-method="startup"/>
    Then perform your startup actions (DB updates, etc) in AppInit's startup method. As long as your servlet is configured to load on startup in web.xml, this should work.

    Comment


    • #3
      Great tip. Thanks.

      Comment


      • #4
        I think a BeanFactoryPostProcessor should be a better approach.
        Otherwise you have to make sure your special bean (and consequently its init method) will be created after all your other beans to be able to use them.

        Comment


        • #5
          I'll take a look at that too. I needed to do something almost identical to what imbali described, and jfredickson's suggestion worked for me. (I'm learning too.)

          Comment


          • #6
            I had the same problem. I need a class to load some properties from the databases as soon as Jetty started and put those properties in a servlet attribute to be used by all jsps during the life of the application.

            I got it to work by making my class implement ServletContextAware and InitializingBean. And I made the injection of DAOs from the applicattionContext.xml.

            The ServletContextAware makes sure I have access to the Servlet Context, so I can put the properties in a servlet attribute like context.setAttribute("staticUrl", properties.getStaticUrl());

            The InitializingBean makes sure the method is only called after all the beans are injected in the class. Is pretty much the same as init-method="startup" mentioned by jfredrickson.

            Im only a year late but as long as Google is referencing the page, people will keep coming...

            Comment


            • #7
              Originally posted by jfredrickson View Post
              I know this reply is coming a month late, so I hope it's still of some use to you or anyone who comes along with the same question...

              You could create a bean that handles your startup actions, then define that bean in your servlet config. Off the top of my head, it'd be something like:

              Code:
              <bean id="appInit" class="myapp.AppInit" scope="singleton" lazy-init="false" init-method="startup"/>
              Then perform your startup actions (DB updates, etc) in AppInit's startup method. As long as your servlet is configured to load on startup in web.xml, this should work.

              HI,
              i am facing a problem. I am using tomcat server for my spring maven project and i want to subscribe my application to facebook once when server starts up. I have tried ApplicationListener ContextRefreshedEvent. It gets invoked on application context initialization, but the issue is at that time my server has not completely started and hence my application is not publically accessible which is required in my case as facebook subscription requires verification using application public URL. So i want to do subscription when my server gets started completey and not on application context initialization.
              Any idea how can i do it? Do i have nay application level event listener that can tell me that server has started completely?

              Regards
              jia

              Comment

              Working...
              X