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.
Servlets are used in Spring-MVC. In Spring-MVC when you write annotation like @Controller, indirectly you are using a Servlet called Dispatcher Servlet. Dispatcher Servlet is defined in web. xml file with properties and class name which is mapped to .
Upon initialization of a DispatcherServlet , the framework looks for a file named [servlet-name]-servlet. xml in the WEB-INF directory of your web application and create the beans defined there, overriding the definitions of any beans defined with the same name in the global scope.
After the web container loads and instantiates the servlet class and before it delivers requests from clients, the web container initializes the servlet.
The servlet path represents the path of the main DispatcherServlet. The DispatcherServlet is an actual Servlet, and it inherits from HttpSerlvet base class. The default value is similar to the context path, i.e. (“/”): spring.mvc.servlet.path=/
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With