2

In my project, we have a spring mvc application. It has got both applicationcontext.xml as well as -servlet.xml config files. Bean definitons are spread in both the files.

I want to know when we have -servlet.xml wats the need for applicationcontext.xml?

Please provide any explanation in this area.

ElderMael
  • 7,000
  • 5
  • 34
  • 53
senthil
  • 335
  • 1
  • 4
  • 7
  • 1
    possible duplicate of [difference between applicationContext and spring-servlet.xml in spring](http://stackoverflow.com/questions/3652090/difference-between-applicationcontext-and-spring-servlet-xml-in-spring) – Donal Fellows Nov 23 '12 at 09:14
  • [Check here it will be helpful to you alot](https://stackoverflow.com/questions/3652090/difference-between-applicationcontext-xml-and-spring-servlet-xml-in-spring-frame) – KIRAN KUMAR MATAM Jun 23 '17 at 02:58

3 Answers3

3

applicationContext.xml will have the bean definitions of the core spring components.

project-servlet.xml will have bean definitions of indivisual servlets.

-servlet.xml can have references to applicationContext.xml not the other way round.

RajeshKashyap
  • 929
  • 5
  • 3
2

What you refer as applicationContext.xml is the root application context (you put beans there when you need application-wide access to them) and what you refer as the [servlet]-context.xml is a specific Spring Bean configuration xml for Spring MVC's DispatcherServlet.

ElderMael
  • 7,000
  • 5
  • 34
  • 53
  • so if i have a bean class and it is defined only in [servlet]-context.xml , i cannot get its instance using getBean method. right? – senthil Nov 23 '12 at 07:21
  • 1
    If you are accessing the WebApplicationContext from the ServletContext then you are right, that is because the ContextLoaderListener will look (by default) to a Spring Configuration file named "applicationContext.xml" in your WEB-INF directory – ElderMael Nov 23 '12 at 07:24
  • Please tell me how to get the servlet-context.xml bean by using getBean (or whatever)...as you used If...could you tell me the other way to get the bean from servlet-context.xml...can the webapplicationcontext have the access only to applicationcontext.xml..does it access to servlet-context.xml also?..to acccess the servlet-context.xml with webapplicationcontext(or with any other class) what should we do? – Pratap M Nov 23 '12 at 07:55
  • I think that is not possible without extending the DispatcherServlet to publish his own context... but feel free to ask another question about it! – ElderMael Nov 23 '12 at 08:16
2

servlet-context is specific to a servlet and application context is shared for whole application. So when you define a Bean in servlet-context.xml the Bean is available to the context of that specific servlet, but when you define a Bean in application-context.xml it is available in the whole application. So if you have multiple dispatcherServlet you can have separate servlet-context for each servlet. But there is only one application-context for the application

Moinul Hossain
  • 2,176
  • 1
  • 24
  • 30