How to load spring application context xml from classpath in java 2013

View previous topic View next topic Go down

How to load spring application context xml from classpath in java 2013

Post by Admin on Sun Sep 15, 2013 4:04 am

2013 2013 2013 <div dir="ltr" style="text-align: left;" trbidi="on">This article will explain you how to load a spring configuration/application context xml file in standalone java application.<br>ApplicationContext is an interface that is being implemented in Spring to load a spring configuration in a java class. The ClassPathXmlApplicationContext class implements this interface which takes a path to the spring configuration application context xml file located in a classpath.<br>Below is the sample implementation to load a xml file and load a bean from that xml file in java.<br><br><iframe src="http://www.flipkart.com/affiliate/displayWidget?affrid=WRID-137657647050915602&affid=kumarbhati" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" ></iframe><iframe src="http://www.flipkart.com/affiliate/displayWidget?affrid=WRID-137657670058847555&affid=kumarbhati" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" ></iframe><iframe src="http://www.flipkart.com/affiliate/displayWidget?affrid=WRID-137657678987077078&affid=kumarbhati" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" ></iframe><iframe src="http://www.flipkart.com/affiliate/displayWidget?affrid=WRID-137657684457385939&affid=kumarbhati" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" ></iframe> </br><pre style="font-family:arial;font-size:12px;border:1px dashed #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;padding:0px;color:#000000;text-align:left;line-height:20px;word-wrap:normal;">import org.springframework.context.ApplicationContext; <br /> import org.springframework.context.support.ClassPathXmlApplicationContext; <br /> public class SpringLoadAppContext { <br /> private static final String[] SPRING_CONFIG_FILES = new String[] { "CLASSPATH_APP_CONTEXT_XML_FILE_1", <br /> "CLASSPATH_APP_CONTEXT_XML_FILE_2", "CLASSPATH_APP_CONTEXT_XML_FILE_3" }; <br /> private static final String CLASSPATH_APP_CONTEXT_XML_FILE_1 = "classpath:PATH_TO_XML_FILE"; <br /> public static void main(String args[]) { <br /> // Load SINGLE application context xml file <br /> ApplicationContext applicationContext = new ClassPathXmlApplicationContext("CLASSPATH_APP_CONTEXT_XML_FILE"); <br /> // Load a bean from application context <br /> Object object = applicationContext.getBean("BEAN_ID"); <br /> // Load MULTIPLE application context xml file <br /> ApplicationContext multipleAppContext = new ClassPathXmlApplicationContext(SPRING_CONFIG_FILES); <br /> } <br /> } <br /></pre> As you can see in the above implementation, we can load multiple spring configuration files at once.<br><br>Once the configuration file is loaded, a bean can be loaded using the <b>applicationContext.getBean("BEAN_ID");</b> where BEAN_ID is the bean id that is configured in the xml file.<br><br> <b>Eager loading of bean in spring java</b><br>By default, spring instantiates all the beans once the file gets loaded using the ClassPathXmlApplicationContext. This is known as eager loading of spring beans.<br>If you want to do a lazy loading of a bean, where the bean will be instantiated only when first client request is received for that bean i.e. applicationContext.getBean() gets called for particular bean then do some modification in the configuration file as shown below<br><br><bean id="lazyLoadingOfBean" class="CLASS_NAME" lazy-init="true"/> <br></div><br> 2013 2013 2013 <br><div dir="ltr" style="text-align: left;" trbidi="on">This article will explain you how to load a spring configuration/application context xml file in standalone java application.<br>ApplicationContext is an interface that is being implemented in Spring to load a spring configuration in a java class. The ClassPathXmlApplicationContext class implements this interface which takes a path to the spring configuration application context xml file located in a classpath.<br>Below is the sample implementation to load a xml file and load a bean from that xml file in java.<br><br><iframe src="http://www.flipkart.com/affiliate/displayWidget?affrid=WRID-137657647050915602&affid=kumarbhati" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" ></iframe><iframe src="http://www.flipkart.com/affiliate/displayWidget?affrid=WRID-137657670058847555&affid=kumarbhati" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" ></iframe><iframe src="http://www.flipkart.com/affiliate/displayWidget?affrid=WRID-137657678987077078&affid=kumarbhati" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" ></iframe><iframe src="http://www.flipkart.com/affiliate/displayWidget?affrid=WRID-137657684457385939&affid=kumarbhati" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" ></iframe> </br><pre style="font-family:arial;font-size:12px;border:1px dashed #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;padding:0px;color:#000000;text-align:left;line-height:20px;word-wrap:normal;">import org.springframework.context.ApplicationContext; <br /> import org.springframework.context.support.ClassPathXmlApplicationContext; <br /> public class SpringLoadAppContext { <br /> private static final String[] SPRING_CONFIG_FILES = new String[] { "CLASSPATH_APP_CONTEXT_XML_FILE_1", <br /> "CLASSPATH_APP_CONTEXT_XML_FILE_2", "CLASSPATH_APP_CONTEXT_XML_FILE_3" }; <br /> private static final String CLASSPATH_APP_CONTEXT_XML_FILE_1 = "classpath:PATH_TO_XML_FILE"; <br /> public static void main(String args[]) { <br /> // Load SINGLE application context xml file <br /> ApplicationContext applicationContext = new ClassPathXmlApplicationContext("CLASSPATH_APP_CONTEXT_XML_FILE"); <br /> // Load a bean from application context <br /> Object object = applicationContext.getBean("BEAN_ID"); <br /> // Load MULTIPLE application context xml file <br /> ApplicationContext multipleAppContext = new ClassPathXmlApplicationContext(SPRING_CONFIG_FILES); <br /> } <br /> } <br /></pre> As you can see in the above implementation, we can load multiple spring configuration files at once.<br><br>Once the configuration file is loaded, a bean can be loaded using the <b>applicationContext.getBean("BEAN_ID");</b> where BEAN_ID is the bean id that is configured in the xml file.<br><br> <b>Eager loading of bean in spring java</b><br>By default, spring instantiates all the beans once the file gets loaded using the ClassPathXmlApplicationContext. This is known as eager loading of spring beans.<br>If you want to do a lazy loading of a bean, where the bean will be instantiated only when first client request is received for that bean i.e. applicationContext.getBean() gets called for particular bean then do some modification in the configuration file as shown below<br><br><bean id="lazyLoadingOfBean" class="CLASS_NAME" lazy-init="true"/> <br></div><br>2013 2013 2013 <br> <a href="http://www.matrixar.com/" title="Matrix ">المصفوفة : أجمل الخلفيات والصور</a>

Admin
Admin

Posts : 64122
Join date : 2013-02-22

View user profile http://prowebsites1.forumaroc.net

Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum