PDA

View Full Version : load servlet on startup


imbali
Jun 9th, 2008, 06:19 PM
Hi, I´m 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 here´s 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 here´s 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.

jfredrickson
Jul 8th, 2008, 07:15 PM
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:

<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.

brismith
Jul 17th, 2008, 09:32 AM
Great tip. Thanks.

gregd
Jul 17th, 2008, 10:21 AM
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.

brismith
Jul 17th, 2008, 11:40 AM
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.)