Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 995
rperez Avatar asked Jan 05 '10 12:01

rperez


People also ask

Can we use servlet in Spring MVC?

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 .

How is the DispatcherServlet instantiated?

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.

What is servlet initialization?

After the web container loads and instantiates the servlet class and before it delivers requests from clients, the web container initializes the servlet.

What is Spring MVC servlet Path?

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=/


1 Answers

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)

like image 107
Bozho Avatar answered Sep 29 '22 01:09

Bozho