Spring Framework

Spring Projects

Spring Project 1

Spring Configuration with Hello World Example
Previous Home Next

Introduction: The Hello world example in spring framework uses the constructor injection. It means that object is created by the IOC container through the constructor.

Class/Library File Descriptions: First of all we are create bean class name Hello then define the parameterize constructor and define a method name display which are print string sanded by configuration XML file.

Configuration information syntax

<bean id="hello" class="org.r4r.Hello">
<constructor-arg value="Hello World !"/>
</bean>
Technologies used to run this source code
  1. Spring 2.5
  2. Eclipse
  3. JDK 1.6

 

Source Code

applicationContext.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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="hello" class="org.r4r.Hello">
<constructor-arg value="Hello World !"/>
</bean>
</beans>

Hello.java

package org.r4r;
public class Hello {
String hello;
public Hello(String hello) {
super();
this.hello = hello;
}
public void display(){
System.out.println(hello);
}
}

HelloTest.java

package org.r4r;
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 HelloTest {
public static void main(String[] args) {
Resource r=new ClassPathResource("applicationContext.xml");
BeanFactory f=new XmlBeanFactory(r);
Hello h=(Hello)f.getBean("hello");
h.display();
}
}

Output:

Hello World !
Previous Home Next

Tolal:0 Click:

Show All Comments

Did not find what you were looking for leave your name and message. We will revert within 24 hours
Name:
eMail:
Comment / Feedback: