Spring中xxx-servlet.xml跟applicationContext.xml的認識

Spring中xxx-servlet.xml和applicationContext.xml的認識
    由於直接使用了SpringMVC,因此以前一直不明白xxx-servlet.xml和applicationContext.xml是如何區別的,其實若是直接使用SpringMVC是能夠不添加applicationContext.xml文件的。
使用applicationContext.xml文件時是須要在web.xml中添加listener的:
<listener>
<listener-class>org.springframeworntext.ContextLoaderListener</listener-class>
  </listener> 
而這個通常是採用非spring mvc架構,如使用struts之類而又想引入spring才添加的,這個是用來加載Application Context。

若是直接採用SpringMVC,只須要把全部相關配置放到xxx-servlet.xml中就OK了。


在後面的使用中又發現了新問題,若是用上面的方式,在jsp中是沒有辦法獲得WebApplicationContext的,必須加上ContextLoaderListener後,即須要applicationContext.xml這個配置文件,才能在jsp中用org.springframeworntext.WebApplicationContext ctx = org.springframeworntext.support.WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());的方式獲取到ApplicationContext,不然獲取的就是null。

因此我如今把Controller、View相關的信息放到了xxx-servlet.xml中,把Model、Service之類的信息放到了applicationContext.xml中。web

相關文章
相關標籤/搜索