Home  >  Spring Core

Example of ClassPathXmlApplicationContext in Spring

By Arvind Rai, April 06, 2013
ClassPathXmlApplicationContext belongs to the package org.springframework.context.support. In case we want to use spring stand alone, ClassPathXmlApplicationContext helps to fetch beans defined in xml. ClassPathXmlApplicationContext loads xml files from the classpath. In the example below we are loading two xml files.

SpringDemo.java
package com.concretepage;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringDemo {
    public static void main(String... args) {
            ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"app-conf.xml", "spring-config.xml"});
            
            Entitlement ent=(Entitlement) context.getBean("entitlement");
            System.out.println(ent.getName());
            
            Subscription sub=(Subscription) context.getBean("subscription");
            System.out.println(sub.getName());
    } 
}
 





spring-config.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">
	    <property name="name" value="Entitlement"/>
	    <property name="time" value="20"/>
    </bean>
</beans>
 


app-conf.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="subscription" class="com.concretepage.Subscription">
	    <property name="name" value="Subscription"/>
	    <property name="type" value="Weekly"/>
    </bean>
</beans>
 


Entitlement.java
package com.concretepage;
public class Entitlement {
	private String name;
	private int time;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getTime() {
		return time;
	}
	public void setTime(int time) {
		this.time = time;
	}

}
 


Subscription.java
package com.concretepage;
public class Subscription {
	private String name;
	private String type;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}	
}
 


Output
Entitlement
Subscription
 
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us