springmvc:配置文件总结

来源:互联网 发布:高数搜题软件哪个好 编辑:程序博客网 时间:2024/05/29 17:13

注意以下区别

区别:include and exclude=> 包含,不包含
目的:防止重复引用
spring-mvc.xml

<!-- 自动扫描且只扫描@Controller --><context:component-scan base-package="org.springside.examples.quickstart" use-default-filters="false"><context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan>

applicationContext.xml

<!-- 使用annotation 自动注册bean, 并保证@Required、@Autowired的属性被注入 --><context:component-scan base-package="org.springside.examples.quickstart"><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan>

非默认扫描方式

目标:如果不想在xml文件中配置bean,可以给类加上spring组件注解,只需再配置下spring的扫描器就可以实现bean的自动载入。

<!-- 定义扫描根路径为leot.test,不使用默认的扫描方式 --><context:component-scan base-package="leot.test" use-default-filters="false">  <!-- 扫描符合@Service @Repository的类 -->  <context:include-filter type="annotation" expression="org.springframework.stereotype.Service" />  <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository" /></context:component-scan><!-- 变异版 --><context:component-scan base-package="com.xhlx.finance.budget" >  <context:include-filter type="regex" expression=".service.*"/></context:component-scan>

applicationcontext.xml和spring-servlet.xml的区别

以下这段话摘自:https://zhidao.baidu.com/question/689286415036688044.html。
但是一直不能理解。

因为直接使用了SpringMVC,所以之前一直不明白xxx-servlet.xml和applicationContext.xml是如何区别的,其实如果直接使用SpringMVC是可以不添加applicationContext.xml文件的。
使用applicationContext.xml文件时是需要在web.xml中添加listener的:
而这个一般是采用非spring mvc架构,如使用struts之类而又想引入spring才添加的,这个是用来加载Application Context。
如果直接采用SpringMVC,只需要把所有相关配置放到xxx-servlet.xml中就OK了。

<listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>

后来查到:

applicationContext.xml是随ContextLoaderListener的加载而执行的,
而xxx-servlet.xml是随DispatcherServlet的加载而执行的,在web.xml中,
加载顺序是listener>filter>servlet,所以applicationContext.xml先加载!

然后:

ApplicationContext.xml  是spring全局配置文件,用来控制spring 特性的、比如:aop,sessionFactoryxxx-servlet.xml 是spring mvc里面的,控制器、拦截uri转发view

继而:

applicationContext是mvc context的父容器,mvc context可以引用applicationContext的bean,而applicationContext无法引用到mvc的bean,如果你这样配,有些东西如果applicationContext需要,它就找不到了,所以还不如全放到applicationContext中。
spring查找bean,会现在当前context中查找,如果没有满足的,再到父容器查找.
applicationContext是在web.xml中配置的ContentLoader监听器启动的,当xml启动时加载,并按照一个约定的key放在java的ServletContext中,然后mvc 的servlet初始化时,先从ServletContext中按照约定的key取出来,以它为父容器,去创建mvc的容器。
再说,两个不同的spring context,是不会有冲突的,是可以存在相同的bean定义的,只不过优先查找当前context,不存在才往上找。所以你应该检查下是什么错误,看看什么原因

参考资料:
https://zhidao.baidu.com/question/134497716297939085.html
http://blog.csdn.net/ydwuli06/article/details/6993219
https://zhidao.baidu.com/question/689286415036688044.html
https://www.oschina.net/question/229402_232191
http://www.oschina.net/question/1015582_125216?sort=time
http://www.oschina.net/question/3270_115274

0 0
原创粉丝点击