6

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.

Mirko N.
  • 10,537
  • 6
  • 38
  • 37
rperez
  • 8,430
  • 11
  • 36
  • 44

2 Answers2

14

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)

Mike G
  • 4,713
  • 2
  • 28
  • 31
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
0

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
  • 103,016
  • 27
  • 158
  • 194