Spring uses multiple xml configuration files

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.