search
HomeBackend DevelopmentXML/RSS TutorialDetailed introduction to Spring's code examples for using multiple xml configuration files

Spring uses multiple xml configuration files, friends in need can refer to them.

1, Define the contextConfigLocation parameter in web.xml. Spring will use this parameter to load all comma-delimited xml. Without this parameter, spring loads the web-inf/applicationContext.xml file by default.

<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*.xmlclasspath*: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 principle is explained as follows:

1. Use ServletContextListener to implement.
Spring provides an implementation class of ServletContextListener, ContextLoaderListener, which can be used

as a listener. It will automatically find the applicationContext.xrnl file under WEB-INF/ when it is created. Therefore, if there is only one configuration file and the file name is applicationContext.xml, you only need to add the following code to the web.xml

file:


<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>

If there are multiple configuration files that need to be loaded, consider using the file name of the configuration file. Because when ContextLoaderListener loads, it will look for a parameter named contextConfigLocation.
Therefore, the parameter name should be contextConfigLocation when configuring context-param.

The web.xml file with multiple configuration files is as follows:

<1-- XML 文件的文件头二〉<?xml version="l.O" encoding="工80-8859-1"?>
< 1-- web.xm1 文件的DTD 等信息一〉<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems. 工口c.//DTD Web Application 2.3//EN"&#39;&#39;http://java.sun.com/dtd/web-app_2_3.dtd&#39;&#39;>
<web-app><!一确定多个配置文件>
<context-param><1-- 参数名为contextConfigLocation…〉
<param-name>contextConfigLocation</param-name><!一多个配置文件之间以,隔开二〉
<param-value>/WEB-工NF/daoContext.xml./WEB-INF/applicationContext.xml</param-value>
</context-param><!-- 采用listener创建Applicat工onContext 实例-->
<listener><listener-class>org.spr工ngframework.web.context.ContextLoader
Listener</listener-class></listener></web-app>

If there is no contextConfigLocation specified configuration file, Spring will automatically find the application

Context.xrnl configuration file. If there is contextConfigLocation, use the configuration file determined by this parameter.

This parameter specifies a string. Spring's ContextLoaderListener is responsible for decomposing the string into multiple

configuration files. Commas ",", spaces "" and semicolons ";" can all be used as characters string delimiter.

If there is neither an applicationContext. All will cause Spring to be unable to


Load the configuration file or fail to create the ApplicationContext instance normally

Configuring a servlet set by spring for loading can achieve the same effect.


Use load-on-startup Servlet accomplish.

Spring provides a special Servllet class: ContextLoaderServlet. When the Servlet is started, it will


automatically search for the applicationContext.xml file under WEB-IN.

Of course, in order to allow the ContextLoaderServlet to start when the application starts, this Servlet should be configured as


Load-on-startup The value of Servleto load-on-startup is smaller, which is more appropriate, because it is necessary to ensure that Application

Context is created first. If there is only one configuration file and the file name is applicationContext.xml, then add the following code to the


web.xml file:

<servlet>
<servlet-name>context</servlet-name><servlet-class>org.springframework.web.context.ContextLoaderServlet</
servlet-class><load-on-startup>l</load-on-startup>
</servlet>

. The web and nl files with multiple configuration files are as follows:

<!-- XML 文件的文件头--><?xml version="1.0" encoding="工SO-8859-1"?>
<! -- web.xml 文件的DTD 等信息→<!DOCTYPE web-appPUBLIC "-//Sun Microsystems , 工口c.//DTD Web Application 2.3//EN"
&#39;&#39;http://java.sun.com/dtd/web-app_2_3.dtd&#39;&#39;><web-app>
<&#39;一确定多个配置文件一><context-param>
<!-- 参数名为contextConfigLocation--><param-name>contextConfigLocation</param-name><!-- 多个配置文件之间以,隔开一〉
<param-value>/WEB-工NF/daoContext.xml, !WEB-工NF/applicationContext.xml</param-value>
</context-param><!一采用load-on-startup Servlet 创建Applicat工onContext 实例一〉
<servlet><servlet-narne>context</servlet-narne>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<!一下面值小一点比较合适,会优先加载一〉<load-on-startup>l</load-on-startup></servlet>
</web-app>

2, use the matching character

<context-param><param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml</param-value></context-param>

. For example, if Hibernate is used, put the hibernate related configuration in applicationContext- hibernate.xml is a file, and some global related information is placed in applicationContext.xml. Other configurations are similar. In this way, it can be loaded without writing separated by spaces or commas!

3, if you use struts to load multiple spring configuration files. The following configuration is actually the contextConfigLocation variable.

