BeanFactory到WebApplicationContext的结构 以及bean和spring容器的关系

来源:互联网 发布:三星s7edge优化精简 编辑:程序博客网 时间:2024/05/16 06:48

BeanFactory: Ioc 容器

ApplicationContext: Spring容器






WebApplicationContext需要ServletContext实例,也就是说它必须在拥有Web 容器的

前提下才能完成启动的工作

Spring分别提供了用于启动WebApplicationContext的 Servlet和 Web容器监听器: org.springframework.web.context.ContextLoaderServlet;

org.springframework.web.context.ContextLoaderListener。

两者的内部都实现了启动 WebApplicationContext 实例的逻辑,我们只要根据 Web 容

器的具体情况选择两者之一,并在web.xml中完成配置就可以了。

<!--①指定配置文件-->

<context-param>                                                        

       <param-name>contextConfigLocation</param-name>  

       <param-value>

         /WEB-INF/baobaotao-dao.xml,/WEB-INF/baobaotao-service.xml

     </param-value>

</context-param>

<!--②声明Web容器监听器-->

<listener>  

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

</listener>

ContextLoaderListener 通过 Web 容器上下文参数 contextConfigLocation 获取 Spring 配

置文件的位置。用户可以指定多个配置文件,用逗号、空格或冒号分隔均可。对于未带资

源类型前缀的配置文件路径, WebApplicationContext默认这些路径相对于Web的部署根路

径。当然,我们可以采用带资源类型前缀的路径配置,如“classpath*:/baobaotao-*.xml”

和上面的配置是等效的。


使用Spring容器以后,程序各元素之间的关系图:



Bean配置信息:定义了Bean的实现和依赖关系。

Spring容器根据 Bean配置信息在容器内部建立Bean注册表。


文章来自于:http://blog.csdn.net/bingjing12345/article/details/9933983


0 0
原创粉丝点击