views:

302

answers:

2

Hi,

I am new to spring MVC. I am looking for a place in my spring mvc applicationwhere I can initialize all sorts of things in the application. usually I did that in the init() method of the my main servlet but now the dispatcher servlet is of spring and I cannot overide the init function.

what is the best practice?

Thanks.

+2  A: 

Use a ServletContextListener and define it in web.xml:

<listener>
    <listener-class>com.company.YourListenerClass</listener-class>
</listener>

(you make a class which extends ServletContextListener and implement the contextInitialized() method, where you place your initialization code)

Bozho
Thanks it works.Is this a common practice for initialization?
rperez
yes, it is. The interface is in the servlet API and is meant exactly for this purpose.
Bozho
A: 

All beans can have an init-method. See the documentation. I suppose that the best practice will be to use this method for every bean you define. A bean can have references to other beans if this is required.

kgiannakakis