rperez
rperez

Reputation: 8520

spring mvc servlet initialization

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.

Upvotes: 6

Views: 8468

Answers (2)

Bozho
Bozho

Reputation: 597016

Use a ServletContextListener and define it in web.xml:

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

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

Upvotes: 14

kgiannakakis
kgiannakakis

Reputation: 104168

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.

Upvotes: 0

Related Questions