applicationContext.xml和dispatcher-spring servlet.xmll的区别

4898人阅读
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中。
欢迎加入我的QQ交流群
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:1876676次
积分:16188
积分:16188
排名:第634名
原创:183篇
转载:790篇
评论:141条
(7)(73)(120)(21)(30)(19)(1)(7)(71)(38)(24)(25)(19)(19)(11)(7)(1)(2)(10)(18)(21)(22)(80)(26)(28)(13)(16)(14)(42)(21)(7)(4)(18)(9)(12)(5)(12)(38)(4)(30)(28)1045人阅读
spring(26)
2个xml文件的区别
一直搞不明白两者的区别。
如果使用了SpringMVC,事实上,bean的配置完全可以在xxx-servlet.xml中进行配置。为什么需要applicationContext.xml?一定必须?
因为直接使用了SpringMVC,所以之前一直不明白xxx-servlet.xml和applicationContext.xml是如何区别的,其实如果直接使用SpringMVC是可以不添加applicationContext.xml文件的。
使用applicationContext.xml文件时是需要在web.xml中添加listener的:
org.springframework.web.context.ContextLoaderListener
而这个一般是采用非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.
虽然现在看的不是很懂,但也是有很大的帮助的
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:95853次
积分:2114
积分:2114
排名:第17873名
原创:122篇
转载:29篇
评论:46条
(3)(8)(11)(14)(5)(13)(15)(21)(8)(19)(1)(1)(1)(6)(4)(2)(2)(2)(2)(13)springMVC(33)
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中。
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场}

我要回帖

更多关于 servletdispatcher 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信