Talk about how to integrate multiple configuration files in Spring

For a large application, there may be multiple configuration files. We can specify these configuration files through an array of Strings when starting the Spring container.

Spring can also integrate these configuration files <import>by introducing multiple configuration files into one file, so that when starting the Spring container, only this main configuration file needs to be specified. For example, the following main configuration file:

<import resource="classpath:beans2.xml"/>
<bean id="author" class="net.deniro.spring4.bean.Author"/>

Assuming that the Book class has been configured in beans2.xml, then the Spring container can load the information of these two beans through this main configuration file O(∩_∩)O haha~

If a configuration file a.xml references a bean in b.xml, it is not necessary to import b.xml through import, just make sure that both a.xml and b.xml are in the configuration file list when the Spring container is started. Can.

The difference between these two configuration methods is that if b.xml is introduced in a.xml by import, it is equivalent to a.xml file that contains all the information of these two configuration files, so the Spring container only needs to load a. xml file; otherwise, two configuration files need to be loaded at the same time when Spring starts, so that the two configuration files can be merged in memory.

We <import>can , and the resource attribute supports Spring's standard resource paths.


In order to prevent the contention of configuration file resources during development, or to facilitate the splitting of modules, large-scale applications often have a directly independent configuration file for each module. We can provide an integratable configuration file at the application level by <import>integrating various modules. In this way, when starting the container, you only need to load the integrated configuration file. O(∩_∩)O haha~

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325811941&siteId=291194637