Example of Spring Applicationcontext by R4R Team

1. Create a POJO class (Emp.java)

 package com.r4r;

public class Emp {

private String address;

private String name;

public String getName()


return name;


public void setName(String name)


this.name = name;


public String getAddress()


return address;


public void setAddress(String address)


this.address = address;

} }

2. Create Applicationcontext configuration file (Beans.xml)

<beans xmlns="http://www.springframework.org/schema/beans"




<bean id="emp" class="com.r4r.Emp">

<property name="name" value="Hello Applicationcontext Program.."/>



3. Create a Main program (EmpMain.java)

package com.r4r;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.FileSystemXmlApplicationContext;

public class EmpMain {

public static void main(String[] args) {

ApplicationContext context = new FileSystemXmlApplicationContext("C:/Users/workspace/Test/src/Beans.xml");

Emp obj = (Emp) appcontext.getBean("emp");


} }

4. Output: Hello Applicationcontext Program..

There are following two important points to note about the main program:
1 First step is to create factory object where we used framework API FileSystemXmlApplicationContext to create the factory bean after loading the bean configuration file from the given path. The FileSystemXmlApplicationContext() API takes care of creating and initializing all the objects ie. beans mentioned in the XML bean configuration file.

2 Second step is used to get required bean using getBean() method of the created context. This method uses bean ID to return a generic object which finally can be casted to actual object. Once you have object, you can use this object to call any class method.

Leave a Comment:
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!