Configure the contextConfigLocation method in the web.xml file in the java Web project javaEE

Configure the contextConfigLocation method in the web.xml file of the java Web project

1. What is the difference between classpath: and classpath * : in web.xml ?
1. classpath: Only look for files in your project class path;
2, classpath * : not only contains the class path, but also includes the jar file (class path) for searching.



2. Configure multiple configuration files by fuzzy matching
1. If the configuration file is in the jar package, the fuzzy matching cannot be found. Multiple configuration files can be configured separated by commas .
2. The configuration reference is as follows:
<context-param>  
    <param-name>contextConfigLocation</param-name>  
    <param-value>
 classpath*:applicationContext.xml,  
        classpath*:app-datasource.xml,  
        classpath*:app-memcached.xml,  
        classpath*:app-ibatis.xml,  
        classpath*:app-rest.xml  
    </param-value>  
</context-param>  


3. Different storage locations
1. Below src , you need to define the following in web.xml: (using classpath)
2. The configuration reference is as follows:
<context-param>  
<param-name>contextConfigLocation</param-name>  
<param-value>classpath:applicationContext.xml</param-value>  
< /context-param>



3. Below WEB-INF , you need to define the following in web.xml:
4. The configuration reference is as follows:
<context-param>  
<param-name>contextConfigLocation</param-name>  
<param-value>/WEB-INF/applicationContext*.xml</param-value>  
</context-param>  


4. How web.xml configures spring through contextConfigLocation
1. SSI framework configuration file path problem:
  • 1 + N paths of struts2: src+src (configurable) name: struts.xml + N
  • 1 path for spring: src name: applicationContext.xml
  • 1 + N paths of ibatis: src+src (configurable) name: SqlMapConfig.xml + N

2. After deploying to tomcat, the configuration files in the src directory will be automatically copied to the classes directory of the application, just like the class files.

3. When the spring configuration file starts, it loads applicationContext.xml in the WEB-INF directory.
The runtime uses applicationContext.xml in the WEB-INF/classes directory.

4. First of all, the configuration file related to Spring must start with "applicationContext-", and it must conform to the idea of ​​convention over configuration, which is much better in efficiency and error rate.

5. It is also best to put all Spring configuration files in a unified directory. If the project is large, you can also build a directory in sub-modules in this directory. This way the program doesn't look cluttered.
6. The configuration reference is as follows:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value> classpath*:**/applicationContext-*.xml</param-value>
</context-param>

7. " **/ " means any directory;
8. "**/applicationContext -* .xml" represents an XML file starting with "applicationContext-" in any directory.
You can modify it yourself as needed. It is best to put all Spring configuration files in a unified directory , such as:
9. The configuration reference is as follows:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value> classpath*:/spring/applicationContext-*.xml</param-value>
</context-param>




Guess you like

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