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.

2 Answers 2

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)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks it works. Is this a common practice for initialization?
yes, it is. The interface is in the servlet API and is meant exactly for this purpose.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.