Home » Spring » Spring Hello World Example by Injecting primitive values using setter method

Spring Hello World Example by Injecting primitive values using setter method

This tutorial will help you to write your first Hello World Spring program by injecting primitive values using setter method.

Files required..

  • HelloBean.java
  • applicationContext.xml
  • Test.java


Tools and Technologies used in this article :

  • Spring
  • Java
  • Eclipse

1. Create a java project in Eclipse

Open Eclipse –> File –> New –> and Select Java project and click on next


Spring Hello World Setter Injection


2. Add jar files to classpath of Project

Right click on the project and create a folder and name it to lib. After creation of lib folder copy spring-2.5.6.jar and commons-logging-1.1.1.jar files into this folder and add these jars to class path of the project.commons-logging and spring jar are required for Spring based application.

Complete folder structure of the project is given below.

Spring Hello World Setter Injection

Assign the name of the project as "HelloWorldSpring"

3. Create Java class

Create a java class HelloBean.java and add following code into this class.

HelloBean.java

package com.javawebtutor.spring;

public class HelloBean {
	private String message;

	public void setMessage(String message) {
		this.message = message;
	}

	public void displayMessage() {
		System.out.println(message);
	}
}

In HelloBean.java, we have written setter method for the property message (primitive), spring container will inject value in this property at run time.

4. Create Spring bean configuration file

Create an xml file (applicationContext.xml) in “src “. This is the Spring’s bean configuration file, which declares all the available Spring beans.

We are providing the information into the bean by this file. The property element invokes the setter method. The value sub element of property will assign the specified value.

applicationContext.xml

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="id1" class="com.javawebtutor.spring.HelloBean">
<property name="message" value="Welcome to the world of Spring" />
</bean>
</beans>

The id attribute of the bean element is used to specify the bean name and the class attribute is used to specify the fully qualified class name of the bean. The property element with in the bean element is used to inject property value via setter injection. The name attribute of the property element represents the bean attribute and the value attribute specifies the corresponding property value.

5. Create Test Class

This class gets the bean from the applicationContext.xml file and calls the displayMessage() method.

Test.java

package com.javawebtutor.spring;

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 Test {
    public static void main(String[] args)
    {
        Resource res = new ClassPathResource("applicationContext.xml");
        BeanFactory factory = new XmlBeanFactory(res);
        Object obj = factory.getBean("id1");
        HelloBean helloBean = (HelloBean)obj;
        helloBean.displayMessage();
    }
}

Explanation :

Review it and make sure the folder structure as follows.

In Test.java first we need to load the configuration file, so we done this at line number 12, so resource, contains all the information about the configuration xml.And give this resource object to BeanFactory [ Spring container ] with XmlBeanFactory, so now factory knows all the beans in the xml file so we can now call any bean with bean id.

In Test.java, if we call getBean(“id1″) then internally the spring framework executes the following statements

WelcomeBean wb = new WelcomeBean();
wb.setMessage(“Welcome to spring”);

Return type of getBean() is always object.

In applicationConext.xml,we have written the property element , here <property /> means we are saying to the spring container that we have written setter method in our bean class HelloBean.java.

6. Run It

Right click on the Test.java and select Run as Java Application, you can see following output in console.

Sep 6, 2014 12:45:25 AM 
Welcome to the world of spring

You can download the source code of the example by clicking on the Download link below.

Download


Previous Next Article
comments powered by Disqus