Wednesday, 20 April 2016

Can we use multiple Spring configuration XML files ?


Using XML config

import tag elements are used in main Spring configuration XML ( usually, applicationcontext.xml ) for assembling modularized bean definitions (XML files) : 
<beans>
  <import resource="order.xml" />
  <import resource="billing.xml" />
  <import resource="shipping.xml" />

  <bean id= "orderService" class="com.lizjason.spring.OrderService"/>
<beans>


Using ApplicationContext

Instead of pre-assembling them in the XML configurations using imports, it is more flexible to configure and easy to manage them through the ApplicationContext.
You can pass an array of bean definitions to the ApplicationContext constructor when getting its reference.

Like :
String[] serviceResources = {"order.xml", "billing.xml", "shipping.xml"};
ApplicationContext orderServiceContext =
        new ClassPathXmlApplicationContext(serviceResources)

No comments:

Post a Comment

Note: only a member of this blog may post a comment.