• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5978
  • Last Modified:

Executing code at application startup with Spring MVC

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
NHBFighter
Asked:
NHBFighter
1 Solution
 
rrzCommented:
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
 
w0lverCommented:
Is it a servlet?  Would the web.xml tag <load-on-startup> work?
0
 
NHBFighterAuthor Commented:
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
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
NHBFighterAuthor Commented:
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
 
rrzCommented:
>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
 
NHBFighterAuthor Commented:
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
 
rrzCommented:
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
 
NHBFighterAuthor Commented:
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
 
rrzCommented:
Thanks for your thoughts.
0
 
GhostModCommented:
PAQed with points refunded (125)

GhostMod
Community Support Moderator
0
 
Makatun1Commented:
<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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now