Spring-ApplicationContext

简介: Spring-ApplicationContext

一、ApplicationContext


  ApplicationContext到底是什么?字面含义是应用的上下文。这块我们需要看看ApplicationContext的具体的结构。

image.png

  通过ApplicationContext实现的相关接口来分析,ApplicationContext接口在具备BeanFactory的功能的基础上还扩展了 应用事件发布,资源加载,环境参数和 国际化的能力。然后我们来看看ApplicationContext接口的实现类的情况。

image.png

  在ApplicationContext的实现类中有两个比较重要的分支 AbstractRefreshableApplicationContext和 GenericApplicationContext.

image.png



二、BeanFactory


  上面分析了 ApplicationContext接口的结构。然后我们来看看 BeanFactory在ApplicationContext中具体的实现是怎么样的

image.png

image.png


可以看到具体的实现是 DefaultListableBeanFactory .然后我们来看看他的体系结构

image.png


BeanFactory的继承体系

image.png




三、BeanDefinition


  然后我们来了解下ApplicationContext是如何来加载Bean定义的。具体代码我们需要分为XML配置文件和基于注解的两种方式来看。



1.基于XML方式


  我们先定义对应的application.xml文件


<?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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
  <bean id="beanE" class="com.study.spring.sample.config.BeanE" />
  <bean id="beanF" class="com.study.spring.sample.config.BeanF" ></bean>
  <context:annotation-config/>
  <context:component-scan base-package="com.study.spring.sample.config" ></context:component-scan>
</beans>
复制代码


然后我们的测试类代码


public class XMLConfigMain {
  public static void main(String[] args) {
    ApplicationContext context = new GenericXmlApplicationContext(
        "classpath:com/study/spring/sample/config/application.xml");
    BeanF bf = context.getBean(BeanF.class);
    bf.do1();
  }
}
复制代码


处理的过程  解析XML --> BeanDefinition --> BeanDefinitionRegistry --> BeanFactory


image.png

2.基于注解方式


  然后来看看基于注解方式的使用的情况。首先是我们的配置类


@Configuration
@ComponentScan("com.study.spring.sample.config")
public class JavaBasedMain {
  @Bean
  public BeanH getBeanH() {
    return new BeanH();
  }
  public static void main(String[] args) {
    ApplicationContext context = new AnnotationConfigApplicationContext(JavaBasedMain.class);
    BeanH bh = context.getBean(BeanH.class);
    bh.doH();
  }
}
复制代码


然后是我们的测试类


public class AnnotationMain {
  public static void main(String[] args) {
    ApplicationContext context = new AnnotationConfigApplicationContext("com.study.spring.sample.config");
    BeanG bg = context.getBean(BeanG.class);
    bg.dog();
  }
}
复制代码


注解使用有两种方法:


  1. 配置扫描路径


  1. 配置@Configuration的注解类



2.1 this构造方法


  在this的构造方法中会完成相关的配置处理。


public AnnotationConfigApplicationContext() {
    this.reader = new AnnotatedBeanDefinitionReader(this);
    this.scanner = new ClassPathBeanDefinitionScanner(this);
  }
复制代码


首先是AnnotatedBeanDefinitionReader(this)方法。会完成核心的

ConfigurationClassPostProcessor的注入。ConfigurationClassPostProcessor 会完成@Configuration相关的注解的解析

image.png


this.scanner其实就是创建了一个对应的扫描器

image.png



2.2 扫描实现


  扫描就需要进入到scan方法中。

image.png

image.png

image.png

完成相关的注册

image.png


2.3 @Configuration


  @Configuration的解析其实是在refresh方法中来实现的。


image.png


3.小结


  通过上面的分析其实我们已经对Bean定义的扫描,解析和注册过程有了一定的了解。归纳为:


  1. reader解析XML,完成xml方法配置的bean定义


  1. scanner扫描指定包下的类,找出带有@Component注解的类,注册成Bean定义


  1. 通过ConfigurationClassPostProcessor对带有@Configuration注解的类进行处理,解析它上面的注解,以及类中带有@Bean 注解,加入这些的Bean的定义。



4.BeanDefinition


  ;然后我们来看看BeanDefinition的继承结构


