Spring mvc servlet initialization

I am new to spring MVC. I am looking for a place in my spring mvc application where I can initialize all kinds of things in the application. I usually did this in the init () method of my main servlet, but now the dispatcher servlet has spring, and I cannot override the init function.

What is the best practice?

Thanks.

+6
java spring initialization spring-mvc
source share
2 answers

Use ServletContextListener and define it in web.xml :

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

(you create a class that implements ServletContextListener and implements the contextInitialized() method, where you put your initialization code)

+13
source share

All beans can have an init method. See the documentation. I suggest that it is best practice to use this method for every bean that you define. A bean may have links to other beans, if necessary.

0
source share

All Articles