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 ...
随机推荐
- WCF-Configuration
Host-Configuration <?xml version="1.0"?> <configuration> <configSections> ...
- 使用ab测试工具 进行并发测试
ab.exe -n1000 -c100 http://localhost:8067/api/todo/555e95feb301baa678141148 http://www.cnblogs.com/y ...
- iOS 进阶 第十三天(0414)
0414 画线.圆和圆弧的第二种方法 5中方法画矩形: 第二种画图形的方法: 之前做的方法都是先把要画的元素缓存到图形上下文CGConteextRef中去,现在第二种方法也缓存到CGPath中去.其实 ...
- SpringMVC Cache注解+Redis
依赖jar包:Xml代码 收藏代码 <!-- redis --> <dependency> <groupId ...
- phpcms v9 企业黄页加入企业会员提示“请选择企业库类型!”
很多新进站长选择使用了PHPCMS v9内容管理系统,它强大的功能无疑吸引了很多人,尤其是企业黄页功能更是其中的佼佼者,但是官方发布的版本兼容性比较差,出现会员加入企业会员提示“请选择企业库类型!”, ...
- 包装设计模式的实现以改进BufferedReader中的readLine方法为例
实现与目标对象相同的接口 BufferedReader 定义一个变量记住目标对象 定义一个构造器接收被增强对象 覆盖需要增强的方法 对于不想增强的方法,直接调用目标对象的方法. package ...
- Xcode8兼容iOS7手记-b
对于Xcode8的发布,苹果也是来了个大的跳跃,默认最低支持的iOS版本为8.0,当然也并不是说8.0以下就直接放弃了,虽然表现出来的是这样,毕竟使用8.0以下系统的还是大有人在的,老项目要兼容iOS ...
- 【转】如何设置Android软键盘的默认不弹出?
在开发Anroid的时候,当你打开一个界面的时候,屏幕的焦点会自动停留在第一个EditText中,Android的软键盘默认会自动弹出,用户第一眼连界面都没有看清楚,软键盘就弹出来了,这就影响到了用户 ...
- eclipse svn 修改了类名之后提交
win下面的文件名不区分大小写,所以不能只是把小写类名改成大写. 正确的做法有如下两种:1,先删除类a,提交,此操作会删除服务器上的文件.再添加类A,提交.2,重命名a为aa,提交,此操作会删除服务器 ...
- PHP之SQL防注入代码(360提供)
<?php class sqlsafe { private $getfilter = "'|(and|or)\\b.+?(>|<|=|in|like)|\\/\\*.+?\ ...