Home  >  Spring Core

FileSystemXmlApplicationContext Spring Example

By Arvind Rai, August 15, 2013
FileSystemXmlApplicationContext belongs to the package org.springframework.context.support. FileSystemXmlApplicationContext is XML ApplicationContext. It works standalone. It can pick the XML file from absolute path by appending keyword “file” and also can fetch from classpath by appending keyword "classpath". Find the example below.

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("classpath: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-3.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.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...
 


pom.xml
<dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>3.1.0.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    
    <dependency>
	  <groupId>aopalliance</groupId>
      <artifactId>aopalliance</artifactId>
      <version>1.0</version>
      <optional>true</optional>
    </dependency>
    
    <dependency>
	  <groupId>cglib</groupId>
      <artifactId>cglib-nodep</artifactId>
	  <version>2.1_3</version>
	  <optional>true</optional>
    </dependency>
    
    <dependency>
	 <groupId>org.hibernate</groupId>
     <artifactId>hibernate-annotations</artifactId>
     <version>3.5.2-Final</version>
     <optional>true</optional>
    </dependency>
    
    <dependency>
     <groupId>org.hibernate</groupId>
     <artifactId>hibernate-core</artifactId>
     <version>3.6.0.Final</version>
     <optional>true</optional>
    </dependency>
    
    <dependency>
	 <groupId>org.springframework</groupId>
     <artifactId>spring-jdbc</artifactId>
     <version>2.0.8</version>
     <scope>compile</scope>
    </dependency>
    
    <dependency>
	  <groupId>org.springframework</groupId>
	  <artifactId>spring-tx</artifactId>
	  <version>3.1.0.RELEASE</version>
	  <scope>compile</scope>
    </dependency>
    
    <dependency>
	  <groupId>commons-dbcp</groupId>
      <artifactId>commons-dbcp-all</artifactId>
      <version>1.3</version>
      <scope>compile</scope>
    </dependency>
    
    <dependency>
	  <groupId>org.springframework</groupId>
	  <artifactId>spring-aop</artifactId>
      <version>3.1.0.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    	
    <dependency>
	  <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>3.1.0.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    
    <dependency>
	 <groupId>org.springframework</groupId>
     <artifactId>spring-core</artifactId>
     <version>3.1.0.RELEASE</version>
     <scope>compile</scope>
    </dependency>
    
    <dependency>
	 <groupId>org.springframework</groupId>
     <artifactId>spring-expression</artifactId>
     <version>3.1.0.RELEASE</version>
     <scope>compile</scope>
    </dependency>
    
    <dependency>
	 <groupId>org.springframework</groupId>
     <artifactId>spring-asm</artifactId>
	  <version>3.1.0.RELEASE</version>
	 <scope>compile</scope>
    </dependency>	
    
    <dependency>
	 <groupId>mysql</groupId>
	 <artifactId>mysql-connector-java</artifactId>
	 <version>5.1.17</version>
    </dependency>
    
    <dependency>
    <groupId>javax.inject</groupId>
    <artifactId>javax.inject</artifactId>
    <version>1</version>
    </dependency>
    
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-agent</artifactId> 
     <version>2.5.6</version> 
     <scope>provided</scope> 
    </dependency> 

   <dependency>
	 <groupId>org.aspectj</groupId>
	 <artifactId>aspectjweaver</artifactId>
	 <version>1.5.4</version>
   </dependency>
  </dependencies>
 
Download Source Code
filesystemxmlapplicationcontext-spring-example.zip
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us