Spring-ApplicationContext解读

xiaoxiao2021-02-28  121

文章目录

BeanFactory和ApplicationContextApplicationContextApplicationContext概述ApplicationContext类体系结构ApplicationContext初始化ApplicationContext两个主要实现类ClassPathXmlApplicationContextFileSystemXmlApplicationContext 加载一组配置文件基于类注解的配置方式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两个主要实现类

ClassPathXmlApplicationContextFileSystemXmlApplicationContext

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 stub ApplicationContext 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.


转载请注明原文地址: https://www.6miu.com/read-47971.html

最新回复(0)