Configure multiple xml configuration files in spring

Source: Internet
Author: User

 

1. the contextconfiglocation parameter is defined in XML. spring will use this parameter to load. XML. without this parameter, spring loads Web-INF/applicationcontext by default. XML file.

<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,
Classpath *: CONF/spring/applicationcontext_staff *. XML,
Classpath *: CONF/spring/applicationcontext_security.xml
Classpath *: CONF/spring/applicationcontext_modules *. xml
Classpath *: CONF/spring/applicationcontext_cti *. xml
Classpath *: CONF/spring/applicationcontext_apm *. xml
</Param-value>
</Context-param>

The contextconfiglocation parameter defines the spring configuration file to be loaded. The principles are described as follows:
And use servletcontextlistener.
Spring provides an implementation class contextloaderlistener for servletcontextlistener, which can be used
Used for listener, which automatically locates the applicationcontext. xrnl file under the WEB-INF/at creation. Because
If there is only one configuration file named applicationcontext. XML
Add the following code to the file:
<Listener>
<Listener-class> org. springframework. Web. Context. contextloaderlistener </listener-class>
</Listener>
If multiple configuration files need to be loaded, use the <context-para (element) element to determine
File Name. Because the contextloaderlistener will find the parameter named contextconfiglocation when loading.
Therefore, the parameter name should be contextconfiglocation when context-Param is configured.
The Web. xml file with multiple configuration files is as follows:
<1 -- XML file header 2> 〉
<? XML version = "L. O" encoding = "job 80-8859-1"?>
<1 -- DTD and other information of the web. xm1 file 1> 〉
<! Doctype web-app
Public "-// Sun Microsystems. Work port C. // DTD web application 2.3 // en"
''Http: // java.sun.com/dtd/web-app_2_3.dtd''>
<Web-app>
<! Multiple configuration files are determined>
<Context-param>
<1 -- the parameter name is contextconfiglocation... > 〉
<Param-Name> contextconfiglocation </param-Name>
<! Multiple configuration files are separated by commas 〉
<Param-value>/Web-work NF/daocontext. xml./WEB-INF/Application
Context. xml </param-value>
</Context-param>
<! -- Use listener to create an applicat workoncontext instance -->
<Listener>
<Listener-class> org. SPR engineer ngframework. Web. Context. contextloader
Listener </listener-class>
</Listener>
</Web-app>
If no configuration file is specified for contextconfiglocation, spring automatically searches for the Application
Context. xrnl configuration file. If contextconfiglocation exists, the configuration file determined by this parameter is used.
A string specified by this parameter. Spring's contextloaderlistener is responsible for breaking this string into multiple
The configuration file, comma ",", space "", and Semicolon ";" can be used as character strings.
If neither the applicationcontext. xrnl file nor the contextconfiglocation parameter is used
Configuration file, or the configuration file specified by contextconfiglocation does not exist. Will cause spring to fail
The configuration file is loaded or the applicationcontext instance cannot be created normally.

Configuring a spring servlet for loading can achieve the same effect.
Load-on-startup servlet is used.
Spring provides a special servllet class: contextloaderservlet. When the servlet is started
Automatically searches for the applicationcontext. xml file on the web-in day.
Of course, to enable the contextloaderservlet to start with the application, you should configure this servlet
The value of load-on-startup's servleto load-on-startup is a little smaller, because you must ensure that the Application
Context is created first. If there is only one configuration file named applicationcontext. XML
Add the following code to the Web. xml file:
<Servlet>
<Servlet-Name> context </servlet port-Arne>
<Servlet-class> org. springframework. Web. Context. contextloaderservlet </
Servlet-class>
<Load-on-startup> L </load-O Port-startup>
</Servlet>

. The web with multiple configuration files and the NL file is as follows:
<! -- XML file header -->
<? XML version = "1.0" encoding = "job so-8859-1"?>
<! -- DTD and other information of the web. xml file →
<! Doctype web-app
Public "-// Sun Microsystems, working port C. // DTD web application 2.3 // en"
''Http: // java.sun.com/dtd/web-app_2_3.dtd''>
<Web-app>
<'One determine multiple configuration files 1'>
<Context-param>
<! -- Parameter name contextconfiglocation -->
<Param-Name> contextconfiglocation </param-Name>
<! -- Separate multiple configuration files with commas 〉
<Param-value>/Web-work NF/daocontext. XML ,! Web-job NF/applicationcontext.
XML </param-value>
</Context-param>
<! I. Use the load-on-startup servlet to create an applicat worker oncontext instance I> 〉
<Servlet>
<Servlet-narne> context </servlet-narne>
<Servlet-class> org. springframework. Web. Context. contextloader
Servlet </servlet-class>
<! It is more appropriate to have a smaller nominal value, and I will be given priority.> 〉
<Load-on-startup> L </load-on-startup>
</Servlet>
</Web-app>

2. Use a match character.

<Context-param>
<Param-Name> contextconfiglocation </param-Name>
<Param-value>/WEB-INF/applicationcontext *. xml </param-value>
</Context-param>

For example, Hibernate is used, the hibernate-related configuration is placed in the applicationContext-hibernate.xml file, and some global-related information is placed in applicationcontext. XML. Other configurations are similar. in this way, you can load it. Do not use spaces or commas to separate them!

3. If you use struts to load Multiple Spring configuration files, the following configuration is actually the contextconfiglocation variable.

Add this in struts-config.xml
<Plug-in classname = "org. springframework. Web. Struts. contextloaderplugin">
<Set-Property = "contextconfiglocation"
Value = "/WEB-INF/applicationcontext. XML,
/WEB-INF/action-servlet.xml, "/>

4. If a non-J2EE application is loaded.

Applicationcontext act = new classpathxmlapplicationcontext (New String [] {"bean1.xml", "bean2.xml "});

Beandefinitionregistry Reg = new defaultlistablebeanfactory ();

Xmlbeandefinitionreader = new xmlbeandefinitionreader (REG );

Reader. loadbeandefinitions (New classpathresource ("bean1.xml "));

Reader. loadbeandefinitions (New classpathresource ("bean2.xml "));

Beanfactory BF = (beanfactory) reg;

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.