Spring-ApplicationContext解读
文章目录
- BeanFactory和ApplicationContext
- ApplicationContext
- ApplicationContext概述
- ApplicationContext类体系结构
- ApplicationContext初始化
- ApplicationContext两个主要实现类
- ClassPathXmlApplicationContext
- FileSystemXmlApplicationContext
- 加载一组配置文件
- 基于类注解的配置方式
- Spring4支持Groovy DSL配置Bean
BeanFactory和ApplicationContext
Spring通过一个配置文件描述Bean和Bean之间的依赖关系,利用Java反射功能实例化Bean,并建立Bean之间的依赖关系。
Spring的IOC容器在完成这些底层工作的基础上,还提供了Bean实例缓存、生命周期管理、Bean实例代理、时间发布、资源装载等高级服务。
BeanFactory是Spring框架最核心的接口,它提供了高级IOC的配置机制。
ApplicationContext建立在BeanFactory的基础上,提供了更多面向应用的功能, 它提供了国际化支持和框架事件体系。
我们一般称BeanFactory为IoC容器,而称ApplicationContext为应用上下文,但有时候为了行文方便,我们也将ApplicationContext称为Spring容器。
对于BeanFactory 和 ApplicationContext的用途:
- BeanFactory是Spring框架的基础设施,面向Spring本身
- ApplicationContext面向使用Spring框架的开发者,几乎所有的应用场合都可以直接使用Application而非底层的BeanFactory.
ApplicationContext
ApplicationContext概述
Application由BeanFactory派生而来,提供了更多面向实际的功能。
在BeanFactory中,很多功能都需要以编程的形式实现,但是在Application中则可以通过配置的方式实现。
ApplicationContext类体系结构
Application继承了HierachicalBeanFactory和ListableBeanFactory接口,在此基础上,还通过其他接口扩展了BeanFactory的功能。
这些接口如下:
- ApplicationEventPublisher:让容器拥有发布应用上下文事件的功能,包括容器的启动事件、关闭事件等。
- MessageSource:为应用提供i18N国际化消息访问的功能
- ResourcePatternResolver:加载资源,可以通过带前缀的Ant风格的资源文件路径装载Spring的配置文件
- LifeCycle:该接口提供start()和stop()方法,主要用于控制异步处理的过程,以达到管理和控制JMX、任务调度等目的
ConfigurableApplicationContext扩展于ApplicationContext,主要新增了两个方法 refresh()和close(),让Application具有启动、刷新、关闭应用上下文的能力。
在应用上下文关闭的情况下,refresh可以启动上下文,在启动的情况下可以清楚缓存并重新装载配置信息。 close方法这可以关闭上下文。
以上内容了解即可。
ApplicationContext初始化
在获取ApplicationContext实例后,我们就可以像BeanFactory那样调用getBean(beanName)返回Bean了。
ApplicationContext的初始化和BeanFactory初始化有一个重大区别:
BeanFactory在初始化容器时,并没有实例化Bean,直到第一次访问某个Bean时才实例化目标Bean。
ApplicationContext会在初始化应用上下文时就实例化所有单实例的Bean。
因此,ApplicationContext的初始化时间会比BeanFactory的时间稍微长一些,不过稍后的调用则没有“第一次惩罚”的问题。
ApplicationContext两个主要实现类
- ClassPathXmlApplicationContext
- FileSystemXmlApplicationContext
Plane.java和bean-plane.xml请参考
http://blog.csdn.net/yangshangwei/article/details/74910935#t3
ClassPathXmlApplicationContext
package com.xgj.master.ioc.beanfactory;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class ClassPathXmlApplicationTest {public static void main(String[] args) {ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:com/xgj/master/ioc/beanfactory/bean-plane.xml");Plane plane = (Plane) applicationContext.getBean("plane");plane.introduce();}}
如果配置文件放在在类路径下,则优先考虑使用ClassPathXmlApplication实现类
对于 ClassPathXmlApplicationContext 来讲:
new ClassPathXmlApplicationContext("classpath:com/xgj/master/ioc/beanfactory/bean-plane.xml");
等同于
new ClassPathXmlApplicationContext("com/xgj/master/ioc/beanfactory/bean-plane.xml");
FileSystemXmlApplicationContext
package com.xgj.master.ioc.beanfactory;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;public class FileSystemXmlApplicationTest {public static void main(String[] args) {ApplicationContext app = new FileSystemXmlApplicationContext("file:D:/workspace/workspace-jee/HelloSpring/Spring4-char2/src/com/xgj/master/ioc/beanfactory/bean-plane.xml");Plane plane = (Plane) app.getBean("plane");plane.introduce();}}
如果配置文件放在文件系统路径下,则优先考虑使用FileSystemXmlApplication实现类
对于FileSystemXmlApplication来讲,
"file:D:/workspace/workspace-jee/HelloSpring/Spring4-char2/src/com/xgj/master/ioc/beanfactory/bean-plane.xml"
等同于
"D:/workspace/workspace-jee/HelloSpring/Spring4-char2/src/com/xgj/master/ioc/beanfactory/bean-plane.xml"
运行结果:
加载一组配置文件
初始化配置文件时,还可以指定一组配置文件,Spring会自动对多个配置文件在内存中“整合”成一个配置文件。
举例:
我们新增一个Car类,和Plane中的属性方法一致,增加一个配置文件bean-car.xml,和bean-plane.xml中的结构一致。
ClassPathXmlApplicationContext加载一组资源文件
//两种写法
ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] {"com/xgj/master/ioc/beanfactory/bean-plane.xml", "com/xgj/master/ioc/beanfactory/bean-car.xml" });ApplicationContext ctx2 = new ClassPathXmlApplicationContext("com/xgj/master/ioc/beanfactory/bean-plane.xml", "com/xgj/master/ioc/beanfactory/bean-car.xml");Plane plane2 = (Plane) ctx2.getBean("plane");
Car car = (Car) ctx2.getBean("car");plane2.introduce();
car.introduce();
FileSystemXmlApplicationContext加载一组资源文件
// 加载多组资源文件
ApplicationContext ctx = new FileSystemXmlApplicationContext("file:D:/workspace/workspace-jee/HelloSpring/Spring4-char2/src/com/xgj/master/ioc/beanfactory/bean-plane.xml","file:D:/workspace/workspace-jee/HelloSpring/Spring4-char2/src/com/xgj/master/ioc/beanfactory/bean-car.xml");Plane plane2 = (Plane) ctx.getBean("plane");
Car car = (Car) ctx.getBean("car");plane2.introduce();
car.introduce();
运行结果:
基于类注解的配置方式
Spring支持基于类注解的配置方式,主要来源于Spring的子项目JavaConfig。 目前JavaConfig已经成为Spring核心框架的一部分。
这里只是简单的演示用法,后续更新详情。
一个标注了@Configuration注解的POJO即可提供Spring所需要的配置信息。
代码如下:
Beans.java 配置信息提供类
package com.xgj.spring4.chapter4.configuration;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration // @Configuration表示是一个配置信息提供类
public class Beans {@Bean(name = "plane") // ͨ定义一个Bean,通过name指定bean的名称public Plane createPlane() {Plane plane = new Plane();plane.setBrand("F35");plane.setColor("GREY");plane.setMaxSpeed(800);return plane;}}
Plane.java
package com.xgj.spring4.chapter4.configuration;public class Plane {private String brand;private String color;private int maxSpeed;...省略setter/getter方法public void introduce(){System.out.println("Plane brand:" + brand + " ,Color:" + color + ",maxSpeed:" + maxSpeed);}
}
AnnotationConfigApplicationTest.java 测试:
package com.xgj.spring4.chapter4.configuration;import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class AnnotationConfigApplicationTest {public static void main(String[] args) {// ͨ通过一个带有@Configuration的POJO装载Bean的配置ApplicationContext ctx = new AnnotationConfigApplicationContext(Beans.class);Plane plane = ctx.getBean("plane", Plane.class);plane.introduce();}}
运行结果:
解析:
Spring为基于注解类的配置提供了专门的ApplicationContext实现类:AnnotationConfigApplication。
AnnnotationConfigApplication将加载Beans.class中的Bean定义并调用Beans.class中的方法实例化Bean,启动并装载Bean。
Spring4支持Groovy DSL配置Bean
Spring4.0支持使用Groovy DSL来进行Bean的定义, 与XML文件的配置类似,只不过基于Groovy脚本语言,可以实现更加负载灵活的Bean的配置逻辑。
步骤:
- 添加依赖包
<dependency><groupId>org.codehaus.groovy</groupId><artifactId>groovy-all</artifactId><version>2.3.6</version></dependency>
- 编写代码
groovy-bean.groovy 配置信息
package com.xgj.spring4.chapter4.groovy;beans{plane(Plane){brand = "A380";color = "red";maxSpeed = 700;}
}
Plane同上。
测试类:
package com.xgj.spring4.chapter4.groovy;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericGroovyApplicationContext;public class GroovyApplicationContextTest {public static void main(String[] args) {// TODO Auto-generated method stubApplicationContext ctx = new GenericGroovyApplicationContext("classpath:com/xgj/spring4/chapter4/groovy/groovy-bean.groovy");Plane plane = (Plane)ctx.getBean("plane");plane.introduce();}}
基于Groovy的配置方式很容易让开发者配置负载Bean的实例化过程,比基于XML文件、注解的配置方式更加灵活。
Spring为依据Groovy的配置提供了专门的Application实现类:GenericiGroovyApplicationContext.
Spring-ApplicationContext解读相关推荐
- Spring MVC 解读——@Autowired
2019独角兽企业重金招聘Python工程师标准>>> Spring MVC 解读--@Autowired 一.@Autowired 作为一个Spring开发者对@Autowired ...
- 使用Arthas 获取Spring ApplicationContext还原问题现场
背景 最近来了个实习僧小弟,安排他实现对目标网站 连通性检测的小功能,简单讲就是将下边的shell 脚本换成Java 代码来实现 1#!/bin/bash2URL="https://www. ...
- Spring MVC 解读——@Autowired、@Controller、@Service从原理层面来分析
Spring MVC 解读--@Autowired 一.@Autowired 作为一个Spring开发者对@Autowired注解必定是非常了解了, 顾名思义自动装配,应该是Spring会自动将我们标 ...
- Spring MVC 解读——context:component-scan/
2019独角兽企业重金招聘Python工程师标准>>> Spring MVC 解读---<context:component-scan/> 注解是骑士魂牵梦绕的美丽公主, ...
- Spring: ApplicationContext cannot be resolved to a type
Spring: ApplicationContext cannot be resolved to a type 创建spring的IOC对象时 ApplicationContext ctx = new ...
- 从源码看spring applicationContext在web容器中加载过程
2019独角兽企业重金招聘Python工程师标准>>> 原文:http://blog.csdn.net/fuliangliang/archive/2006/12/22/1454382 ...
- Spring MVC 解读——mvc:annotation-driven/
2019独角兽企业重金招聘Python工程师标准>>> 一.AnnotationDrivenBeanDefinitionParser 通常如果我们希望通过注解的方式来进行Spring ...
- Spring:applicationContext.xml的头文件信息
文章目录 Spring介绍 1.内核 2.提供服务 3.特性 4.体系结构 xml头文件代码展示 Spring介绍 分层的JavaSE/EE,应用full-stack(全栈)轻量级开源框架. 1.内核 ...
- spring applicationContext.xml最全约束
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- Spring - Java/J2EE Application Framework 应用框架 第 3 章 Beans, BeanFactory和ApplicationContext
第 3 章 Beans, BeanFactory和ApplicationContext 3.1. 简介 在Spring中,两个最基本最重要的包是 org.springframework.beans 和 ...
最新文章
- TensorFlow、PyTorch之后,“国产”AI框架还有没有机会?
- bat 复制文件夹_怎么生成电脑上文件夹的目录结构
- 文件修改如何简单修改Android的so文件
- Maven-pon.xml模板(ssm自用)持续更新
- Python基础之字典
- QQ截图自动保存工具分享
- 文件读写和字符串、列表的排序
- numpy实现BN层
- C语言 55555图形 找车牌问题
- (10)python日志收集
- 华研计算机系统苹果用不了,系统瘦身:Apple 苹果 在 macOS Catalina 中取消 Dashboard...
- 思科ccie认证网络工程师——MAC地址认证和MAC地址旁路认证解析必看
- TypeScript02 方法特性【参数种类、参数个数】、generate方法、析构表达式、箭头表达式、循环...
- 叶雕——叹为观止的艺术!
- nrf51822代码流程(从main展开)
- 在linux中使用getch()函数
- Markdown书写软件Typora的使用 -- 渲染(让你的Typora更上档次)
- VMware Workstation Pro 16下载地址及key
- kendryte K210开发:关于MAIX BIT 无法使用 kendryte IDE 下载的问题
- Record: Linux指令笔记