Solved

Executing code at application startup with Spring MVC

Posted on 2006-06-28
12
5,728 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

617 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