栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring IOC基于注解启动示例详析

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring IOC基于注解启动示例详析

Spring 基于注解启动

主要有两个Class实现注解启动

  • AnnotationConfigApplicationContext
  • AnnotationConfigWebApplicationContext

我们以AnnotationConfigApplicationContext 为研究对象


AnnotationConfigApplicationContext.png

引入Spring 最小依赖

 
  org.springframework
  spring-context
  ${spring.version}
 

编写器启动代码

 public static void main(String[] args) {
 AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
 applicationContext.register(BeanConfig.class);
 applicationContext.refresh();
 Date date = applicationContext.getBean("date",Date.class);
 System.out.println(date);
 }

AnnotationConfigApplicationContext 构造函数

 public AnnotationConfigApplicationContext() {
    //负责注册Class ,读取器
  this.reader = new AnnotatedBeanDefinitionReader(this);
    //负责扫描指定类路径下的Class,注册bean
  this.scanner = new ClassPathBeanDefinitionScanner(this);
 }

AnnotatedBeanDefinitionReader 构造方法

 public AnnotatedBeanDefinitionReader(BeanDefinitionRegistry registry) {
  this(registry, getOrCreateEnvironment(registry));
 }

 public AnnotatedBeanDefinitionReader(BeanDefinitionRegistry registry, Environment environment) {
  Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
  Assert.notNull(environment, "Environment must not be null");
  this.registry = registry;
    //初始化Conditionevaluator
  this.conditionevaluator = new Conditionevaluator(registry, environment, null);
     
  
  AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry);
 }

Conditionevaluator 这个对象干什么,点击进去

 public Conditionevaluator(@Nullable BeanDefinitionRegistry registry,
   @Nullable Environment environment, @Nullable ResourceLoader resourceLoader) {

  this.context = new ConditionContextImpl(registry, environment, resourceLoader); 
 }
  //ConditionContextImpl 实现了ConditionContext接口,Conditionevaluator静态内部类
  public ConditionContextImpl(@Nullable BeanDefinitionRegistry registry,
    @Nullable Environment environment, @Nullable ResourceLoader resourceLoader) {

   this.registry = registry;
   this.beanFactory = deduceBeanFactory(registry);
   this.environment = (environment != null ? environment : deduceEnvironment(registry));
   this.resourceLoader = (resourceLoader != null ? resourceLoader : deduceResourceLoader(registry));
   this.classLoader = deduceClassLoader(resourceLoader, this.beanFactory);
  }

可以知道Conditionevaluator使用外部传参的方法初始化了Spring容器顶级对象
BeanFactory,Environment,ResourceLoader,ClassLoader。在将这些传给ConditionContextImpl为接下来的解析@Conditional注解做好准备

ClassPathBeanDefinitionScanner构造函数

 public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry) {
  this(registry, true);
 }

 public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters) {
  this(registry, useDefaultFilters, getOrCreateEnvironment(registry));
 }

 public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters,
   Environment environment) {
  this(registry, useDefaultFilters, environment,
    (registry instanceof ResourceLoader ? (ResourceLoader) registry : null));
 }

 public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters,
   Environment environment, @Nullable ResourceLoader resourceLoader) {
  Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
  this.registry = registry;
  if (useDefaultFilters) {
   registerDefaultFilters();
  }
  setEnvironment(environment);
  setResourceLoader(resourceLoader);
 }

 protected void registerDefaultFilters() {
  this.includeFilters.add(new AnnotationTypeFilter(Component.class));
  ClassLoader cl = ClassPathScanningCandidateComponentProvider.class.getClassLoader();
  try {
   this.includeFilters.add(new AnnotationTypeFilter(
     ((Class) ClassUtils.forName("javax.annotation.ManagedBean", cl)), false));
   logger.debug("JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning");
  }
  catch (ClassNotFoundException ex) {
  }
  try {
   this.includeFilters.add(new AnnotationTypeFilter(
     ((Class) ClassUtils.forName("javax.inject.Named", cl)), false));
   logger.debug("JSR-330 'javax.inject.Named' annotation found and supported for component scanning");
  }
  catch (ClassNotFoundException ex) {
   // JSR-330 API not available - simply skip.
  }
 }

绕了地球几圈了,其实就是将Spring 顶级接口 Environment,ResourceLoader赋值,使用默认注解过滤器,首先将@Component加入List中,判断当前环境是否支持JSR-250,JSR-330,相应加入过滤器中。也就是这个扫描器默认只扫描@Component或者JSR-250,JSR-330的标记的Class。

applicationContext.register(BeanConfig.class)

 public void register(Class... annotatedClasses) {
  Assert.notEmpty(annotatedClasses, "At least one annotated class must be specified");
  this.reader.register(annotatedClasses); //调用 刚刚初始化读取器
 }
    |
============================AnnotatedBeanDefinitionReader 读取器代码======================================================================================================
  public void register(Class... annotatedClasses) {
  for (Class annotatedClass : annotatedClasses) {
   registerBean(annotatedClass);
  }
  }

 public void registerBean(Class annotatedClass) {
  doRegisterBean(annotatedClass, null, null, null);
 }
  
 
  void doRegisterBean(Class annotatedClass, @Nullable Supplier instanceSupplier, @Nullable String name,
   @Nullable Class[] qualifiers, BeanDefinitionCustomizer... definitionCustomizers) {

  //根据Class的Annotated 得出元数据 Annotationmetadata
  AnnotatedGenericBeanDefinition abd = new AnnotatedGenericBeanDefinition(annotatedClass);
  
  if (this.conditionevaluator.shouldSkip(abd.getmetadata())) { //如果 @Conditional条件不满足,不进行注册
   return;
  }

  abd.setInstanceSupplier(instanceSupplier);
  //解析Class是否有@Scope,解析@Scope注解返回Scopemetadata对象,没有直接返回空
  Scopemetadata scopemetadata = this.scopemetadataResolver.resolveScopemetadata(abd);
  abd.setScope(scopemetadata.getScopeName());
  //判断注解上Value是否有值,有就使用这个作为BeanName,没有则取类名 
  String beanName = (name != null ? name : this.beanNameGenerator.generateBeanName(abd, this.registry));
  //继续解析Annotationmetadata的@Lazy,@Primary,@DependsOn,@Role,@Description的注解,放入结果放入对象的属性中
  AnnotationConfigUtils.processCommonDefinitionAnnotations(abd);
  //这个类只是BeanDefinition 包装类
  BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(abd, beanName);
  //是否需要代理类,如果是则修改内部属性,重新生成BeanDefinition 对象
  definitionHolder = AnnotationConfigUtils.applyScopedProxyMode(scopemetadata, definitionHolder, this.registry);
  //调用DefaultListableBeanFactory.registerBeanDefinition的方法,做一些安全性校验再,将definitionHolder 放入register容器中
  BeanDefinitionReaderUtils.registerBeanDefinition(definitionHolder, this.registry);
 }

这个方法就是将注册的Bean,解析Class上的注解,初始化注解数据,做相应处理,转化成BeanDefinition ,放入Spring 容器中保存起来。

我们看下BeanDefinition是怎么实现注册到Spring的容器中,主要由DefaultListableBeanFactory.registerBeanDefinition来实现

