文档章节

Configuring Spring Bean and creating Spring Bea...

xyxo
 xyxo
发布于 2012/11/07 14:18
字数 466
阅读 43
收藏 0

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

本文转载自:http://www.raistudies.com/spring/creating-spring-bean/

xyxo

xyxo

粉丝 8
博文 19
码字总数 5259
作品 0
深圳
程序员
私信 提问
jeesite没有网络时部署出错

jeesite项目在联网的状态下可以正常运行, 在断开网络之后部署失败了, 出错日志如下, 求大神解答! Caused by: org.springframework.beans.factory.BeanCreationException: Error creating be...

ufwsse
2016/05/23
316
0
:war exploded: Error during artifact deployment.

07:55:45.125 ERROR org.springframework.web.context.ContextLoader [] (admin-listener(4)) [o.s.web.context.ContextLoader] -- Context initialization failed org.springframework.bean......

kwyxiaoli
2014/06/03
9.1K
2
江湖救急:maven打包了dubbo服务的一个jar包,本机运行可以,虚拟机不行

我搭建dubbo服务,用maven打包了一个服务,本地运行没问题,但是放到虚拟机上,运行就出问题了 The last packet sent successfully to the server was 0 milliseconds ago. The driver has n...

君千殇520
2016/10/13
2.3K
4
springboot+myatis,@Autowired报错

@MapperScan(value="com.XX") @SpringBootApplication public class DsamsApp{ public static void main(String[] args) { SpringApplication.run(DsamsApp.class, args); } } public class ......

wb520
2018/01/31
123
5
Error during artifact deployment.

07:55:45.125 ERROR org.springframework.web.context.ContextLoader [] (admin-listener(4)) [o.s.web.context.ContextLoader] -- Context initialization failed org.springframework.bean......

kwyxiaoli
2014/06/03
5.8K
2

没有更多内容

加载失败,请刷新页面

加载更多

CSS定位

CSS定位 relative相对定位 absolute绝对定位 fixed和sticky及zIndex relative相对定位 position特性:css position属性用于指定一个元素在文档中的定位方式。top、right、bottom、left属性则...

studywin
19分钟前
4
0
从零基础到拿到网易Java实习offer,我做对了哪些事

作为一个非科班小白,我在读研期间基本是自学Java,从一开始几乎零基础,只有一点点数据结构和Java方面的基础,到最终获得网易游戏的Java实习offer,我大概用了半年左右的时间。本文将会讲到...

Java技术江湖
昨天
5
0
程序性能checklist

程序性能checklist

Moks角木
昨天
7
0
VUE 计算属性

本文转载于:专业的前端网站▶VUE 计算属性 1、示例代码 <!DOCTYPE html><html lang="zh"> <head> <meta charset="UTF-8" /> <title>vue示例</title> </hea......

前端老手
昨天
6
0
快速搭建LNMT平台和环境部署 Tomcat详解

Tomcat部署的基本概念 1. CATALINA_HOME与CATALINA_BASE分别指什么?     CATALINA_HOME指的是Tomcat的安装目录     bin:\\Tomcat一些脚本存放目录,比如启动脚本startup.bat/start...

网络小虾米
昨天
8
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部