Specify the location of the spring configuration file

Source: Internet
Author: User
Tags config

This article uses spring3.1.0, which mainly explains spring's configuration file default location and the location of the specified spring configuration file.

1. Default location

A) Default MVC configuration file
Configure in the Web. xml file:

<!--Front Controller--
<servlet>
	<servlet-name>annomvc</servlet-name>
	< Servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class>	
</servlet>
<servlet-mapping>
	<servlet-name>annomvc</servlet-name>
	<url-pattern>/< /url-pattern>
</servlet-mapping>

Specify spring to handle the requested Servlet, by default the address of the MVC configuration file is:/web-inf/${servletname}-servlet.xml, the MVC profile found by default in the example we configured is:/web-inf/ Annomvc-servlet.xml.

B) Other configuration files

The other configuration files here refer to the configuration of the DataSource, the configuration of the persistence layer, the configuration information of the service layer, etc. To load additional configuration files, you need to include a Contextloaderlistener listener in the Web. XML configuration file to configure. The Contextloaderlistener only listens for beans outside the initial chemical control MVC-related configuration. The code is as follows:

<!--context Load listener--
<listener>
	<listener-class> Org.springframework.web.context.contextloaderlistener</listener-class>
</listener>


If no other parameters are specified, the default lookup profile location is:/web-inf/applicationcontext.xml.

2) Specify the configuration file location

A) Modify the MVC configuration file location

To modify the location of the MVC configuration file, you need to specify the location of the MVC configuration file when configuring Dispatcherservlet. For example, if you want to put Annomvc-servlet.xml in src/config/annomvc-servlet.xml, you need to specify the <init-param> tag when configuring Dispatcherservlet. The specific code is as follows:

<!--Front Controller--
<servlet>
	<servlet-name>annomvc</servlet-name>
	< Servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class>
	<init-param >
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:config/ annomvc-servlet.xml</param-value>
	</init-param>
</servlet>
<servlet-mapping >
	<servlet-name>annomvc</servlet-name>
	<url-pattern>/</url-pattern>
</servlet-mapping>


B) Modify other configuration file Locations

To modify the configuration file location of a bean other than the MVC configuration file, you need to include the <context-param> tag in Web. XML and specify a specific location. We have three configuration files (Service-context.xml, Persistence-context.xml, datasource-context.xml) They are all located under the src/config/folder, then the configuration code is as follows:


<!--context Load listener--
<listener>
	<listener-class> org.springframework.web.context.contextloaderlistener</listener-class>
</listener>
< context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>
  		Classpath:config/service-context.xml
  		classpath:config/persistence-context.xml
  		classpath:config/ Datasource-context.xml
 		</param-value>
</context-param>

OK, in this position you can place your configuration files as you like.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.