Spring-BeanFactory容器
Spring的BeanFactory容器
这是Spring中最简单地容器,它主要的功能是为依赖注入(DI)提供支持。这个容器接口在org.springframework.beans.factory.BeanFactor中被定义。BeanFactory和行管的接口,比如BeanFactoryAware,DisposableBean,InitializingBean,仍旧保留在Spring中,主要目的是向后兼容已经存在的和那些Spring整合在一起的第三方框架。
在Spring中,有大量对BeanFactory接口的实现。其中,最常被使用的是XmlBeanFactory类。这个容器从一个XML文件中读取配置元数据,由这些元数据来生成一个被配置化的系统或者应用。
在资源宝贵的移动设备或者基于applet的应用当中,BeanFactory会被优先选择。否则一般使用的是ApplicationContext,除非你又更好的理由选择BeanFactory。
例子:
下面是HelloWorld.java的内容:
package com.tutorialspoint;
public class HelloWorld {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
下面是MainApp.java的内容:
package com.tutorialspoint; import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource; public class MainApp {
public static void main(String[] args) {
// ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("Beans.xml"));
HelloWorld h = (HelloWorld) factory.getBean("helloWorld");
System.out.println(h.getMessage());
}
}
在主程序当中,需要注意一下两点:
- 第一步利用框架提供的XmlBeanFactory()API去生成工厂bean以及利用ClassPathResource()API去加载在路径CLASSPATH下可用的bean配置文件。XmlBeanFactory()API负责创建并初始化所有的对象,即在配置文件中提到的bean。
- 第二步利用第一步生成的bean工厂对象的getBean()方法得到所需的bean。这个方法通过配置文件中的beanID来返回一个真正的对象,该对象最后可以用于实际的对象。一旦得到这个对象,就可以利用这个对象来调用方法。
下面是Beans.xml文件的内容:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="helloWorld" class="com.tutorialspoint.HelloWorld">
<property name="message" value="Hello World!"/>
</bean> </beans>
执行的结果:

Spring-BeanFactory容器的更多相关文章
- Spring BeanFactory 容器
Spring 的 BeanFactory 容器 这是一个最简单的容器,它主要的功能是为依赖注入 (DI) 提供支持,这个容器接口在 org.springframework.beans.factory. ...
- Spring的IoC容器-Spring BeanFactory容器
虽然这个方法已经在3.1之后已经弃用了,但是可以了解一下. 在Spring中,有大量对BeanFactory接口的实现.其中,最常被使用的是XmlBeanFactory类.这个容器从一个XML文件中读 ...
- MyEclipse Spring 学习总结一 Spring IOC容器
一.Spring IOC容器---- Spring AllicationContext容器 程序的结构如下: 1.首先在MyEclipse 创建创建Java Project 2.创建好后,添加spin ...
- Spring ——Spring IoC容器详解(图示)
1.1 Spring IoC容器 从昨天的例子当中我们已经知道spring IoC容器的作用,它可以容纳我们所开发的各种Bean.并且我们可以从中获取各种发布在Spring IoC容器里的Bean,并 ...
- Spring IOC 容器 简介
Spring 容器是 Spring 框架的核心.容器将创建对象,把它们连接在一起,配置它们,并管理他们的整个生命周期从创建到销毁. Spring 容器使用依赖注入(DI)来管理组成一个应用程序的组件. ...
- Java框架spring 学习笔记(一):SpringBean、ApplicationContext 容器、BeanFactory容器
Spring容器是Spring框架的核心,容器可以创建对象并创建的对象连接在一起,配置和管理他们的整个生命周期.Spring 容器使用依赖注入(DI)来作为管理应用程序的组件,被称为 Spring B ...
- spring揭密学习笔记(3)-spring ioc容器:Spring的IoC容器之BeanFactory
1. Spring的IoC容器和IoC Service Provider的关系 Spring的IoC容器和IoC Service Provider所提供的服务之间存在一定的交集,二者的关系如图4-1所 ...
- Spring IoC容器的初始化过程
Spring IoC容器的初始化包括 BeanDefinition的Resource定位.载入和注册 这三个基本的过程.IoC容器的初始化过程不包含Bean依赖注入的实现.Bean依赖的注入一般会发生 ...
- 对Spring IoC容器实现的结构分析
本文的目标:从实现的角度来认识SpringIoC容器. 观察的角度:从外部接口,内部实现,组成部分,执行过程四个方面来认识SpringIoC容器. 本文的风格:首先列出SpringIoC的外部接口及内 ...
- 解读Spring Ioc容器设计图
在Spring Ioc容器的设计中,有俩个主要的容器系列:一个是实现BeanFactory接口的简单容器系列,这系列容器只实现了容器最基本的功能:另外一个是ApplicationContext应用上下 ...
随机推荐
- C语言 · 图形输出
算法提高 图形输出 时间限制:1.0s 内存限制:512.0MB 编写一程序,在屏幕上输出如下内容: X | X | X ---+---+--- | | ---+---+--- O ...
- PHP——简单的表单提交
<body> <form name="" method="post" action="CHULI.php"> < ...
- list中存放map实例
list中存放map实例 2016年08月08日 18:46:14 阅读数:22279 List中存放Map遍历输出的实例 import java.util.ArrayList; import ...
- HTML5+Canvas+jQuery调用手机拍照功能实现图片上传(二)
上一篇仅仅讲到前台操作,这篇专门涉及到Java后台处理.前台通过Ajax提交将Base64编码过的图片数据信息传到Java后台,然后Java这边进行接收处理.通过对图片数据信息进行Base64解码,之 ...
- 转载:Python十分钟入门
Python十分钟入门:http://python.jobbole.com/23425/
- 修改Android 界面颜色
btnGetCode.setTextColor(getResources().getColor(R.color.dark_white)); Color.parseColor("#1a71d4 ...
- datatable详解(angular-datatable)+后台分页(springmvc)
datable常规配置,百度一大堆 function prepareDatatable(selector, options) { var defaultOptions = { autoWidth: t ...
- uva 11381(神奇的构图、最小费用最大流)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=29821 思路:首先拆点,每个字符对应的位置拆成i和i+len,然后 ...
- react-native新导航组件react-navigation详解
http://blog.csdn.net/sinat_17775997/article/details/70176688
- PowerShell中的配置文件
http://www.cnblogs.com/ceachy/archive/2013/03/01/PowerShell_Profile.html