Configuring Spring Bean and creating Spring Bea...

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)

Speaks interface has only one method.

package com.raistudies.beans;

public interface Speaks {
    public void talk();
}

Cat Class (Cat.java)

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() {
        System.out.println("Miao-miao");
    }
}

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="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.raistudies.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 abean.

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

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

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");
        speaks.talk();
    }
}

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 from bellow links. Import the project in Eclipse ans run SpringBeanTestRunner class to test Spring IoC Bean creation technique.

Source + lib : Download

你可能感兴趣的:(spring,IOC)