Spring mvc项目的web.xml以及注释

时间:2023-03-10 07:14:49
Spring mvc项目的web.xml以及注释

web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app id="WebApp_ID" version="3.0"
  3.    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  5.    <context-param>
  6.       <param-name>log4jConfigLocation</param-name>
  7.       <param-value>classpath:config/properties/log4j.properties</param-value>
  8.    </context-param>
  9.    <listener>
  10.       <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  11.    </listener>
  12. <!-- context-param中配置的contextConfigLocation只对ContextLoaderListener有效 不会对DispatcherServlet有效 -->
  13. <!-- DispatcherServlet用来加载mvc相关的bean ContextLoaderListener用来加载其他bean -->
  14. <!-- ContextLoaderListener会生成一个spring的context 注:DispatcherServlet不会识别@controller注解-->
  15.    <context-param>
  16.       <param-name>contextConfigLocation</param-name>
  17.       <param-value>/WEB-INF/config/spring-bean/*.xml</param-value>
  18.    </context-param>
  19.    <listener>
  20.       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  21.    </listener>
  22. <!-- 配置DispatcherServlet load-on-startup要為1 它會根据contextConfigLocation生成一個spring的context-->
  23.    <servlet>
  24.       <servlet-name>spring</servlet-name>
  25.       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  26.       <init-param>
  27.          <param-name>contextConfigLocation</param-name>
  28.          <param-value>/WEB-INF/config/spring-servlet.xml</param-value>
  29.       </init-param>
  30.       <load-on-startup>1</load-on-startup>
  31.    </servlet>
  32.    <servlet-mapping>
  33.       <servlet-name>spring</servlet-name>
  34.       <url-pattern>/</url-pattern>
  35.    </servlet-mapping>
  36.  <filter>
  37.         <filter-name>encodingFilter</filter-name>
  38.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  39.         <init-param>
  40.             <param-name>encoding</param-name>
  41.             <param-value>UTF-8</param-value>
  42.         </init-param>
  43.         <init-param>
  44.             <param-name>forceEncoding</param-name>
  45.             <param-value>true</param-value>
  46.         </init-param>
  47.     </filter>
  48.     <filter-mapping>
  49.         <filter-name>encodingFilter</filter-name>
  50.         <url-pattern>/*</url-pattern>
  51.     </filter-mapping>
  52. </web-app>

ContextLoaderListener中加载的context成功后,spring 将 applicationContext存放在ServletContext中key值为"org.springframework.web.context.WebApplicationContext.ROOT"的attribute中。

servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);

可以通过WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext)或WebApplicationContextUtils.getWebApplicationContext(servletContext)方法来获取对应的applicationContext。

DispatcherServlet加载的context成功后,如果 publishContext属性的值设置为true的话(缺省为true) 会将applicationContext存放在org.springframework.web.servlet.FrameworkServlet.CONTEXT. + (servletName)的attribute中。

在每次request请求时,DispatcherServlet会将此applicationContext存放在request中,

可以通过 RequestContextUtils.getWebApplicationContext 或 WebApplicationContextUtils.getWebApplicationContext(servletContext,attrname)方法 来获取对应的applicationContext。

在配置的过程中尽量让DispatcherServlet和ContextLoaderListener职责分离,避免一个bean被加载两次。

或者只使用DispatcherServlet(没有试过 应该可行)。

从web.xml的配置可知ContextLoaderListener会先于DispatcherServlet创建ApplicationContext,DispatcherServlet在创建ApplicationContext时会先找到由ContextLoaderListener所创建的ApplicationContext,再将后者的ApplicationContext作为参数传给DispatcherServlet的ApplicationContext的setParent()方法。

当Spring在执行ApplicationContext的getBean时,如果在自己context中找不到对应的bean,则会在父ApplicationContext中去找。这也解释了为什么我们可以在DispatcherServlet中获取到由ContextLoaderListener对应的ApplicationContext中的bean。

spring-servlet.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
  4.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  6.        http://www.springframework.org/schema/beans/spring-beans.xsd
  7.        http://www.springframework.org/schema/context
  8.        http://www.springframework.org/schema/context/spring-context.xsd
  9.        http://www.springframework.org/schema/tx
  10.        http://www.springframework.org/schema/tx/spring-tx.xsd
  11.           http://www.springframework.org/schema/mvc
  12.        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
  13.     <!-- 配置扫描的包 这里配置的包和它的下级包都会被扫描 这样不仅会扫描@controller @service @repository 还会扫描@component-->
  14.     <context:component-scan base-package="com.zjf" />
  15.      <!-- 注册HandlerMapper、HandlerAdapter两个映射类 这是spring为@Controller分发请求所必须的 同时也是表单数据转换为对象(如xml json等)必须的-->
  16.     <mvc:annotation-driven />
  17.      <!-- 访问静态资源 如果没有这个配置 那么静态的js css文件将会报错 因为在web.xml中我们配置了<url-pattern>/</url-pattern> -->
  18.     <mvc:default-servlet-handler />
  19.     <!-- 视图解析器 这里使用InternalResourceViewResolver -->
  20.     <bean
  21.         class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  22.         <property name="prefix" value="/WEB-INF/view/"></property>
  23.         <property name="suffix" value=".jsp"></property>
  24.     </bean>
  25. </beans>