struts-config.xml add this

<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"><set-property property="contextConfigLocation"
value="/WEB-INF/applicationContext.xml,/WEB-INF/action-servlet.xml,,,,,,,"/>

4, if it is non-j2ee Apply direct program loading.

ApplicationContext act = new ClassPathXmlApplicationContext(new
String[]{"bean1.xml","bean2.xml"});BeanDefinitionRegistry reg = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(reg);
reader.loadBeanDefinitions(new ClassPathResource("bean1.xml"));
reader.loadBeanDefinitions(new ClassPathResource("bean2.xml"));
BeanFactory bf = (BeanFactory)reg;

Finally, explain the difference between the following classpath*: and classpath:

classpath*: appears to load the same file from multiple jar files.classpath: Only the first file found can be loaded.

The above is the detailed content of Detailed introduction to Spring's code examples for using multiple xml configuration files. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
RSS Documents: The Foundation of Web SyndicationRSS Documents: The Foundation of Web SyndicationApr 18, 2025 am 12:04 AM

RSS documents are XML-based structured files used to publish and subscribe to frequently updated content. Its main functions include: 1) automated content updates, 2) content aggregation, and 3) improving browsing efficiency. Through RSSfeed, users can subscribe and get the latest information from different sources in a timely manner.

Decoding RSS: The XML Structure of Content FeedsDecoding RSS: The XML Structure of Content FeedsApr 17, 2025 am 12:09 AM

The XML structure of RSS includes: 1. XML declaration and RSS version, 2. Channel (Channel), 3. Item. These parts form the basis of RSS files, allowing users to obtain and process content information by parsing XML data.

How to Parse and Utilize XML-Based RSS FeedsHow to Parse and Utilize XML-Based RSS FeedsApr 16, 2025 am 12:05 AM

RSSfeedsuseXMLtosyndicatecontent;parsingtheminvolvesloadingXML,navigatingitsstructure,andextractingdata.Applicationsincludebuildingnewsaggregatorsandtrackingpodcastepisodes.

RSS Documents: How They Deliver Your Favorite ContentRSS Documents: How They Deliver Your Favorite ContentApr 15, 2025 am 12:01 AM

RSS documents work by publishing content updates through XML files, and users subscribe and receive notifications through RSS readers. 1. Content publisher creates and updates RSS documents. 2. The RSS reader regularly accesses and parses XML files. 3. Users browse and read updated content. Example of usage: Subscribe to TechCrunch's RSS feed, just copy the link to the RSS reader.

Building Feeds with XML: A Hands-On Guide to RSSBuilding Feeds with XML: A Hands-On Guide to RSSApr 14, 2025 am 12:17 AM

The steps to build an RSSfeed using XML are as follows: 1. Create the root element and set the version; 2. Add the channel element and its basic information; 3. Add the entry element, including the title, link and description; 4. Convert the XML structure to a string and output it. With these steps, you can create a valid RSSfeed from scratch and enhance its functionality by adding additional elements such as release date and author information.

Creating RSS Documents: A Step-by-Step TutorialCreating RSS Documents: A Step-by-Step TutorialApr 13, 2025 am 12:10 AM

The steps to create an RSS document are as follows: 1. Write in XML format, with the root element, including the elements. 2. Add, etc. elements to describe channel information. 3. Add elements, each representing a content entry, including,,,,,,,,,,,. 4. Optionally add and elements to enrich the content. 5. Ensure the XML format is correct, use online tools to verify, optimize performance and keep content updated.

XML's Role in RSS: The Foundation of Syndicated ContentXML's Role in RSS: The Foundation of Syndicated ContentApr 12, 2025 am 12:17 AM

The core role of XML in RSS is to provide a standardized and flexible data format. 1. The structure and markup language characteristics of XML make it suitable for data exchange and storage. 2. RSS uses XML to create a standardized format to facilitate content sharing. 3. The application of XML in RSS includes elements that define feed content, such as title and release date. 4. Advantages include standardization and scalability, and challenges include document verbose and strict syntax requirements. 5. Best practices include validating XML validity, keeping it simple, using CDATA, and regularly updating.

From XML to Readable Content: Demystifying RSS FeedsFrom XML to Readable Content: Demystifying RSS FeedsApr 11, 2025 am 12:03 AM

RSSfeedsareXMLdocumentsusedforcontentaggregationanddistribution.Totransformthemintoreadablecontent:1)ParsetheXMLusinglibrarieslikefeedparserinPython.2)HandledifferentRSSversionsandpotentialparsingerrors.3)Transformthedataintouser-friendlyformatsliket

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor