Spring hello world example in eclipse.

Let us create spring hello world example by following below steps:
1. Download spring jar files and add into project class path.
2. Create java class (bean).
3. Create spring configuration file which contains all bean configurations.
4. Create spring test class.
5. Load spring configuration file into Resource Interface object.
Syntax: Resource resource = new ClassPathResource(“spring configuration file”);
6. Create BeanFactory (Spring IOC container) object by reading spring configuration file through resource object.
Syntax: BeanFactory beanFactory = new XmlBeanFactory(resource);
7. Get bean object from beanFactory (spring ioc container) by passing beanId.
TestBean testBean = beanFactory.getBean(“testBeanId″);
Note:
1. We can also use ApplicationContext object for getting bean object as:
Synyax:
ApplicationContext context =
new ClassPathXmlApplicationContext("spring configuration file");
TestBean testBean = (TestBean) context.getBean("testBeanId");
2. Spring IOC container is responsible for creating bean objects and dependency injection.
3. Spring uses singleton design pattern for beans, so every spring bean is singleton class by default.

Example explanation:

First create a java bean class named “HelloWorld.java” which have a propoerty userName and a method sayHello. Next create a spring configuration file which defines the bean and its properties. Here we define HelloWorld.java bean with id helloWorld. This id will be used later to get the bean object from ApplicationContext object. Next create the Test.java class as a main program. Framework creates the ApplicationContext object by using spring configuration file. Then get the HelloWorld bean object from ApplicationContext by using getBean() method. We have to pass beanId in the getBean() method. At the time of object creation IoC container sets the bean propoerties value defined in spring configuration file i.e. userName property value will be set to jai.

Example:

HelloWorld.java
package com.javawithease.business;
 
/**
* This class will be used as a bean class.
* @author javawithease
*/

public class HelloWorld {
private String userName;
 
public void setUserName(String userName) {
this.userName = userName;
}
 
public void sayHello(){
System.out.println("Hello: " + userName);
}
}
applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
>
 
<bean id="helloWorld" class="com.javawithease.business.HelloWorld">
<property name="userName" value="jai"/>
</bean>
 
</beans>
Test.java
package com.javawithease.business;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class Test {
public static void main(String[] args) {
//Get ApplicationContext using spring configuration file.
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
 
//Get HelloWorld bean object from ApplicationContext instance.
HelloWorld helloWorld = (HelloWorld) context.getBean("helloWorld");
 
//Call sayHello method of HelloWorld bean.
helloWorld.sayHello();
}
}

Output:

Hello: jai
 

No comments: