Home Backend Development XML/RSS Tutorial Detailed introduction to Spring's code examples for using multiple xml configuration files

Detailed introduction to Spring's code examples for using multiple xml configuration files

Mar 08, 2017 pm 04:53 PM

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>
Copy after login

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>
Copy after login

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>
Copy after login

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>
Copy after login

. 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>
Copy after login

2, use the matching character

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

. 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,,,,,,,"/>
Copy after login

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;
Copy after login

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 of this Website
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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
A new programming paradigm, when Spring Boot meets OpenAI A new programming paradigm, when Spring Boot meets OpenAI Feb 01, 2024 pm 09:18 PM

In 2023, AI technology has become a hot topic and has a huge impact on various industries, especially in the programming field. People are increasingly aware of the importance of AI technology, and the Spring community is no exception. With the continuous advancement of GenAI (General Artificial Intelligence) technology, it has become crucial and urgent to simplify the creation of applications with AI functions. Against this background, "SpringAI" emerged, aiming to simplify the process of developing AI functional applications, making it simple and intuitive and avoiding unnecessary complexity. Through "SpringAI", developers can more easily build applications with AI functions, making them easier to use and operate.

Can I open an XML file using PowerPoint? Can I open an XML file using PowerPoint? Feb 19, 2024 pm 09:06 PM

Can XML files be opened with PPT? XML, Extensible Markup Language (Extensible Markup Language), is a universal markup language that is widely used in data exchange and data storage. Compared with HTML, XML is more flexible and can define its own tags and data structures, making the storage and exchange of data more convenient and unified. PPT, or PowerPoint, is a software developed by Microsoft for creating presentations. It provides a comprehensive way of

Use Spring Boot and Spring AI to build generative artificial intelligence applications Use Spring Boot and Spring AI to build generative artificial intelligence applications Apr 28, 2024 am 11:46 AM

As an industry leader, Spring+AI provides leading solutions for various industries through its powerful, flexible API and advanced functions. In this topic, we will delve into the application examples of Spring+AI in various fields. Each case will show how Spring+AI meets specific needs, achieves goals, and extends these LESSONSLEARNED to a wider range of applications. I hope this topic can inspire you to understand and utilize the infinite possibilities of Spring+AI more deeply. The Spring framework has a history of more than 20 years in the field of software development, and it has been 10 years since the Spring Boot 1.0 version was released. Now, no one can dispute that Spring

Where is the win10 user profile? How to set the user profile in Win10 Where is the win10 user profile? How to set the user profile in Win10 Jun 25, 2024 pm 05:55 PM

Recently, many Win10 system users want to change the user profile, but they don’t know how to do it. This article will show you how to set the user profile in Win10 system! How to set up user profile in Win10 1. First, press the "Win+I" keys to open the settings interface, and click to enter the "System" settings. 2. Then, in the opened interface, click "About" on the left, then find and click "Advanced System Settings". 3. Then, in the pop-up window, switch to the "" option bar and click "User Configuration" below.

How to set transaction isolation level in Spring How to set transaction isolation level in Spring Jan 26, 2024 pm 05:38 PM

How to set the transaction isolation level in Spring: 1. Use the @Transactional annotation; 2. Set it in the Spring configuration file; 3. Use PlatformTransactionManager; 4. Set it in the Java configuration class. Detailed introduction: 1. Use the @Transactional annotation, add the @Transactional annotation to the class or method that requires transaction management, and set the isolation level in the attribute; 2. In the Spring configuration file, etc.

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

How to use PHP functions to process XML data? How to use PHP functions to process XML data? May 05, 2024 am 09:15 AM

Use PHPXML functions to process XML data: Parse XML data: simplexml_load_file() and simplexml_load_string() load XML files or strings. Access XML data: Use the properties and methods of the SimpleXML object to obtain element names, attribute values, and subelements. Modify XML data: add new elements and attributes using the addChild() and addAttribute() methods. Serialized XML data: The asXML() method converts a SimpleXML object into an XML string. Practical example: parse product feed XML, extract product information, transform and store it into a database.

Install Helm on Ubuntu Install Helm on Ubuntu Mar 20, 2024 pm 06:41 PM

Helm is an important component of Kubernetes that simplifies the deployment of Kubernetes applications by bundling configuration files into a package called HelmChart. This approach makes updating a single configuration file more convenient than modifying multiple files. With Helm, users can easily deploy Kubernetes applications, simplifying the entire deployment process and improving efficiency. In this guide, I'll cover different ways to implement Helm on Ubuntu. Please note: The commands in the following guide apply to Ubuntu 22.04 as well as all Ubuntu versions and Debian-based distributions. These commands are tested and should work correctly on your system. in U

See all articles