3.Spring IoC : HelloWorld!!

For spring application we need following resources as programs
·         POJO Class                 à a Simple Bean
·         Business Logic Class à Logic….
·         Main Client Class       à contains main method with loading the spring container
·         Spring.xml                 à all Bean Configurations

package bo;

public class HelloBean {

       private String message;

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

       public String getMessage() {
              return message;

       public void Hello() {

----------------- HelloMain .java---------
package main;

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;

import bo.HelloBean;

public class HelloMain {
       public static void main(String[] args) {
              Resource resource = new ClassPathResource("res/helloSpring.xml");
              BeanFactory factory = new XmlBeanFactory(resource);
              HelloBean bean = (HelloBean) factory.getBean("ob");

----------------- helloSpring.xml---------
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
       <bean id="ob" class="bo.HelloBean">
              <property name="message" value="Hello Spring!!" />


Step –by –step Execution Flow
1.    In main method Resource class loads the helloSpring.xml file.
Resource resource = new ClassPathResource("res/helloSpring.xml");
2.    It just check the Syntax and creates a Resource object
3.    BeanFactory instantiate by loading resource object
BeanFactory factory = new XmlBeanFactory(resource);
4.    While loading Spring Container will inject the all dependencies by loading xml file
5.    Container will inject the all the dependencies and it will create the Bean Class Objects
6.    All the Bean class objects will stored in BeanFactory Object
7.    We can get the BeanFactory object by calling getBean(“id”),
HelloBean bean = (HelloBean) factory.getBean("ob");
8.    it always return object of java.lang.Object.so we have to type cast

9.    and call appropriate methods