Home Tutorial Spring Spring3 Web ClassPathXmlApplicationContext and FileSystemXmlApplicationContext

 
 

ClassPathXmlApplicationContext and FileSystemXmlApplicationContext
Posted on: April 2, 2011 at 12:00 AM
Advertisement
In this section we will discuss about the ClassPathXmlApplicationContext and FileSystemXmlApplicationContext

ClassPathXmlApplicationContext and FileSystemXmlApplicationContext

Spring provides various ApplicationContext implementations to load spring configuration file. Here is some most commonly used classes.

1. ClassPathXmlApplicationContext : It loads context information from an XML file which is present in classpath.

The sample code for loading context information from classpath:

ApplicationContext classpathCtx = new ClassPathXmlApplicationContext("helloWorld.xml");
HelloWorld helloBean = (HelloWorld) classpathCtx.getBean("helloWorld");

2. FileSystemXmlApplicationContext : It loads context information from an XML file which is present in the file system.

FileSystemXmlApplicationContext fileCtx = new FileSystemXmlApplicationContext("C:\\helloWorldFile.xml");
HelloWorld helloBean = (HelloWorld) fileCtx.getBean("helloWorld");

An example:

Application directory structure: The application has directory structure like picture beloew where lib folder contains all jar files required for spring and net/roseindia folder contains our application class files HellowWorld and HelloWorldMain.

HellowWorld.java

package net.roseindia;

public class HelloWorld {
public void sayHello(){
System.out.println("Hi........");
}
}

 

helloWorld.xml:

Declare the above class as bean in spring context file as below:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="helloWorld" class="net.roseindia.HelloWorld" />

</beans>

 

HelloWorldMain.java:

This class demonstrates loading the application context file which is present in classpath ( C:\Spring3\helloWorld.xml) and loading if you have the context file in some place in file system (C:\helloWorldFile.xml).

package net.roseindia;

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

public class HelloWorldMain {

public static void main(String[] args) {

HelloWorld helloBean = null;

ApplicationContext classpathCtx = new ClassPathXmlApplicationContext("helloWorld.xml");
helloBean = (HelloWorld) classpathCtx.getBean("helloWorld");
helloBean.sayHello();

FileSystemXmlApplicationContext fileCtx = new FileSystemXmlApplicationContext("C:\\helloWorldFile.xml");
helloBean = (HelloWorld) fileCtx.getBean("helloWorld");
helloBean.sayHello();
}
}

 

Output:

Set the classpath, compile the java files and run the HelloWorldMain.class file. You will see the output as below:


C:\Spring3>set classpath=%classpath%;C:\Spring3\lib\*;

C:\Spring3>javac C:\Spring3\net\roseindia\*.java

C:\Spring3>java net.roseindia.HelloWorldMain
Apr 1, 2011 5:53:59 PM org.springframework.context.support.AbstractApplicationCo
ntext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationCont
ext@4413ee: startup date [Fri Apr 01 17:53:59 GMT+05:30 2011]; root of context h
ierarchy
Apr 1, 2011 5:53:59 PM org.springframework.beans.factory.xml.XmlBeanDefinitionRe
ader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [helloWorld.xml]
Apr 1, 2011 5:53:59 PM org.springframework.beans.factory.support.DefaultListable
BeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.
DefaultListableBeanFactory@13d28e3: defining beans [helloWorld]; root of factory
hierarchy
Hi........
Apr 1, 2011 5:53:59 PM org.springframework.context.support.AbstractApplicationCo
ntext prepareRefresh
INFO: Refreshing org.springframework.context.support.FileSystemXmlApplicationCon
text@32784a: startup date [Fri Apr 01 17:53:59 GMT+05:30 2011]; root of context
hierarchy
Apr 1, 2011 5:53:59 PM org.springframework.beans.factory.xml.XmlBeanDefinitionRe
ader loadBeanDefinitions
INFO: Loading XML bean definitions from file [C:\helloWorldFile.xml]
Apr 1, 2011 5:54:00 PM org.springframework.beans.factory.support.DefaultListable
BeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.
DefaultListableBeanFactory@feb48: defining beans [helloWorld]; root of factory h
ierarchy
Hi........

C:\Spring3>

Advertisement

Related Tags for ClassPathXmlApplicationContext and FileSystemXmlApplicationContext:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 2, 2011

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com