image.png

  继承属性访问器和元数据接口,增加了Bean定义操作,实现了数据和操作解耦。属性访问器和元数据接口接着往下看。


4.1 BeanMetadataElement


  BeanMetadataElement提供了获取数据源的方式,也就是可以指导Bean是来自哪个类。


public interface BeanMetadataElement {
  /**
   * Return the configuration source {@code Object} for this metadata element
   * (may be {@code null}).
   */
  @Nullable
  default Object getSource() {
    return null;
  }
}
复制代码


4.2 BeanMetadataAttribute元数据属性


  实现了元数据接口,增加了属性的名字和值。。


public class BeanMetadataAttribute implements BeanMetadataElement {
  private final String name;
  @Nullable
  private final Object value;
  @Nullable
  private Object source;
}
复制代码


4.3 AttributeAccessor属性访问器


  AttributeAccessor用来给Bean定义了增删改查属性的功能


public interface AttributeAccessor {
  /**
   * Set the attribute defined by {@code name} to the supplied {@code value}.
   * If {@code value} is {@code null}, the attribute is {@link #removeAttribute removed}.
   * <p>In general, users should take care to prevent overlaps with other
   * metadata attributes by using fully-qualified names, perhaps using
   * class or package names as prefix.
   * @param name the unique attribute key
   * @param value the attribute value to be attached
   */
  void setAttribute(String name, @Nullable Object value);
  /**
   * Get the value of the attribute identified by {@code name}.
   * Return {@code null} if the attribute doesn't exist.
   * @param name the unique attribute key
   * @return the current value of the attribute, if any
   */
  @Nullable
  Object getAttribute(String name);
  /**
   * Remove the attribute identified by {@code name} and return its value.
   * Return {@code null} if no attribute under {@code name} is found.
   * @param name the unique attribute key
   * @return the last value of the attribute, if any
   */
  @Nullable
  Object removeAttribute(String name);
  /**
   * Return {@code true} if the attribute identified by {@code name} exists.
   * Otherwise return {@code false}.
   * @param name the unique attribute key
   */
  boolean hasAttribute(String name);
  /**
   * Return the names of all attributes.
   */
  String[] attributeNames();
}
复制代码


4.4 AttributeAccessorSupport属性访问抽象实现类


  内部定义了1个map来存放属性。


public abstract class AttributeAccessorSupport implements AttributeAccessor, Serializable {
  /** Map with String keys and Object values. */
  private final Map<String, Object> attributes = new LinkedHashMap<>();
  @Override
  public void setAttribute(String name, @Nullable Object value) {
    Assert.notNull(name, "Name must not be null");
    if (value != null) {
      this.attributes.put(name, value);
    }
    else {
      removeAttribute(name);
    }
  }
    //  ......
}
复制代码


4.5 BeanMetadataAttributeAccessor元数据属性访问器


  继承AttributeAccessorSupport具备属性访问功能,实现BeanMetadataElement具备获取元数据功能。 **AbstractBeanDefinition就继承于它,使得同时具有属性访问和元数据访问的功能 **。

image.png


结合AbstractBeanDefinition.来看看他们的类图结构

image.png

5. BeanDefinition继承体系


5.1 AnnotatedBeanDefinition


  增加了2个方法,获取bean所在类的注解元数据和工厂方法元数据,这些数据在进行解析处理的时候需要用到。


public interface AnnotatedBeanDefinition extends BeanDefinition {
  /**
   * Obtain the annotation metadata (as well as basic class metadata)
   * for this bean definition's bean class.
   * @return the annotation metadata object (never {@code null})
   */
  AnnotationMetadata getMetadata();
  /**
   * Obtain metadata for this bean definition's factory method, if any.
   * @return the factory method metadata, or {@code null} if none
   * @since 4.1.1
   */
  @Nullable
  MethodMetadata getFactoryMethodMetadata();
}
复制代码


  该注解有三个具体的实现。ScannedGenericBeanDefinition、

AnnotatedGenericBeanDefinition、ConfigurationClassBeanDefinition。


image.png

5.2 AbstractBeanDefinition模板类


  AbstractBeanDefinition我们可以称之为BeanDefinition的模板类。结构我们上面其实有梳理


