前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring-ApplicationContext解读

Spring-ApplicationContext解读

作者头像
小小工匠
发布2021-08-16 16:19:23
3910
发布2021-08-16 16:19:23
举报
文章被收录于专栏:小工匠聊架构小工匠聊架构

文章目录

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

代码语言:javascript
复制
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 来讲:

代码语言:javascript
复制
new ClassPathXmlApplicationContext(
			"classpath:com/xgj/master/ioc/beanfactory/bean-plane.xml");

等同于

代码语言:javascript
复制
new ClassPathXmlApplicationContext(
			"com/xgj/master/ioc/beanfactory/bean-plane.xml");

FileSystemXmlApplicationContext

代码语言:javascript
复制
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来讲,

代码语言:javascript
复制
"file:D:/workspace/workspace-jee/HelloSpring/Spring4-char2/src/com/xgj/master/ioc/beanfactory/bean-plane.xml"

等同于

代码语言:javascript
复制
"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加载一组资源文件

代码语言:javascript
复制
//两种写法	
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加载一组资源文件

代码语言:javascript
复制
// 加载多组资源文件
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 配置信息提供类

代码语言:javascript
复制
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

代码语言:javascript
复制
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 测试:

代码语言:javascript
复制
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的配置逻辑。

这里写图片描述
这里写图片描述

步骤:

  • 添加依赖包
代码语言:javascript
复制
	<dependency>
			<groupId>org.codehaus.groovy</groupId>
			<artifactId>groovy-all</artifactId>
			<version>2.3.6</version>
		</dependency>
  • 编写代码 groovy-bean.groovy 配置信息
代码语言:javascript
复制
package com.xgj.spring4.chapter4.groovy;

beans{
	plane(Plane){
		brand = "A380";
		color = "red";
		maxSpeed = 700;
	}
}

Plane同上。

测试类:

代码语言:javascript
复制
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.


本文参与?腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-07-10 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客?前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体同步曝光计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • BeanFactory和ApplicationContext
  • ApplicationContext
    • ApplicationContext概述
      • ApplicationContext类体系结构
        • ApplicationContext初始化
          • ApplicationContext两个主要实现类
            • ClassPathXmlApplicationContext
            • FileSystemXmlApplicationContext
          • 加载一组配置文件
            • 基于类注解的配置方式
              • Spring4支持Groovy DSL配置Bean
              相关产品与服务
              容器服务
              腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
              http://www.vxiaotou.com