How to load multiple spring bean configuration files?
When you are developing any spring based complex application, the size of the xml based configuration file grows unmanageable.
It is a standard practice to split the configuration file into multiple small configuration files based on functionality.
This page helps you to understand how to handle multiple spring bean configurations files, we can achieve this by using
<import> tag. Lets take a simple example, you have seperated your configurations into two files, one is to manage jdbc configurations,
and another one is to manage all beans. Below configuration file called database-config.xml handles all jdbc configurations:
<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-3.0.xsd">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/java2novice</value>
</property>
<property name="username"><value>root</value></property>
<property name="password"><value>password</value></property>
</bean>
</beans>
|
Here is another configuration file which handles all beans with in the application, it is called bean-config.xml file:
<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-3.0.xsd">
<bean id="appDao" class="com.java2novice.beans.MyAppDao" />
<bean id="empEntity" class="com.java2novice.beans.Employee" />
<bean id="companyBean" class="com.java2novice.beans.Company" />
</beans>
|
Now below configuration file imports the above two configuration files, it is called applicationContext.xml file. We need to
load this file in our application to get all bean objects.
<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-3.0.xsd">
<import resource="bean-config.xml" />
<import resource="database-config.xml" />
</beans>
|
|