Solved

Executing code at application startup with Spring MVC

Posted on 2006-06-28
12
5,530 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

HOW TO: Connect to the VMware vSphere Hypervisor 6.5 (ESXi 6.5) using the vSphere (HTML5 Web) Host Client 6.5, and perform a simple configuration task of adding a new VMFS 6 datastore.
A Short Story about the Best File Recovery Software – Acronis True Image 2017
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now