Home  >  Spring

Spring FileSystemXmlApplicationContext Example

By Arvind Rai, July 04, 2022
On this page we will learn using FileSystemXmlApplicationContext class.
1. The FileSystemXmlApplicationContext is a standalone XML application context. It takes context definition files from the file system or from URLs.
2. It interprets plain paths as relative file system locations. Suppose we have a plain path ("mydir/myfile.txt"). This path will be interpreted as relative to the current VM working directory, even if they start with a slash.
3. To enforce an absolute file path, use an explicit "file:" prefix as ("file:C:/mydir/myfile.txt").

Example

SpringDemo.java
package com.concretepage;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class SpringDemo {
    public static void main(String... args) {
   	ApplicationContext context = new FileSystemXmlApplicationContext("spring-app.xml");
    	Entitlement ent=(Entitlement) context.getBean("entitlement");
        System.out.println(ent.getName());
    } 
} 
spring-app.xml
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
    <bean id="entitlement" class="com.concretepage.Entitlement">
	    <constructor-arg name="name" value="Testing..."/>
	    <constructor-arg name="time" value="20"/>
    </bean>
</beans> 
Entitlement.java
package com.concretepage;
public class Entitlement {
	private String name;
	private int time;
	public Entitlement(String name,int time){
		this.name=name;
		this.time=time;
	} 
	public String getName() {
		return name;
	}
	public int getTime() {
		return time;
	}
} 
Output
Testing... 
POSTED BY
ARVIND RAI
ARVIND RAI











©2022 concretepage.com | Privacy Policy | Contact Us