Spring通过一个配置文件描述Bean及Bean直接的依赖关系,利用Java语言的反射功能实例化Bean并建立Bean之间的依赖关系。Sprig的IoC容器在完成这些底层工作的基础上,还提供了Bean实例缓存、生命周期管理、Bean实例代理、事件发布、资源装载等高级服务。

    Bean工厂(com.springframework.beans.factory.BeanFactory)是Spring框架最核心的接口,它提供了高级IoC的配置机制。BeanFactory使管理不同类型的Java对象成为可能,应用上下文(com.springframework.context.ApplicationContext)建立在BeanFactory基础之上,提供了更多面向应用的功能,它提供了国际化支持和框架事件体系,更易于创建实际应用。我们一般称BeanFactory为IoC容器,而称 ApplicationContext为应用上下文。但有时为了行文方便,我们也将ApplicationContext称为Spring容器。
    BeanFactory是Spring框架的基础设施,面向Spring本身;ApplicationContext面向使用Spring框架的开发者,几乎所有的应用场合都直接使用ApplicationContext而非底层的BeanFactory。
 
1、初始化BeanFactory
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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="car" class="com.reflect.Car"
p:brand="迈锐宝"
p:color="黑色"
p:maxSpeed="300"/>
</beans>

BeanFactoryTest:

package com.beanfactory;
import com.reflect.Car;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import java.io.IOException;
/**
* Created by gao on 16-3-18.
*/
public class BeanFactoryTest {
public static void main(String[] args) throws IOException {
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource res = resolver.getResource("classpath:beans.xml");
BeanFactory bf = new XmlBeanFactory(res);
System.out.println("init BeanFactory.");
Car car = bf.getBean("car", Car.class);
System.out.println("car bean is ready for use!");
}
}
在初始化BeanFactory时,必须为其提供一种日志框架,我们使用Log4J,即在类路径下提供Log4J配置文件,这样启动Spring容器才不会报错。
log4j.properties
log4j.rootLogger=INFO,A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d %5p [%t] (%F:%L) - %m%n
测试输出:
2016-03-18 17:19:27,045  INFO [main] (XmlBeanDefinitionReader.java:315) - Loading XML bean definitions from class path resource [beans.xml]
init BeanFactory.
car bean is ready for use!
 
 
2、ApplicationContext
    ApplicationContext使用ClassPathXmlApplicationContext和FileSystemXMLApplicationContext,前者默认从类路径下加载配置文件,后者默认从文件系统中装载配置文件。在获取ApplicationContext实例后,就可以像BeanFactory一样调用getBean(beanName)返回Bean了。BeanFactory在初始化容器时,并未实例化Bean,直到第一次访问某个Bean时才实例目标Bean;而ApplicationContext则在初始化应用上下文时就实例化所以单实例的Bean。
Beans:

package com.context;
import com.reflect.Car;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
/**
* Created by gao on 16-3-18.
*/
@Configurable
public class Beans {
@Bean(name = "car")
public Car buildCar() {
Car car = new Car();
car.setBrand("英菲迪尼");
car.setMaxSpeed(300);
return car;
}
}

AnnotationApplicationContext:

package com.context;
import com.reflect.Car;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* Created by gao on 16-3-18.
*/
public class AnnotationApplicationContext {
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(Beans.class);
Car car = ctx.getBean("car", Car.class);
System.out.println(car.getBrand());
System.out.println(car.getMaxSpeed());
}
}

 
 
 
 
 
 
 
 

