How To Load Multiple Spring Bean Configuration File

Loading Multiple Spring Bean Configuration File

 Put all spring xml files under project classpath. 

Define one main module beans configuration file. Say AppContext.xml

<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="classpath:AppServicesContext.xml"/>
        <import resource="classpath:AppConnectionContext.xml"/>
        <import resource="classpath:AppModuleA.xml"/>
        <import resource="classpath:AppModuleB.xml"/>
</beans>

Now you can load a single xml file like this :

ApplicationContext context = 
    		new ClassPathXmlApplicationContext("AppContext.xml");

 

Advertisements

Using Property File in Spring

Often times, Spring developers keep there configuration value in XML file itself. Say for example DB setting as

<bean id=”dataSource”
class=”org.springframework.jdbc.datasource.DriverManagerDataSource”>

<property name=”driverClassName” value=”com.mysql.jdbc.Driver” />
<property name=”url” value=”jdbc:mysql://localhost:3306/rr” />
<property name=”username” value=”root” />
<property name=”password” value=”password” />
</bean>

But, in a corporate environment, deployment detail is usually only can ‘touch’ by your system or database administrator, they just refuse to access your bean configuration file directly, and they will request a separate file for deployment configuration, for example, a simple properties, with deployment detail only.  To fix it, one can use spring API called PropertyPlaceHolderConfigurer.

Example of PropertyPlaceHolderConfigurer

Create a properties file (database.properties), include your details (say database details), put it into your project class path.

database.properties

        jdbc.driverClassName=com.mysql.jdbc.Driver
	jdbc.url=jdbc:mysql://localhost:3306/rr
	jdbc.username=root
	jdbc.password=password

Declare a PropertyPlaceholderConfigurer  bean in bean configuration file and map to the ‘database.properties‘ properties file you created just now.

<bean 
	class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
	<property name="location">
	  <value>database.properties</value>
	</property>
</bean>

At End our beans configuration file look like as

<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
	 class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
	  <property name="location">
		<value>database.properties</value>
	  </property>
	</bean>
	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="${jdbc.driverClassName}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>

</beans>

Hope this helped you…… 🙂