How To Load Multiple Spring Bean Configuration Files

 Posted On  | Yashwant Chavan 

For the larger size project, its good approach to keep all spring bean definition in separate file. It will helps to manage the bean configuration in proper way. Its file to use single file when your application size is small. Let see how to load the multiple file using spring application context API

Database Configuration Bean Definition

Create new "springtutorial-database.xml" file, which is used to configure database details.

<?xml version="1.0" encoding="UTF-8"?>
<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">
 
	<!--  This is Database Configuration -->
	<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/technicalkeeda</value></property>
		<property name="username"><value>root</value></property>
		<property name="password"><value>password</value></property>
	</bean>
		
</beans>

Create another file "springtutorial-beans.xml" which is used to define the bean definitions.

<?xml version="1.0" encoding="UTF-8"?>
<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"> 
	<bean id="personDao" class="com.net.technicalkeeda.dao.PersonDaoImpl">	
		<property name="dataSource" ref="dataSource" />
	</bean>		
</beans>

Create one more file "springtutorial-context.xml" which import above two files.

<?xml version="1.0" encoding="UTF-8"?>
<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="springtutorial-database.xml" />
	<import resource="springtutorial-beans.xml" />	
</beans>

Load springtutorial-context.xml file


package com.net.technicalkeeda.controller;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.net.technicalkeeda.bean.Person;
import com.net.technicalkeeda.interfaces.PersonDao;

public class Test {
public static void main(String[] args) {
	ApplicationContext context = new ClassPathXmlApplicationContext(
				"springtutorial-context.xml");
		
}
}

There is another approach , rather than importing "springtutorial-beans.xml" and "springtutorial-database.xml" into "springtutorial-context.xml", We can directly load both the configuration files. Just pass the String array which contains the file name. But there is important condition is files should be available under class path.


ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"springtutorial-beans.xml",
"springtutorial-database.xml"});	


Promote your Blog, Website, or services on technicalkeeda.com, My paypal id: yashwantchavan@gmail.com

Payment will be accepted through paypal only.


© technicalkeeda.com 2016

 |  Find us on Google+ |  Rss Feed

Loaded in 0.0290 seconds.