Thursday 13 December 2012

Getting Started with Spring IoC

Posted by Naveen Katiyar On 02:32 No comments

Configuring Spring Bean and creating Spring Bean instance

This quick start will make you go through the implementations of Spring IoC example and illustrate how to configure your Spring Bean in Spring Configuration file and how to get instance of the bean using Spring IoC container. The example will take a example of Cat class (Cat.java) that will implement Speaks interface (Speaks.java) .


Speaks Interface (Speaks.java)

package com.naveen.beans;

public interface Speaks {
    public void talk();
}

Speaks interface has only one method.


Cat Class (Cat.java)

package com.naveen.beans;

public class Cat implements Speaks {

    public void talk() {
        System.out.println("Miao-miao");
    }
}

Cat class implements Speaks interface and define its own version of talk method.

Spring IoC Bean Configuration File to configure Cat class as a Spring Bean

spring-servlet.xml is our Spring Bean Configuration file that configures Cat class as a Spring IoC bean. 


<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <bean id="cat" class="com.naveen.beans.Cat"/>

</beans>

  • <beans/>: tag is the top level tag for Spring IoC bean configuration file. This tag contains all the beans configurations.
  • <bean/>: tag is used to configure a class as a bean in Spring IoC container. There are two attributes in it, one is “id”,is used to identify a bean in Spring IoC container and also used to get the instance of the bean from Spring IoC container, and the other one is “class”, which defines the fully qualified java class to be configure as a bean.
Runner class (SpringBeanTestRunner.java) of the example

SpringBeanTestRunner class will use Spring IoC to create instance of Cat class and will also invoke talk method in that instance.


package com.naveen.runner;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.naveen.beans.Speaks;

public class SpringBeanTestRunner {

    public static void main(String[] args) {
        System.out.println("Initialing bean factory");
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("spring-servlet.xml");
        System.out.println("Getting cat bean instance");
        Speaks speaks = (Speaks)beanFactory.getBean("cat");
        speaks.talk();
    }
}

ClassPathXmlApplicationContext is used to read our Spring IoC bean configuration file “spring-servlet.xml” from class path and configures all beans. BeanFactory is the class that is used as a factory class of all the bean classes in Spring IoC bean configuration file. BeanFactory is also ClassPathXmlApplicationContext in respective to inheritance. Then we use “getBean” method of BeanFactory to get the bean instance by providing bean id as a parameter.

That's it.You can download  source from bellow links. Import the project in Eclipse and run SpringBeanTestRunner class to test Spring IoC Bean creation technique.

Download Source Code with jars

Click here to download source code

 

0 comments: