1. Create the project directory structure.
2. Create the WelcomeBean class with the name property.
WelcomeBean.java
[java]package com.splessons;
public class WelcomeBean
{
private String name;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public void display()
{
System.out.println(name);
}
}
[/java]
3. Create the Spring xml file with name applicationContext.xml.
applicationContext.xml
[java]<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="welcomeBeanId" class="com.splessons.WelcomeBean">
<property name="name" value="Welcome to splessons"/>
</bean>
</beans> [/java]
4. Create the BeanFactory class and reads the xml file using Resource object and BeanFactory object.
BeanFactoryClass.java
[java]package com.splessons;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class BeanFactoryClass
{
public static void Main(String[] args)
{
// TODO Auto-generated method stub
Resource resource=new ClassPathResource("applicationcontext.xml");
BeanFactory factory = new XmlBeanFactory(resource);
WelcomeBean bean=(WelcomeBean)factory.getBean("welcomeBeanId");
bean.display();
}
}
[/java]