How to load multiple Spring bean configuration file
Problem
In a large project structure, the Spring’s bean configuration files are located in different folders for easy maintainability and modular. For example,Spring-Common.xml
in common
folder,Spring-Connection.xml
in connection
folder,Spring-ModuleA.xml
in ModuleA
folder…and etc.
You may load multiple Spring bean configuration files in the code :
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"Spring-Common.xml", "Spring-Connection.xml","Spring-ModuleA.xml"});
Put all spring xml files under project classpath.
project-classpath/Spring-Common.xml
project-classpath/Spring-Connection.xml
project-classpath/Spring-ModuleA.xml
Solution
The above ways are lack of organizing and error prone, the better way should be organized all your Spring bean configuration files into a single XML file. For example, create a Spring-All-Module.xml
file, and import the entire Spring bean files like this :
File : Spring-All-Module.xml
<?xml version="1.0"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<import resource="common/Spring-Common.xml" />
<import resource="connection/Spring-Connection.xml" />
<import resource="moduleA/Spring-ModuleA.xml" />
</beans>
Now you can load a single xml file like this :
ApplicationContext context = new ClassPathXmlApplicationContext(Spring-All-Module.xml);
Put this file under project classpath.
project-classpath/Spring-All-Module.xml
Note In Spring3, the alternative solution is using JavaConfig
@Import
.
相關文章
- How to inject value into bean properties in SpringBeanSpring
- How to Cim load multiple entries into 7.6.5.4 SO Container Maintenance or 16.11AINaN
- 【Spring 註解】@Configuration和@BeanSpringBean
- How to modify multiple items in the SAP menu
- ssis multiple table to one file
- configuration error-could not load file or assembly crystaldecisions.reportappserver.clientdocErrorAPPServerclient
- EF:split your EDMX file into multiple diagrams
- How to build a jar file by mavenUIJARMaven
- input file multiple 批量上傳檔案
- Weblogic configuration fileWeb
- How to Convert Class File to Java File Online?Java
- [Spring]BeanSpringBean
- ORA-27046: file size is not a multiple of logical block sizeBloC
- How to use Qt Designed Ui fileQTUI
- How to dynamically evaluate a tag within a java fileJava
- HOW TO PRINT FILE TO NOTEPAD IN GUI?GUI
- Unreal: Dynamic load map from Pak fileUnreal
- How to link multiple docker-compose services via networkDocker
- Spring中Bean及@Bean的理解SpringBean
- How to Setup SCAN Listener and Client for TAF and Load Balancingclient
- Spring Bean容器SpringBean
- 【Spring】Bean管理SpringBean
- Oracle ASM How many allocation units per fileOracleASM
- 高手sos!!! Unable to load document from file...
- Configuration of Load Balancing and Transparent Application FailoverAPPAI
- 【Spring註解開發】元件註冊-使用@Configuration和@Bean給容器中註冊元件Spring元件Bean
- Spring Bean 綜述SpringBean
- Spring bean 裝配SpringBean
- spring boot factory beanSpring BootBean
- Spring基礎(Bean)SpringBean
- Spring Bean作用域SpringBean
- Spring Bean 詳解SpringBean
- 淺談Spring BeanSpringBean
- Spring Bean裝配SpringBean
- Spring中的BeanSpringBean
- Spring – 裝配BeanSpringBean
- system image file too large for device hardware configurationdev
- How To Retry Multiple Errored Approval Workflow Processes After A Fix Or Patch Has Been ImplementedErrorAPP