对SpringMVC上下文容器配置的理解

来源:互联网 发布:阿依莲正品淘宝折扣店 编辑:程序博客网 时间:2024/06/06 01:21

1、在读程序的过程中有一个疑问:

WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();

FilterSql filterSql = (FilterSql)webApplicationContext.getBean("filterSql");//获取bean


上面的程序中webApplicationContext 没有获取任何的xml配置文件,为什么可以获取authority_applicationContext.xml中的bean呢?


这个与以下配置有关:

在web.xml中


<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:authority_config/authority_applicationContext.xml
            classpath:authority_config/authority_springSecurity.xml
        </param-value>
    </context-param>

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


只要有以上的配置就可以用webApplicationContext 获取authority_applicationContext.xml中的bean了。


2、具体解释如下:

spring上下文容器配置

  spring为我们提供了实现ServletContextListener接口的上下文初始化监听器:org.springframework.web.context.ContextLoaderListener

  spring为我们提供的IOC容器,需要我们指定容器的配置文件,然后由该监听器初始化并创建该容器。要求你指定配置文件的地址及文件名称,一定要使用:contextConfigLocation作为参数名称。

复制代码
<context-param>    <param-name>contextConfigLocation</param-name>    <param-value>/WEB-INF/applicationContext.xml,/WEB-INF/action-servlet.xml,/WEB-INF/jason-servlet.xml</param-value></context-param><listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
复制代码

该监听器,默认读取/WEB-INF/下的applicationContext.xml文件。但是通过context-param指定配置文件路径后,便会去你指定的路径下读取对应的配置文件,并进行初始化。


其他的关于SpringMVC的理解,还可以查看下面的文章

http://www.cnblogs.com/brolanda/p/4265597.html


原创粉丝点击