【轉載】Spring中的applicationContext.xml與SpringMVC的xxx-servlet.xml的區別

來源:互聯網
上載者:User

標籤:style   class   blog   http   tar   ext   

一直搞不明白兩者的區別。
如果使用了SpringMVC,事實上,bean的配置完全可以在xxx-servlet.xml中進行配置。為什麼需要applicationContext.xml?一定必須?
一、
因為直接使用了SpringMVC,所以之前一直不明白xxx-servlet.xml和applicationContext.xml是如何區別的,其實如果直接使用SpringMVC是可以不添加applicationContext.xml檔案的。
使用applicationContext.xml檔案時是需要在web.xml中添加listener的:
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
而這個一般是採用非spring mvc架構,如使用struts之類而又想引入spring才添加的,這個是用來載入Application Context。
如果直接採用SpringMVC,只需要把所有相關配置放到xxx-servlet.xml中就OK了。
二、
Spring lets you define multiple contexts in a parent-child hierarchy.
The applicationContext.xml defines the beans for the "root webapp context", i.e. the context associated with the webapp.
The spring-servlet.xml (or whatever else you call it) defines the beans for one servlet‘s app context. There can be many of these in a webapp, one per Spring servlet (e.g. spring1-servlet.xml for servlet spring1, spring2-servlet.xml for servlet spring2).
Beans in spring-servlet.xml can reference beans in applicationContext.xml, but not vice versa.
All Spring MVC controllers must go in the spring-servlet.xml context.
In most simple cases, the applicationContext.xml context is unnecessary. It is generally used to contain beans that are shared between all servlets in a webapp. If you only have one servlet, then there‘s not really much point, unless you have a specific use for it.



免責聲明:
    本文轉自網路文章,轉載此文章僅為個人收藏,分享知識,如有侵權,請聯絡博主進行刪除。
    原文zb0567
    原文地址:http://blog.csdn.net/zb0567/article/details/7930642

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.