BeanFactory 和 ApplicationContext
Spring通过一个配置文件描述Bean及Bean直接的依赖关系,利用Java语言的反射功能实例化Bean并建立Bean之间的依赖关系。Sprig的IoC容器在完成这些底层工作的基础上,还提供了Bean实例缓存、生命周期管理、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: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!");
}
}
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
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的更多相关文章
- Spring系列之beanFactory与ApplicationContext
一.BeanFactoryBeanFactory 是 Spring 的“心脏”.它就是 Spring IoC 容器的真面目.Spring 使用 BeanFactory 来实例化.配置和管理 Bean. ...
- BeanFactory和ApplicationContext的区别
1.BeanFactory和ApplicationContext的异同点: 相同点: 两者都是通过xml配置文件加载bean,ApplicationContext和BeanFacotry相比 ...
- BeanFactory和ApplicationContext的作用和区别
BeanFactory和ApplicationContext的作用和区别 作用: 1. BeanFactory负责读取bean配置文档,管理bean的加载,实例化,维护bean之间的依赖关系,负责be ...
- spring中的BeanFactory与ApplicationContext的作用和区别?
BeanFactory类关系继承图 1. BeanFactory类结构体系: BeanFactory接口及其子类定义了Spring IoC容器体系结构,由于BeanFactory体系非常的庞大和复杂, ...
- BeanFactory 和ApplicationContext
BeanFactory和ApplicationContext对待bean后置处理器稍有不同.ApplicationContext会自动检测在配置文件中实现了BeanPostProcessor接口的所有 ...
- 【学习笔记】Spring中的BeanFactory和ApplicationContext 以及 Bean的生命周期(Y2-3-2)
BeanFactory和ApplicationContext Spring的IoC容器就是一个实现了BeanFactory接口的可实例化类. Spring提供了两种不同的容器: 一种是最基本的Bean ...
- spring BeanFactory及ApplicationContext中Bean的生命周期
spring bean 的生命周期 spring BeanFactory及ApplicationContext在读取配置文件后.实例化bean前后.设置bean的属性前后这些点都可以通过实现接口添加我 ...
- BeanFactory和ApplicationContext的简单介绍
引言 Spring通过一个配置文件描述Bean及Bean之间的依赖关系,利用Java语音的反射功能实例化Bean并建立Bean之间的依赖关系.Spring的IoC容器在完成这些底层工作的基础上,还提供 ...
- 转:Spring系列之beanFactory与ApplicationContext
原文地址:Spring系列之beanFactory与ApplicationContext 一.BeanFactoryBeanFactory 是 Spring 的“心脏”.它就是 Spring IoC ...
- Spring中BeanFactory与ApplicationContext的区别
BeanFactory:Bean工厂接口,是访问Spring Bean容器的根接口,基本Bean视图客户端.从其名称上即可看出其功能,即实现Spring Bean容器的读取. ApplicationC ...
随机推荐
- asp.net 生成PDF方法
今天转博客园看到有人发表了一篇生成PFd的文章,准备自己也留一份准备以后用到的时候方便调用: 首先去itextsharp网站下载控件(https://sourceforge.net/projects/ ...
- String面试题
//a b c 分别是怎么存储的, a和b a和c分别有什么区别// c和d的区别是什么 String a= "hello";String b= "hello" ...
- VBS基础篇 - RegExp 对象
正则表达式(RegExp)对象下面的代码说明了RegExp对象的用法: Function RegExpTest(patrn, strng) Dim regEx, Match, Matches '创建变 ...
- ios状态栏调整 简单动画的知识点
首先状态栏式view的属性,所以在viewController中直接重写: /** 修改状态栏 */ - (UIStatusBarStyle)preferredStatusBarStyle { // ...
- Android Studio 单刷《第一行代码》系列 01 —— 第一战 HelloWorld
前言(Prologue) 本系列将使用 Android Studio 将<第一行代码>(书中讲解案例使用Eclipse)刷一遍,旨在为想入坑 Android 开发,并选择 Android ...
- Careercup - Facebook面试题 - 23869663
2014-05-02 03:37 题目链接 原题: A string is called sstring if it consists of lowercase english letters and ...
- Discuz!NT 后台任意文件上传的源代码修补方法
相关的漏洞介绍: http://www.wooyun.org/bugs/wooyun-2013-035199 Discuz!NT 管理后台可以自定义文件类型,从而造成任意文件上传,测试过程中 aspx ...
- [原]android不支持命名的semaphore
之前sem_open在iOS上, 创建命名的semaphore没有问题 (iOS不支持匿名的semaphore), 但是现在Android平台的sem_open时候报错,返回ENOSYS. 命名的se ...
- 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 ...
- uva 11174
刘书上例题 #include <cstdio> #include <cstdlib> #include <cmath> #include <map> # ...