image.png

  通过上面我们可以看到AbstractBeanDefinition 具备了 Bean元数据的获取和属性相关的操作。同时AbstractBeanDefinition的继承结构

image.png

5.3 RootBeanDefinition根bean定义


  它主要用在spring内部的bean定义、把不同类型的bean定义合并成RootBeanDefinition(getMergedLocalBeanDefinition方法)。没有实现BeanDefinition接口的设置获取父bean定义方法,不支持设置父子beanDefinition。



5.4 ConfigurationClassBeanDefinition


  用作ConfigurationClassPostProcessor解析过程中封装配置类的bean定义。


5.5 GenericBeanDefinition


  GenericBeanDefinition通用Bean的定义。


5.6 ScannedGenericBeanDefinition


  @ComponentScan扫描的bean定义使用。



5.7 AnnotatedGenericBeanDefinition



相关文章
|
数据可视化 定位技术
three.js实现烟雾缭绕效果
前言 大家好!我是Fly哥,最近接广告的接的有点多, 感谢大家还是一如既往的支持我!respect, 前几天我在朋友圈分享了一个烟雾缭绕的效果。很多小伙伴都表示非常感兴趣,有的同学说用到了噪声, 有的同学说用到了着色器,还有更过分说用到了ps, 胖虎竟然无语凝噎。其实都是就是简单的贴图。配合一点想象力。我们先看下效果: 然后我就发了一条朋友圈,问这个像什么?? 有的说 云层, 有的说云墨,有的说雾霭, 其实都不是, 我想做的是烟雾。好的话不不多说!, 本篇文章阅读大概5分钟。不耽误大家太多时间,主要是介绍思路, 说太多也没啥意义。如果你对three.js 还没有一点了解都没有,
three.js实现烟雾缭绕效果
|
存储 人工智能 监控
从代码生成到自主决策:打造一个Coding驱动的“自我编程”Agent
本文介绍了一种基于LLM的“自我编程”Agent系统,通过代码驱动实现复杂逻辑。该Agent以Python为执行引擎,结合Py4j实现Java与Python交互,支持多工具调用、记忆分层与上下文工程,具备感知、认知、表达、自我评估等能力模块,目标是打造可进化的“1.5线”智能助手。
3036 62
|
负载均衡 监控 算法
负载均衡的方法有哪些?
负载均衡的方法有哪些?
1072 59
|
XML Java 数据格式
探索Spring之利剑:ApplicationContext接口
本文深入介绍了Spring框架中的核心接口ApplicationContext,解释了其作为应用容器的功能,包括事件发布、国际化支持等,并通过基于XML和注解的配置示例展示了如何使用ApplicationContext管理Bean实例。
|
微服务 设计模式 测试技术
深入理解 DDD(领域驱动设计)思想
领域驱动设计(DDD)是一种以业务为核心的软件开发方法,通过限界上下文、聚合、实体、值对象等模型,分离业务与技术复杂性,提升系统可维护性与扩展性,尤其适用于复杂业务系统的架构设计。
2747 7
|
存储 设计模式 NoSQL
ddd领域驱动设计
领域驱动设计(DDD)是一种应对复杂软件系统的思维革命与系统方法。它通过“通用语言”统一团队认知,运用“限界上下文”划分业务边界,结合“聚合”“领域事件”等战术模式,精准构建业务模型。DDD不仅提升软件对业务的映射能力,更为微服务架构提供科学的边界划分依据,是打造高内聚、低耦合系统的核心方法论。
|
算法 定位技术 C++
A* 算法详解(超级详细讲解,附有大图)
A* 算法详解(超级详细讲解,附有大图)
10602 0
|
存储 Python 内存技术
python WAV音频文件处理—— (1)读写WAV文件
python WAV音频文件处理—— (1)读写WAV文件
881 14
|
C++
VS2013密钥(所有版本)
Visual Studio Ultimate 2013 KEY(密钥):BWG7X-J98B3-W34RT-33B3R-JVYW9 Visual Studio Premium 2013 KEY(密钥):FBJVC-3CMTX-D8DVP-RTQCT-92494 Visual Studio Pro...
8877 0
|
SQL Java 数据库连接
PageHelper分页插件的使用
PageHelper分页插件的使用
868 0