Spring configuration file –applicationcontext.xml file pathJava Programming
The default address for spring configuration file Applicationcontext.xml is Web-inf, as long as the code is added to Web. xml
1 2 3 5 |
< Span style= "FONT-SIZE:16PX;" >< listener > < > org.springframework.web.context.contextloaderlistener </ listener-class > </ listener > |
Spring will be loaded automatically
But in the actual development process, we may need to adjust the position of the Applicationcontext.xml to make the program structure more clear. In Web. XML, the code for configuring the spring configuration file is as follows:
1 2 3 4 |
< context-param > < param-name >contextConfigLocation</ param-name > < param-value >这里写路劲</ param-value > </ context-param > |
According to the API description of the spring framework, there are four ways to configure the Applicationcontext.xml file path
1./web-inf/applicationcontext.xml
2. Com/config/applicationcontext.xml
3. File:c:/javacode/springdemo/com/config/applicationcontext.xml
4. Classpath:com/config/applicationcontext.xml
Note: The above path is just an example, specific use or to the real project, do the programming of this point extrapolate ability or some of it
In the development process, if spring's configuration file applicationcontext.xml is not loaded, the general return of such errors
Could not open ServletContext resource [/web-inf/applicationcontext.xml]
Here's a common way for you to customize Applicationcontext.xml paths for example.
1. Spring configuration file under Web-inf
In this case you can not take care of him, do not configure, because spring will default to load, if it must be configured, you can
1 2 3 4 |
< context-param > < param-name >contextConfigLocation</ param-name > < param-value >WEB-INF/applicationContext.xml</ param-value > </ context-param > |
2, spring configuration file under Web-inf under a folder, such as config, you can configure this
1 2 3 |
< context-param > < param-name >contextConfigLocation</ param-name > < param-value >WEB-INF/config/applicationContext.xml</ param-value > </ context-param > |
3, spring configuration file under SRC, this can be configured
1 2 3 |
< init-param > < param-name >contextConfigLocation</ param-name > < param-value >classpath:applicationContext.xml</ param-value > </init -param > |
Or
1 2 3 |
< context-param > < param-name >contextConfigLocation</ param-name > < param-value >classpath:applicationContext.xml</ param-value > </ context-param > |
4, spring configuration file in a package under SRC, such as Com.config, can be configured like this
1 2 3 |
< context-param > < param-name >contextConfigLocation</ param-name > < param-value >WEB-INF/classes/com/config/applicationContext.xml</ param-value > </ context-param > |
Or
1 2 3 |
< context-param > < param-name >contextConfigLocation</ param-name > < param-value >classpath:com/config/applicationContext.xml</ param-value > </ context-param > |
Article source http://www.codingwhy.com/81.html
Detailed –applicationcontext.xml file path for spring configuration file