[springMVC学习]1、springmvc设置spring配置文件

生活中,最使人疲惫的往往不是道路的遥远,而是心中的郁闷;最使人痛苦的往往不是生活的不幸,而是希望的破灭;最使人颓废的往往不是前途的坎坷,而是自信的丧失;最使人绝望的往往不是挫折的打击,而是心灵的死亡。所以我们要有自己的梦想,让梦想的星光指引着我们走出落漠,走出惆怅,带着我们走进自己的理想。

导读:本篇文章讲解 [springMVC学习]1、springmvc设置spring配置文件,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

        我们给springmvc设置配置文件,实际上就是让某个类在tomcat启动时,自动的去读取配置文件,完成ioc容器的创建,完成初始化工作。在spring中,我们是手动new出来,但是在springMVC中就是通过配置了,配置文件路径后就会自己创建spring容器。

        DispathcerServlet是springMVC的核心,这个类用于所有请求的处理,分发等等,这个类非常的复杂,如果是第一次学springMVC,那么现在会用即可。该类的类图和功能如下

[springMVC学习]1、springmvc设置spring配置文件

 [springMVC学习]1、springmvc设置spring配置文件

        DispatcherServlet为springMVC的核心控制器,用于请求的分发,本质还是一个Servlet,我们在web.xml中配置该Servlet,让其拦截所有请求。

    <servlet>
        <servlet-name>springConfig</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springConfig</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

        我们在配置好DiapatcherServlet之后,需要配置spring的配置文件,将其传给DispathcerServlet。常见的有2种方式,一种是通过init-param进行设置,步骤如下:

        第一步先在src下面创建spring配置文件

[springMVC学习]1、springmvc设置spring配置文件

        接下来就是通过init-param进行配置 

    <servlet>
        <servlet-name>springConfig</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springConfig</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

        另外一种方式就是通过名称来进行配置,相对比较方便。

        配置之前,先来看下DiapatcherServlet的源代码,下面的代码为DiapatcherServlet类的父类的一部分

/**
 * Base servlet for Spring's web framework. Provides integration with
 * a Spring application context, in a JavaBean-based overall solution.
 *
 * <p>This class offers the following functionality:
 * <ul>
 * <li>Manages a {@link org.springframework.web.context.WebApplicationContext
 * WebApplicationContext} instance per servlet. The servlet's configuration is determined
 * by beans in the servlet's namespace.
 * <li>Publishes events on request processing, whether or not a request is
 * successfully handled.
 * </ul>
 *
 * <p>Subclasses must implement {@link #doService} to handle requests. Because this extends
 * {@link HttpServletBean} rather than HttpServlet directly, bean properties are
 * automatically mapped onto it. Subclasses can override {@link #initFrameworkServlet()}
 * for custom initialization.
 *
 * <p>Detects a "contextClass" parameter at the servlet init-param level,
 * falling back to the default context class,
 * {@link org.springframework.web.context.support.XmlWebApplicationContext
 * XmlWebApplicationContext}, if not found. Note that, with the default
 * {@code FrameworkServlet}, a custom context class needs to implement the
 * {@link org.springframework.web.context.ConfigurableWebApplicationContext
 * ConfigurableWebApplicationContext} SPI.
 *
 * <p>Accepts an optional "contextInitializerClasses" servlet init-param that
 * specifies one or more {@link org.springframework.context.ApplicationContextInitializer
 * ApplicationContextInitializer} classes. The managed web application context will be
 * delegated to these initializers, allowing for additional programmatic configuration,
 * e.g. adding property sources or activating profiles against the {@linkplain
 * org.springframework.context.ConfigurableApplicationContext#getEnvironment() context's
 * environment}. See also {@link org.springframework.web.context.ContextLoader} which
 * supports a "contextInitializerClasses" context-param with identical semantics for
 * the "root" web application context.
 *
 * <p>Passes a "contextConfigLocation" servlet init-param to the context instance,
 * parsing it into potentially multiple file paths which can be separated by any
 * number of commas and spaces, like "test-servlet.xml, myServlet.xml".
 * If not explicitly specified, the context implementation is supposed to build a
 * default location from the namespace of the servlet.
 *
 * <p>Note: In case of multiple config locations, later bean definitions will
 * override ones defined in earlier loaded files, at least when using Spring's
 * default ApplicationContext implementation. This can be leveraged to
 * deliberately override certain bean definitions via an extra XML file.
 *
 * <p>The default namespace is "'servlet-name'-servlet", e.g. "test-servlet" for a
 * servlet-name "test" (leading to a "/WEB-INF/test-servlet.xml" default location
 * with XmlWebApplicationContext). The namespace can also be set explicitly via
 * the "namespace" servlet init-param.
 *
 * <p>As of Spring 3.1, {@code FrameworkServlet} may now be injected with a web
 * application context, rather than creating its own internally. This is useful in Servlet
 * 3.0+ environments, which support programmatic registration of servlet instances. See
 * {@link #FrameworkServlet(WebApplicationContext)} Javadoc for details.
**/

[springMVC学习]1、springmvc设置spring配置文件

         上面的注释就说明我们前面的第一种方式就是显示指定配置文件,当然,我们也可以不指定,默认就会在/WEB-INF/下寻找  xxx-servlet.xml的文件作为配置文件,xxx就是我们在web.xml中配置的DispatcherServlet的servlet-name

    <servlet>
        <servlet-name>springConfig</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springConfig</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

         这里我们的servlet-name配置的是springConfig,那么当项目启动时,默认就会去读取springConfig-servlet.xml作为spring的配置文件,我们只需要在/WEB-INF/下创建该文件即可

[springMVC学习]1、springmvc设置spring配置文件


         由于这个是springmvc了,所以我们在spring配置文件中加入以下的基本配置

    <!--开启注解驱动-->
    <mvc:annotation-driven/>
    <!--springmvc不能处理的交由tomcat处理-->
    <mvc:default-servlet-handler/>

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/140250.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!