Spring uses wildcards to load configuration files classpath and WEB-INF/classes problem

First, write the code structure first.

 

Second, look at the configuration in web.xml.

copy code
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>SpringMVC</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <context-param>
            <param-name>contextConfigLocation</param-name>
            <!-- <param-value>classpath*:config/applicationContext.xml</param-value> -->
            <param-value>/WEB-INF/classes/config/applicationContext.xml</param-value>
  </context-param>
  
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
     <servlet> 
        <servlet-name>springmvc</servlet-name> 
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
           <param-name>contextConfigLocation</param-name>
           <!-- <param-value>classpath*:config/Springmvc-servlet.xml</param-value> -->
           <param-value>/WEB-INF/classes/config/Springmvc-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup> 
    </servlet>  
    <servlet-mapping> 
        <servlet-name>springmvc</servlet-name> 
        <url-pattern>/</url-pattern>  
    </servlet-mapping>
</web-app>
copy code

 

Third, the following is the description of the configuration file.

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
ContextLoaderListener is Spring's listener. Its function is to automatically assemble the configuration information of the ApplicationContext when the Web container is started. Because it implements the ServletContextListener interface, the listener is configured in web.xml and the method it implements will be executed by default when the container is started.

  <context-param>
            <param-name>contextConfigLocation</param-name>
            <!-- <param-value>classpath*:config/applicationContext.xml</param-value> -->
            <param-value>/WEB-INF/classes/config/applicationContext.xml</param-value>
  </context-param>

This configuration is used to specify the location of the applicationContext.xml configuration file, which can be specified through context-param:

Here we need to figure out what classpath is and what is the difference between classpath: and classpath*:

1. First classpath refers to the classes directory under the WEB-INF folder

2. The difference between classpath and classpath*: 
classpath: will only look for files in your class path; 
classpath*: not only include the class path, but also the jar file (class path) for search. 

 

If the applicationContext.xml configuration file is stored in the src directory, just like the storage location in the above code structure, then the configuration in web.xml is as follows:

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

If the applicationContext.xml configuration file is stored under WEB-INF, then the configuration in web.xml is as follows:

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/applicationContext*.xml</param-value>
    </context-param>

It should be noted that after deploying to the application server, the configuration file in the src directory will be automatically copied to the classes directory of the application like the class file. When the spring configuration file is started, the applicationContext in the web-info directory is loaded. .xml, the applicationContext.xml in the web-info/classes directory is used at runtime. Therefore, regardless of whether the applicationContext.xml configuration file is stored in the src directory or under WEB-INF, the path can be configured in the following way:

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/applicationContext*.xml</param-value>
    </context-param>

When multiple configuration files are loaded, the following code can be used to configure:

copy code
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value> 
            classpath*:conf/spring/applicationContext_core*.xml,
            classpath*:conf/spring/applicationContext_dict*.xml,
            classpath*:conf/spring/applicationContext_hibernate.xml,
            ......
        </param-value>
    </context-param>
copy code

It can also be used in the following way:

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:**/applicationContext-*.xml</param-value>
    </context-param>

"**/" means any directory; 

"**/applicationContext-*.xml" represents an XML file starting with "applicationContext-" in any directory. 

The Spring configuration file is best to start with "applicationContext-", and it is best to put all Spring configuration files in a unified directory, or you can create them in modules.

   Reprinted from https://www.cnblogs.com/cczz_11/p/4363314.html, only for learning and communication, copyright deletion

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325218033&siteId=291194637