Spring-ApplicationContext解读

简介: Spring-ApplicationContext解读

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

相关文章
|
安全 Java 开发者
Java集合框架:详解Deque接口的栈操作方法全集
理解和掌握这些方法对于实现像浏览器后退功能这样的栈操作来说至关重要,它们能够帮助开发者编写既高效又稳定的应用程序。此外,在多线程环境中想保证线程安全,可以考虑使用ConcurrentLinkedDeque,它是Deque的线程安全版本,尽管它并未直接实现栈操作的方法,但是Deque的接口方法可以相对应地使用。
761 12
|
Java Maven 开发工具
maven导入项目出现Unable to import maven project: See logs for details
maven导入项目出现Unable to import maven project: See logs for details
659 0
maven导入项目出现Unable to import maven project: See logs for details
|
监控 Java 编译器
Java虚拟机调优指南####
本文深入探讨了Java虚拟机(JVM)调优的精髓,从内存管理、垃圾回收到性能监控等多个维度出发,为开发者提供了一系列实用的调优策略。通过优化配置与参数调整,旨在帮助读者提升Java应用的运行效率和稳定性,确保其在高并发、大数据量场景下依然能够保持高效运作。 ####
524 58
|
Java Spring 容器
spring的四种注入方式
spring的四种注入方式
886 0
|
Java Linux 调度
Java【付诸实践 03】Spring定时任务注解@Scheduled+@EnableAsync用法详解(简单说明+应用场景+demo源代码+执行过程分析)
Java【付诸实践 03】Spring定时任务注解@Scheduled+@EnableAsync用法详解(简单说明+应用场景+demo源代码+执行过程分析)
674 2
|
存储 前端开发 Java
springboot中的第二个IOC容器BootstrapContext
springboot中的第二个IOC容器BootstrapContext
springboot中的第二个IOC容器BootstrapContext
|
Arthas 人工智能 监控
JVM工作原理与实战(三十五):性能调优
JVM作为Java程序的运行环境,其负责解释和执行字节码,管理内存,确保安全,支持多线程和提供性能监控工具,以及确保程序的跨平台运行。本文主要介绍了性能调优、性能调优案例等内容。
1032 1
|
Ubuntu Linux
在Linux中,如何查看当前系统的版本信息?
在Linux中,如何查看当前系统的版本信息?
|
Java 测试技术 开发者
Java线程池ThreadPoolExcutor源码解读详解09-4种拒绝策略
本文介绍了线程池的四种拒绝策略:AbortPolicy、DiscardPolicy、DiscardOldestPolicy和CallerRunsPolicy,并通过代码示例展示了它们在任务过多时的不同处理方式。AbortPolicy会抛出异常并停止主线程;DiscardPolicy会默默丢弃新任务;DiscardOldestPolicy会抛弃队列中最旧的任务来接纳新任务;而CallerRunsPolicy则是由调用者线程执行被拒绝的任务,以减缓新任务的提交速度。这四种策略适用于不同的场景,开发者可以根据需求选择合适的策略。
2239 5
|
消息中间件 存储 Kafka
【Kafka】Replica、Leader 和 Follower 三者的概念分析
【4月更文挑战第11天】【Kafka】Replica、Leader 和 Follower 三者的概念分析