• Spring MVC 指导文档解读(一)


    22.1 指导文档章节

    In the Web MVC framework, each DispatcherServlet has its own WebApplicationContext, which inherits all the beans already defined in the root WebApplicationContext. 

    解读

    一、 DispatcherServlet 可以定义多个

    二、 root WebApplicationContext 适用于所有的 DispatcherServlet ,换句话说是继承所有root中定义的bean。(经过实验,如果在root中定义一个单例bean,则这个bean的实例被所有servlet的上下文所共享,必须使用指定linstener才能加载root的配置文件 )

    三、 可以通过 默认配置文件/初始化参数配置文件 定义自己私有的 context ,换句话说定义自己的beans.

    备注

    ① 默认配置文件:

    a file named [servlet-name]-servlet.xml in the WEB-INF directory .

    ② 初始化参数配置文件:

        <servlet>
            <servlet-name>dispatcher</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:s22-servlet.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>

     param-value 为 resources ,多种引入方式查看 8. Resources

    通常使用 classpath:a/b/c.xml

    Line n1: 2016-10-03 21:19:17 INFO XmlBeanDefinitionReader:317 - Loading XML bean definitions from ServletContext resource [/WEB-INF/s1-servlet.xml]
    Line n2: 2016-10-03 21:19:18 INFO XmlBeanDefinitionReader:317 - Loading XML bean definitions from class path resource [s22-servlet.xml]

    通过日志可以看出,默认形式使用的是servlet 规范,而自己指定 contextConfigLocation 则是 spring 的形式

    这两种效果相同,都是为了加载spring bean ,这些bean 是绑定servlet的,在谁的里面声明了,谁就能用,其他servlet不能使用。

    还可以说,如果不存在 root context,那么每个servlet 都有自己专属的上下文,自己声明的bean,只能在自己上下文中使用。

    如果存在 root context ,那么root context 中定义的bean 被所有servlet 共享,是整个应用的上下文。(参数名同servlet init ,必须使用listener)

          <!-- Configuration locations must consist of one or more comma- or space-delimited
            fully-qualified @Configuration classes. Fully-qualified packages may also be
            specified for component-scanning -->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:rootContext-Beans.xml</param-value>
        </context-param>
    
        <!-- Bootstrap the root application context as usual using ContextLoaderListener -->
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>

    <param-value>classpath:a.xml classpath:b.xml</param-value>

    <param-value>classpath:a.xml,classpath:b.xml</param-value>

    上面两个正确

    <param-value>classpath:a.xml b.xml</param-value>

    上面这个是错误的

    已验证。

  • 相关阅读:
    tkinter 表格
    Python编程学习笔记 随时更新
    WIN32窗口程序
    OutputDebugString方便格式化WIN32封装
    免费的剪贴板工具Ditto安装与使用
    Notepad++安装json插件
    华为机试训练题
    Python+Flask+MysqL的web建设技术过程
    python Django 用法总结(转)
    python Robot Framework用法总结(转)
  • 原文地址:https://www.cnblogs.com/zno2/p/5929344.html
Copyright © 2020-2023  润新知