博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Configuring Spring Bean and creating Spring Bea...
阅读量:6710 次
发布时间:2019-06-25

本文共 2995 字,大约阅读时间需要 9 分钟。

hot3.png

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.

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

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 :

转载于:https://my.oschina.net/panjavay/blog/87574

你可能感兴趣的文章
Docker安装及基础命令
查看>>
ARP欺骗
查看>>
输入一个字符串,统计该字符串中分别包含多少个数字,多少个字母,多少个其他字符...
查看>>
请求重定向sendRedirect()方法 和 请求转发forward()方法
查看>>
Oracle专题12之游标
查看>>
两句话笔记--架构学习之一:并发基础课程(2)
查看>>
LINUX概念与常识
查看>>
SqlServer 添加用户 添加角色 分配权限
查看>>
HBase解决Region Server Compact过程占用大量网络出口带宽的问题
查看>>
Shell编程(基础)
查看>>
CSS3的线性渐变(linear-gradient)
查看>>
环境变量
查看>>
K盘显示文件系统变没,要怎样恢复资料
查看>>
windows常用命令整理
查看>>
网络安全与管理精讲视频笔记5-认证中心及证书原理
查看>>
使用andbug的monitor命令
查看>>
zabbix服务器设置邮箱报警
查看>>
文本三剑客之grep加vim编辑器
查看>>
细说mysql索引
查看>>
云栖专辑| 阿里毕玄:程序员的成长路线
查看>>