Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Executing code at application startup with Spring MVC

Posted on 2006-06-28
12
Medium Priority
?
5,769 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 28

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 28

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 28

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 28

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

In this article, WatchGuard's Director of Security Strategy and Research Teri Radichel, takes a look at insider threats, the risk they can pose to your organization, and the best ways to defend against them.
In this article, I’ll show how research, determination, and use of modern technology helped me solve a DNA mystery.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

730 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