美文网首页
Spring项目的配置文件解析

Spring项目的配置文件解析

作者: 巧克力er | 来源:发表于2017-11-17 15:33 被阅读0次

作者:ssunday

坐标:江苏 南京

TIP: 这个建议是针对于那些还在执着于 eclipse 编程的小伙伴们,既然我们都在使用的是 Spring 的框架,那么为什么部选择一款 Spring 的 IDEA ,强烈给大家推荐一款编辑器:### Spring Tool Suite™ (STS)

Spring MVC集成了Spring对象管理、“约定大于配置(CoC)”、函数式编程的思想以及现有MVC框架的特点于一身,成为Java领域Web项目中最流行的MVC框架。

当今流行的还有就是微服务使用的框架:Spring Boot,当然 Spring Boot 提倡的是 “习惯优与配置”。在Spring Boot 中使用大量的注解代替了 Xml 的配置问题,极大的简化了开发人员的,需要去解决不同需求的配置问题。有兴趣的同学可以去看下这本书《Spring Boot 实战》。

——好的回到我们今天的标题内容:

本文主要讨论的是:web.xml, root-context.xml, servlet-context.xml

web.xml: web.xml可以说是Web项目的驱动配置,Spring及Spring MVC的初始化就是写在这里。

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <!-- 配置Spring配置文件的加载路径 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>
    
    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <!-- 配置Spring容器的监听器,它将使用上配置的contextConfigLocation的值的路径来获得Spring配置文件的路径  -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <!-- SpringMVC的主控Servlet -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <!--SpringMVC处理的URL  -->    
    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

web.xml 的加载过程:

  • 当我们去启动一个 web 项目的时候,首先会加载 web.xml 这个文件,只有当这个文件加载完成,并且没有发生错误的时候项目才会启动。
  • 启动项目的时候会首先读取 web.xml 这个配置文件的两个节点,<context-param>, <listener>
  • 紧接着,容器创建一个ServletContext(application),这个WEB项目所有部分都将共享这个上下文。
  • 容器以<context-param></context-param>的name作为键,value作为值,将其转化为键值对,存入ServletContext。
  • 容器创建<listener></listener>中的类实例,根据配置的class类路径<listener-class>来创建监听,在监听中会有contextInitialized(ServletContextEvent args)初始化方法,启动Web应用时,系统调用Listener的该方法,在这个方法中获得:
ServletContext application =ServletContextEvent.getServletContext();
context-param的值= application.getInitParameter("context-param的键");
  • 得到这个context-param的值之后,你就可以做一些操作了。
  • 接下来容器会读取<filter> 当然如果有filter,然后实例化这个filter。
  • 到这里整个项目还没有完全启动,最后是 <sevlet> 在用户第一次请求的时候会实例化这个servlet。

到这里整个web.xml 就实例化结束了,综上所述:真个过程可以总结为

<context-param>---> <listener>---><filter>---><servlet>

<servlet>配置

DispatcherServlet类是Spring MVC的转发控制器,所以需要指明初始化DispatcherServlet类的必要信息。此例中是设置contextConfigLocation参数的值为/WEB-INF/spring/appServlet/servlet-context.xml,Spring MVC框架的controller配置、静态资源配置、上传文件配置都是写在这个xml文件中。

servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd 
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> 

    <!-- DispatcherServlet Context: defines this servlet's request-processing 
        infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <!-- 开启注解 -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving 
        up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources 
        in the /WEB-INF/views directory -->
    <beans:bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <!-- 使用组件扫描,将action扫描出来,在spring容器中进行注册,自动对action在spring容器中进行配置 -->
    <context:component-scan base-package="com.lw.myapp.controller" />

</beans:beans>

root-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <!-- Root Context: defines shared resources visible to all other web components -->
        
</beans>

相关文章

网友评论

      本文标题:Spring项目的配置文件解析

      本文链接:https://www.haomeiwen.com/subject/uhwdvxtx.html