2012-11-23 63 views
2

在我的項目中,我們有一個spring mvc應用程序。 它具有applicationcontext.xml以及-servlet.xml配置文件。 Bean定義分佈在兩個文件中。spring mvc applicationcontext.xml和spring servlet.xml的區別

我想知道我們什麼時候有-servlet.xml,wats需要applicationcontext.xml嗎?

請在這方面提供任何解釋。

+1

[在spring中的applicationContext和spring-servlet.xml之間的區別]的可能重複(http://stackoverflow.com/questions/3652090/difference-between-applicationcontext-and -spring-servlet-xml-in-spring) –

+0

[請在此檢查它對你有幫助](https://stackoverflow.com/questions/3652090/difference-between-applicationcontext-xml-and-spring-servlet- xml-in-spring-frame) –

回答

2

你提到的applicationContext.xml什麼是根應用程序上下文(你把豆那裏當你需要應用程序範圍內對它們的訪問),你提到的作爲[servlet]-context.xml什麼是Spring MVC的DispatcherServlet特定的Spring bean配置XML。

+0

所以如果我有一個bean類,它只在[servlet] -context.xml中定義,我無法使用getBean方法獲取它的實例。對? – senthil

+1

如果您正在從ServletContext訪問WebApplicationContext,那麼您是對的,這是因爲ContextLoaderListener將在默認情況下查找您的WEB-INF目錄中名爲「applicationContext.xml」的Spring配置文件 – ElderMael

+0

請告訴我如何獲取servlet-context.xml bean通過使用getBean(或其他)......就像你使用的那樣......你能否告訴我從servlet-context.xml中獲取bean的另一種方式...... webapplicationcontext是否可以訪問只有applicationcontext.xml..does它的訪問servlet-context.xml也... ..以獲得servlet-context.xml與webapplicationcontext(或與任何其他類)我們該怎麼辦? – user533

3

applicationContext.xml將具有核心彈簧組件的bean定義。

project-servlet.xml將具有獨立servlet的bean定義。

-servlet.xml可以引用applicationContext.xml而不是其他方式。

2

servlet-context特定於一個servlet,並且應用程序上下文爲整個應用程序共享。所以,當你在servlet-context.xml中定義一個Bean時,Bean可以用於該特定Servlet的上下文,但是當你在application-context.xml中定義一個Bean時,它在整個應用程序中都可用。所以如果你有多個dispatcherServlet,你可以爲每個servlet分別使用不同的servlet-context。但是應用程序只有一個應用程序上下文

相關問題