Are applicationContext.xml
and spring-servlet.xml
related anyhow in spring framework? Will the properties files declared in applicationContext.xml
be available to DispatcherServlet
? On a related note, why do I need a *-servlet.xml
at all ? Why is applicationContext.xml
alone insufficient?
Spring lets you define multiple contexts in a parent-child hierarchy. The The Beans in All Spring MVC controllers must go in the In most simple cases, the | |||||||||||||||||||||
|
![](http://engine.adzerk.net/i.gif?e=eyJhdiI6MjA4MTIsImF0Ijo0LCJjbSI6NDM0NjMsImNoIjoxMTc4LCJjciI6MTE2NzExLCJkaSI6IjE4MDQ3NDJhYjUyMDRhODM4YzY0MGU3YmI2NGRiODM4IiwiZG0iOjEsImZjIjoxNTg4NzUsImZsIjo3NzExNiwia3ciOiJqYXZhLHNwcmluZyIsIm53IjoyMiwicmYiOiJodHRwOi8vc3RhY2tvdmVyZmxvdy5jb20vcXVlc3Rpb25zLzExODQ4NzE2L3RyYW5zYWN0aW9uLXdpdGgtc2VydmljZS1pbi1zcHJpbmctYW5ub3RhdGlvbi1jb25maWd1cmF0aW9uIiwicnYiOjQuMzcsInByIjoxNTY3LCJzdCI6ODI3Nywiem4iOjQ0fQ&s=0kSLfgfAwtURQ0M4fwThhX9DqIg)
One more point I want to add. In
In
| |||||||||||||
|
Scenario1. In client application(application is not web application.E.g may be swing app)
No need of web.xml.ApplicationContext as container for getting bean service.No need for web server container. In test-client.xml there can be Simple bean with no remoting,bean with remoting. Conclusion:In Scenario1 applicationContex and DispatcherServlet they are not related. Scenario2. In server application(application deployed in server e.g tomcat).Accessed service via remoting from client program(e.g swing app) Define listener in web.xml
When server startup ContextLoaderListener instantiates beans defined in applicationcontext.xml. If there are suppose
beans are instantiated from all four test1.xml,test2.xml,test3.xml,test4.xml. Conclusion:In Scenario2 applicationContex and DispatcherServlet they are not related. Scenario3.In web application with spring MVC.In web.xml define. <
When tomcat starts bean defied in springweb-servlet.xml is instantiated. DispatcherServlet extends FrameworkServlet.In FrameworkServlet bean instantiation takes place for springweb .In our case springweb is FrameworkServlet. Conclusion:In Scenario3 applicationContex and DispatcherServlet they are not related. Scenario4.In web application with spring MVC.springweb-servlet.xml for servlet and applicationcontext.xml for accessing the business service within the server program Or for accessing DB service in another server program.In web.xml define. org.springframework.web.context.ContextLoaderListener
When server startup ContextLoaderListener instantiates beans defined in applicationcontext.xml If there are suppose
beans are all instantiated from all four test1.xml,test2.xml,test3.xml,test4.xml. After the completion of bean instantiation defined in applicationcontext then bean defied in springweb-servlet.xml is instantiated. So instantiation order is root is application context ,then FrameworkServlet. Now it makes clear why they are important in which scenario. |