spring-xml configuration

Explanation

  • The spring default configuration file needs to be configured in WEB-INF, if not configured, the container will fail to load

Configure WEB.XML

  • Configure listener, why do you need a listener?

    • Because let the listener listen to when the ServiceContextListener starts, and load the spring container when starting, to ensure that there is only one spring container in the WEB project
      <listener>s
          <listener-class >
             org.springframework.web.context.ContextLoaderListener
          </listener-class>
      </listener>
    
  • Load spring configuration file

    • The configuration file named applicationContext.xml in the WEB-INF directory is loaded by default
    • If you change the directory where the configuration file is located, you need to configure it to take effect. The default directory: WEB-INF
    • If you change the name of the configuration file, you need to configure it to take effect, the default name: applicationContext.xml
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <!-- 加载src下的配置文件. 可以使用通配符的方式 -->
        <param-value>classpath : *. xml</param-value>
        <!-- 加载scr下所有以bean开头的配置文件 -->
        <param-value>classpath : bean-*. xml</param-value>
        <!-- 这是加载其他路径的配置文件 -->
        <param-value>classpath:config/spring/bean.xml</param-value>
    </context-param>
    
  • Configure encoding filters

    • Loaded at startup, each request will be filtered
        <filter>
            <filter-name>encodingFilter</filter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
            <init-param>
                <param-name>encoding</param-name>
                <param-value>UTF-8</param-value>
            </init-param>
            <init-param>
                <param-name>forceEncoding</param-name>
                <param-value>true</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>encodingFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    
Published 20 original articles · Likes0 · Visits 930

Guess you like

Origin blog.csdn.net/vistaed/article/details/105558380