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>
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.