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 ( that will implement Speaks interface ( .

Speaks Interface (

Speaks interface has only one method.

package com.raistudies.beans;

public interface Speaks {
    public void talk();

Cat Class (

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

package com.raistudies.beans;

public class Cat implements Speaks {

    public void talk() {

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

app-config.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=""

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

  • <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 abean.

Runner class ( 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.raistudies.runner;

import org.springframework.beans.factory.BeanFactory;

import com.raistudies.beans.Speaks;

public class SpringBeanTestRunner {

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

ClassPathXmlApplicationContext is used to read our Spring IoC bean configuration file “app-config.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.

While running the SpringBeanTestRunner class in eclipse you will get following output (the text in red are generated by logger class, you can avoid it):

Spring IoC Bean Creation Example Output

Spring IoC Bean Creation Example Output

You can download full project or source from bellow links. Import the project in Eclipse ans run SpringBeanTestRunner class to test Spring IoC Bean creation technique.

Source: Download

Source + lib : Download

Related Posts:

Leave a Comment Cancel reply

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>