BeanFactory 和 ApplicationContext的更多相关文章

  1. Spring系列之beanFactory与ApplicationContext

    一.BeanFactoryBeanFactory 是 Spring 的“心脏”.它就是 Spring IoC 容器的真面目.Spring 使用 BeanFactory 来实例化.配置和管理 Bean. ...

  2. BeanFactory和ApplicationContext的区别

     1.BeanFactory和ApplicationContext的异同点: 相同点:     两者都是通过xml配置文件加载bean,ApplicationContext和BeanFacotry相比 ...

  3. BeanFactory和ApplicationContext的作用和区别

    BeanFactory和ApplicationContext的作用和区别 作用: 1. BeanFactory负责读取bean配置文档,管理bean的加载,实例化,维护bean之间的依赖关系,负责be ...

  4. spring中的BeanFactory与ApplicationContext的作用和区别?

    BeanFactory类关系继承图 1. BeanFactory类结构体系: BeanFactory接口及其子类定义了Spring IoC容器体系结构,由于BeanFactory体系非常的庞大和复杂, ...

  5. BeanFactory 和ApplicationContext

    BeanFactory和ApplicationContext对待bean后置处理器稍有不同.ApplicationContext会自动检测在配置文件中实现了BeanPostProcessor接口的所有 ...

  6. 【学习笔记】Spring中的BeanFactory和ApplicationContext 以及 Bean的生命周期(Y2-3-2)

    BeanFactory和ApplicationContext Spring的IoC容器就是一个实现了BeanFactory接口的可实例化类. Spring提供了两种不同的容器: 一种是最基本的Bean ...

  7. spring BeanFactory及ApplicationContext中Bean的生命周期

    spring bean 的生命周期 spring BeanFactory及ApplicationContext在读取配置文件后.实例化bean前后.设置bean的属性前后这些点都可以通过实现接口添加我 ...

  8. BeanFactory和ApplicationContext的简单介绍

    引言 Spring通过一个配置文件描述Bean及Bean之间的依赖关系,利用Java语音的反射功能实例化Bean并建立Bean之间的依赖关系.Spring的IoC容器在完成这些底层工作的基础上,还提供 ...

  9. 转:Spring系列之beanFactory与ApplicationContext

    原文地址:Spring系列之beanFactory与ApplicationContext 一.BeanFactoryBeanFactory 是 Spring 的“心脏”.它就是 Spring IoC ...

  10. Spring中BeanFactory与ApplicationContext的区别

    BeanFactory:Bean工厂接口,是访问Spring Bean容器的根接口,基本Bean视图客户端.从其名称上即可看出其功能,即实现Spring Bean容器的读取. ApplicationC ...

随机推荐

  1. asp.net 生成PDF方法

    今天转博客园看到有人发表了一篇生成PFd的文章,准备自己也留一份准备以后用到的时候方便调用: 首先去itextsharp网站下载控件(https://sourceforge.net/projects/ ...

  2. String面试题

    //a b c 分别是怎么存储的, a和b a和c分别有什么区别// c和d的区别是什么 String a= "hello";String b= "hello" ...

  3. VBS基础篇 - RegExp 对象

    正则表达式(RegExp)对象下面的代码说明了RegExp对象的用法: Function RegExpTest(patrn, strng) Dim regEx, Match, Matches '创建变 ...

  4. ios状态栏调整 简单动画的知识点

    首先状态栏式view的属性,所以在viewController中直接重写: /** 修改状态栏 */ - (UIStatusBarStyle)preferredStatusBarStyle { // ...

  5. Android Studio 单刷《第一行代码》系列 01 —— 第一战 HelloWorld

    前言(Prologue) 本系列将使用 Android Studio 将<第一行代码>(书中讲解案例使用Eclipse)刷一遍,旨在为想入坑 Android 开发,并选择 Android ...

  6. Careercup - Facebook面试题 - 23869663

    2014-05-02 03:37 题目链接 原题: A string is called sstring if it consists of lowercase english letters and ...

  7. Discuz!NT 后台任意文件上传的源代码修补方法

    相关的漏洞介绍: http://www.wooyun.org/bugs/wooyun-2013-035199 Discuz!NT 管理后台可以自定义文件类型,从而造成任意文件上传,测试过程中 aspx ...

  8. [原]android不支持命名的semaphore

    之前sem_open在iOS上, 创建命名的semaphore没有问题 (iOS不支持匿名的semaphore), 但是现在Android平台的sem_open时候报错,返回ENOSYS. 命名的se ...

  9. Codeforces Round #241 (Div. 2)->B. Art Union

    B. Art Union time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  10. uva 11174

    刘书上例题 #include <cstdio> #include <cstdlib> #include <cmath> #include <map> # ...