1. Create the project directory structure.
2. Add the required spring jar files through Add external jar files.
3. create the java class under the package folder.
BeanClass.java
[java]
package com.splessons;
public class BeanClass
{
private String userName;
public void setUserName(String userName)
{
this.userName = userName;
}
public String getUserName()
{
System.out.println("Hello, " + userName);
return userName;
}
}
[/java]
MainApplication.java
[java]package com.splessons;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApplication {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationcontext.xml");
BeanClass beanClass1 = (BeanClass) context.getBean("BeanScope");
beanClass1.setUserName(" Welcome to SPLessons ");
beanClass1.getUserName();
BeanClass beanClass2 = (BeanClass) context.getBean("BeanScope");
beanClass2.getUserName();
}
}
[/java]
4. Create the bean configuration file under source (src) folder.
applicationcontext.xml
[xml]<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="BeanScope" class="com.splessons.BeanClass"
scope="singleton">
</bean>
</beans>[/xml]
5. Run the application and display the output like