public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)
   throws BeanDefinitionStoreException {

  Assert.hasText(beanName, "Bean name must not be empty");
  Assert.notNull(beanDefinition, "BeanDefinition must not be null");

  if (beanDefinition instanceof AbstractBeanDefinition) {
   try {
    //对beanDefinition 进行校验判断MethodOverrides不能为空,必须拥有工厂方法
    ((AbstractBeanDefinition) beanDefinition).validate();
   }
   catch (BeanDefinitionValidationException ex) {
    throw new BeanDefinitionStoreException(beanDefinition.getResourceDescription(), beanName,
      "Validation of bean definition failed", ex);
   }
  }

  BeanDefinition oldBeanDefinition;

  oldBeanDefinition = this.beanDefinitionMap.get(beanName); 
  if (oldBeanDefinition != null) {
    //这个方法是判断是否允许出现重名bean,并且是不同的定义bean,是否可以覆盖前者
   if (!isAllowBeanDefinitionOverriding()) {
    throw new BeanDefinitionStoreException(beanDefinition.getResourceDescription(), beanName,
      "Cannot register bean definition [" + beanDefinition + "] for bean '" + beanName +
      "': There is already [" + oldBeanDefinition + "] bound.");
   }
   else if (oldBeanDefinition.getRole() < beanDefinition.getRole()) {
    // e.g. was ROLE_APPLICATION, now overriding with ROLE_SUPPORT or ROLE_INFRASTRUCTURE
    if (this.logger.isWarnEnabled()) {
     this.logger.warn("Overriding user-defined bean definition for bean '" + beanName +
"' with a framework-generated bean definition: replacing [" +
oldBeanDefinition + "] with [" + beanDefinition + "]");
    }
   }
   else if (!beanDefinition.equals(oldBeanDefinition)) {
    if (this.logger.isInfoEnabled()) {
     this.logger.info("Overriding bean definition for bean '" + beanName +
"' with a different definition: replacing [" + oldBeanDefinition +
"] with [" + beanDefinition + "]");
    }
   }
   else {
    if (this.logger.isDebugEnabled()) {
     this.logger.debug("Overriding bean definition for bean '" + beanName +
"' with an equivalent definition: replacing [" + oldBeanDefinition +
"] with [" + beanDefinition + "]");
    }
   }
   this.beanDefinitionMap.put(beanName, beanDefinition);
  }
  else {
   //调用alreadyCreated.isEmpty(),alreadyCreated Set对象,保存已经创建beanName
   //文档中表示created,跟这里注册应该不是同一个行为,这个要看到后面才知道什么意思
   if (hasBeanCreationStarted()) {
    synchronized (this.beanDefinitionMap) {//更新数据
     this.beanDefinitionMap.put(beanName, beanDefinition);
     List updatedDefinitions = new ArrayList<>(this.beanDefinitionNames.size() + 1);
     updatedDefinitions.addAll(this.beanDefinitionNames);
     updatedDefinitions.add(beanName);
     this.beanDefinitionNames = updatedDefinitions;
     if (this.manualSingletonNames.contains(beanName)) {
      Set updatedSingletons = new linkedHashSet<>(this.manualSingletonNames);
      updatedSingletons.remove(beanName);
      this.manualSingletonNames = updatedSingletons;
     }
    }
   }
   else { 
    //Spring beanDefinition 容器,一个Map转载
    this.beanDefinitionMap.put(beanName, beanDefinition);
    //保存beanName,主要用于记录每个bean注册顺序
    this.beanDefinitionNames.add(beanName);
    //删除单例,注册成一个普通bean
    this.manualSingletonNames.remove(beanName);
   }
   this.frozenBeanDefinitionNames = null;
  }

  if (oldBeanDefinition != null || containsSingleton(beanName)) { 
   //更新Spring容器里beanName
   resetBeanDefinition(beanName);
  }
 }

将beanDefinition注册到Spring容器中,并没有太多复杂的逻辑,只是做一些安全性的检查。

BeanDefinition

一个BeanDefinition描述了一个bean的实例,包括属性值,构造方法参数值和继承自它的类的更多信息。BeanDefinition仅仅是一个最简单的接口,主要功能是允许BeanFactoryPostProcessor 例如PropertyPlaceHolderConfigure 能够检索并修改属性值和别的bean的元数据(译注)

Spring 容器beanDefinition主要分为RootBeanDefinition,AnnotatedGenericBeanDefinition这两种

  • RootBeanDefinition     Spring Factory中的特定bean
  • AnnotatedGenericBeanDefinition      用户自定义bean

Spring 启动流程总结

AnnotationConfigApplicationContext 初始化.png

这些BeanDefinition只是放入到Spirng 容器中,并没有进行任何初始化对象的操作,真正的IOC操作都在refresh(),这个方法有空再进行分析。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对考高分网的支持。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/138897.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号