zoukankan      html  css  js  c++  java
  • SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-002- 在xml中引用Java配置文件,声明DispatcherServlet、ContextLoaderListener

    一、所有声明都用xml

    1.

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <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
     3 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
     4     <context-param>
     5         <param-name>contextConfigLocation</param-name>
     6         <param-value>/WEB-INF/spring/root-context.xml</param-value>
     7     </context-param>
     8     <listener>
     9         <listener-class>
    10             org.springframework.web.context.ContextLoaderListener
    11         </listener-class>
    12     </listener>
    13     <servlet>
    14         <servlet-name>appServlet</servlet-name>
    15         <servlet-class>
    16             org.springframework.web.servlet.DispatcherServlet
    17         </servlet-class>
    18         <load-on-startup>1</load-on-startup>
    19     </servlet>
    20     <servlet-mapping>
    21         <servlet-name>appServlet</servlet-name>
    22         <url-pattern>/</url-pattern>
    23     </servlet-mapping>
    24 </web-app>

    如果没有指定DispatcherServlet 的配置文件,则会自动找根路径下的"servlet名称"-context.xml。DispatcherServlet loads its application context with beans defined in a file whose name is based on the servlet name. In listing 7.3, the servlet is named appServlet .Therefore, DispatcherServlet loads its application context from an XML file at /WEB-INF /appServlet-context.xml.

    当然也可以指定配置文件

     1 <servlet>
     2     <servlet-name>appServlet</servlet-name>
     3     <servlet-class>
     4         org.springframework.web.servlet.DispatcherServlet
     5     </servlet-class>
     6     <init-param>
     7         <param-name>contextConfigLocation</param-name>
     8         <param-value>
     9             /WEB-INF/spring/appServlet/servlet-context.xml
    10         </param-value>
    11     </init-param>
    12     <load-on-startup>1</load-on-startup>
    13 </servlet>

    二、在xml中引入Jav配置文件

    1.如何在xml中引入以 @Configuration 注解的Java配置文件呢?

    To use Java-based configuration in Spring MVC , you need to tell DispatcherServlet and ContextLoaderListener to use AnnotationConfigWebApplicationContext , an implementation of WebApplicationContext that loads Java configuration classes instead of XML . You can do that by setting the contextClass context parameter and initialization parameter for DispatcherServlet .

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <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
     3 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
     4     <context-param>
     5         <param-name>contextClass</param-name>
     6         <param-value>
     7             org.springframework.web.context.support.AnnotationConfigWebApplicationContext
     8         </param-value>
     9     </context-param>
    10     <context-param>
    11         <param-name>contextConfigLocation</param-name>
    12         <param-value>com.habuma.spitter.config.RootConfig</param-value>
    13     </context-param>
    14     <listener>
    15         <listener-class>
    16             org.springframework.web.context.ContextLoaderListener
    17         </listener-class>
    18     </listener>
    19     <servlet>
    20         <servlet-name>appServlet</servlet-name>
    21         <servlet-class>
    22             org.springframework.web.servlet.DispatcherServlet
    23         </servlet-class>
    24         <init-param>
    25             <param-name>contextClass</param-name>
    26             <param-value>
    27                 org.springframework.web.context.support.AnnotationConfigWebApplicationContext
    28             </param-value>
    29         </init-param>
    30         <init-param>
    31             <param-name>contextConfigLocation</param-name>
    32             <param-value>
    33                 com.habuma.spitter.config.WebConfigConfig
    34             </param-value>
    35         </init-param>
    36         <load-on-startup>1</load-on-startup>
    37     </servlet>
    38     <servlet-mapping>
    39         <servlet-name>appServlet</servlet-name>
    40         <url-pattern>/</url-pattern>
    41     </servlet-mapping>
    42 </web-app>
  • 相关阅读:
    Vue项目中svg图标不能正常显示
    Django Rest Framework
    Django contenttype
    Django CKEdirtor配置(图片上传,粘贴,文件上传)
    [SDOI2017] 数字表格
    [SDOI2015] 约数个数和
    [BZOJ4407] 于神之怒加强版
    [SRM625 Div1 Hard] Seatfriends
    [SRM613~] TaroCheckers
    [玲珑杯1138] 震惊,99%的中国人都会算错的问题
  • 原文地址:https://www.cnblogs.com/shamgod/p/5245059.html
Copyright © 2011-2022 走看看