Solved

Executing code at application startup with Spring MVC

Posted on 2006-06-28
12
5,668 Views
Last Modified: 2009-10-08
Hi

In Struts one can create a class that implements org.apache.struts.action.PlugIn and define that class in in the struts-config.xml file with the <plug-in> tag. Doing so will cause that classes init() metod will be executed when the application starts up.

I'm building an app using Spring MVC and I would like to have something similiar to the above. Basically I want some code to run when the web app starts up.
I'm new to spring and would appreciate some suggestions.

Thanks everyone
Dave

0
Comment
Question by:NHBFighter
12 Comments
 
LVL 27

Expert Comment

by:rrz
ID: 17001346
I don't use Struts or Spring.  But you could use a servlet that loads on start up.   You could put something like the following in your web.xml
    <servlet>    
                   <servlet-name>myServlet</servlet-name>
                   <servlet-class>test.MyServlet</servlet-class>
                   <load-on-startup/>
    </servlet>  
0
 
LVL 3

Expert Comment

by:w0lver
ID: 17001426
Is it a servlet?  Would the web.xml tag <load-on-startup> work?
0
 
LVL 4

Author Comment

by:NHBFighter
ID: 17001508
Both struts and Spring MVC have their own controller servlet. In many application this controller servlet is the only servlet that is used. And in struts the init() method of the controller servlet instantiates the startup plugin class and calls its init method.

the spring controller servlet is called the DispatcherServlet. It is the servlet that manages all request that come into the spring app. (in theory).  I want to use Spring specific techniques to run code on application start up, hence I don't want to create my own servlet just to manage initialization stuff.  I suppose I could write a class that extends the DispatcherServlet and override the init() method.

Does anyone know the standard way to handle this in spring?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Author Comment

by:NHBFighter
ID: 17002350
One way I see of handling this it to create a class that implements the ApplicationListener interface. In this class's onApplicationEvent() check to see if the passed in ApplicationEvent is of the type ContextRefreshedEvent. If so then I will do my application start up processing. This seems like it will work. And thoughts?

Dave
0
 
LVL 27

Expert Comment

by:rrz
ID: 17002515
>Basically I want some code to run when the web app starts up.
What are you doing in this code ?  
If   ApplicationListener  doesn't  work for you, then you do have
javax.servlet.ServletContextListener
0
 
LVL 4

Author Comment

by:NHBFighter
ID: 17002708
Thanks for the replies everyone

In the code I will be quering a data base and caching some data objects built from that query.  Since I will be using spring to manage the datasource I will need to ensure that the spring context has been initialized so I can get access to the datasource when I need it. I won't be able to uses the ServletContextListner in that case.

A nother option for me would be to create a standard bean in the spring servlet-context.xml and mark it as lazy-init=false. This will cause the bean to be instantiated on start up and I can use that bean to handle my start up stuff to.

I'm really looking for spring specific solutions to this. How have other Sping users handled this situation in the past.

thanks
0
 
LVL 27

Expert Comment

by:rrz
ID: 17002829
You have answered your own question here. If you don't attract a Spring expert, then I would reccomend that you ask for deletion and get your points back.   Thanks for getting us to take a look at Spring.   Do you like it better than Struts ?
0
 
LVL 4

Author Comment

by:NHBFighter
ID: 17002887
Well I haven't had an opurtunity to use Spring too much yet. From what I've seen so far spring is framework for building all types of application and it happens to have a web MVC module as well. I really like Struts and would prefere to use it (mostly because I'm already familiar with it) but where I'm at now uses Spring as a corporate standard.  Spring does seem like an extreamlly well written frame work and I'm excieted about the oppurnity I have to learn about it. Although the documentation of sturts seems to be better.
0
 
LVL 27

Expert Comment

by:rrz
ID: 17006052
Thanks for your thoughts.
0
 
LVL 1

Accepted Solution

by:
GhostMod earned 0 total points
ID: 17399950
PAQed with points refunded (125)

GhostMod
Community Support Moderator
0
 

Expert Comment

by:Makatun1
ID: 25531874
<bean id="onLoad" class="springapp.repository.OnLoad" lazy-init="false" init-method="onStartup"/>

create public void onStartup()  method in OnLoad and it will be run every startup
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

While it may be true that the internet is a place of possibilities, it is also a hostile environment lurking with many dangers. By clicking on the wrong link, trusting the wrong person or using a weak password, you are virtually inviting hackers to …
Active Directory security has been a hot topic of late, and for good reason. With 90% of the world’s organization using this system to manage access to all parts of their IT infrastructure, knowing how to protect against threats and keep vulnerabil…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question