How to deal with multiple spring configuration files?

Posted: December 9, 2011 in Java, Spring
Tags: , , ,

We often use a separate spring config file for each layer in our project – say springConfig-delegate.xml for the delegate layer, springConfig-service.xml for the service layer and springConfig-dao.xml for the dao layer.

Normally, people load them like

ApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {“springConfig-delegate.xml”,”springConfig-service.xml”,”springConfig-dao.xml”});

This is not a good practice when we have the provision to organize all our spring configuration files into a single XML file & load it.

Create a single spring config file names “springConfig.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="common/Spring-Common.xml"/>
        <import resource="connection/Spring-Connection.xml"/>
        <import resource="moduleA/Spring-ModuleA.xml"/>

</beans>

and then load it like

ApplicationContext context =
new ClassPathXmlApplicationContext(springConfig.xml);

Advertisements
Comments
  1. mahehs says:

    yes right but you mistake small one aboue mentioned diffrent xml files …………………..
    but in mentioned in xml files we wrote different xml files………….. change it once..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s