进阶Spring(2)-BeanFactory和ApplicationContext实现

这篇具有很好参考价值的文章主要介绍了进阶Spring(2)-BeanFactory和ApplicationContext实现。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

🏠个人主页:阿杰的博客
💪个人简介:大家好,我是阿杰,一个正在努力让自己变得更好的男人👨
目前状况🎉:24届毕业生,奋斗在找实习的路上🌟
🚗🚗为了让更多的人看到更优质的博客,阿杰正在努力的更新学习中心中的内容。


第二讲:BeanFactory和ApplicationContext实现

首先看代码

ConfigurableApplicationContext context = SpringApplication.run(BeanfactoryimpApplication.class, args);

ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
//class org.springframework.beans.factory.support.DefaultListableBeanFactory
System.out.println(beanFactory.getClass());

spring底层创建实体类就是依赖于DefaultListableBeanFactory。他是BeanFactory接口的一个主要实现类。

先创建类

package com.ajie.beanfactoryimp.newbean;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author ajie
 * @version 1.0
 * @date 2023/5/2 15:56
 */
@Slf4j
public class Bean1 {
    public Bean1(){
        log.debug("构造 Bean1");
    }
    @Autowired
    private Bean2 bean2;
    public Bean2 getBean2(){
        return  bean2;
    }
}
package com.ajie.beanfactoryimp.newbean;

import lombok.extern.slf4j.Slf4j;

/**
 * @author ajie
 * @version 1.0
 * @date 2023/5/2 15:56
 */
@Slf4j
public class Bean2 {

    public Bean2(){
        log.debug("构造 Bean2");
    }
}
package com.ajie.beanfactoryimp.newbean;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author ajie
 * @version 1.0
 * @date 2023/5/2 15:55
 */
@Configuration
public class Config {
    @Bean
    public Bean1 bean1(){
        return  new Bean1();
    }


    @Bean
    public Bean2 bean2(){
        return  new Bean2();
    }
}

开始创建测试类

package com.ajie.beanfactoryimp.newbean;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.annotation.AnnotationConfigUtils;

/**
 * @author ajie
 * @version 1.0
 * @date 2023/5/2 15:52
 */
public class TestBeanFactory {
    public static void main(String[] args) {
        //spring底层创建实体类依赖这个类 DefaultListableBeanFactory
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        //定义名字为config的bean
        AbstractBeanDefinition beanDefinition =
                BeanDefinitionBuilder.genericBeanDefinition(Config.class).setScope("singleton").getBeanDefinition();
        beanFactory.registerBeanDefinition("config",beanDefinition);

        //给BeanFactory 添加一些常用的后处理器
        AnnotationConfigUtils.registerAnnotationConfigProcessors(beanFactory);

        //BeanFactory的运行后处理器 可以解析@Configuration @Bean
        beanFactory.getBeansOfType(BeanFactoryPostProcessor.class).values().forEach(beanFactoryPostProcessor -> {
            beanFactoryPostProcessor.postProcessBeanFactory(beanFactory);
        });

        for (String name : beanFactory.getBeanDefinitionNames()) {
            System.out.println(name);
        }

    }
}

可以看到@Bean被扫描 bean1 和bean2 被创建 @Configuration是用于spring类扫描的时候用的,加了这个注解的类被扫描到了就会被放进Bean工厂

进阶Spring(2)-BeanFactory和ApplicationContext实现

在加上这句代码

System.out.println(beanFactory.getBean(Bean1.class).getBean2());

会看到

进阶Spring(2)-BeanFactory和ApplicationContext实现

为什么呢? 因为beanFactory并没有解析@Autowired, @Resource…

加上

beanFactory.getBeansOfType(BeanPostProcessor.class).values().forEach(beanFactory::addBeanPostProcessor);

bean2就被创建出来了

进阶Spring(2)-BeanFactory和ApplicationContext实现

**注意: **

BeanFactory中的对象都是懒加载的,如果不去调用get()方法获取的话,就不会初始化,如果想要让对象在get()之前就创建好,需要调用beanFactory.preInstantiateSingletons()方法。

总结:

beanFactory 不会做的事

  • 不会主动调用BeanFactory的后处理器
  • 不会主动添加Bean的后处理器
  • 不会主动初始化单例
  • 不会解析BeanFactory,还不会解析 ${}, #{}

下面咱们在添加一点类

先定义一个接口Inter,再定义两个Bean,名称分别为Bean3和Bean4,都继承Inter,接着在Config中通过@Bean注解将Bean3和Bean4都加进Bean工厂中,然后在Bean1中定义一个Inter对象,通过@Autowired注解将实现类注入进来。

package com.ajie.beanfactoryimp.newbean;

/**
 * @author ajie
 * @version 1.0
 * @date 2023/5/2 23:07
 */
public interface Inter {
}

package com.ajie.beanfactoryimp.newbean;

import lombok.extern.slf4j.Slf4j;

/**
 * @author ajie
 * @version 1.0
 * @date 2023/5/2 23:08
 */
@Slf4j
public class Bean4 implements Inter{
    public Bean4() {
        log.debug("构造 bean4 ");
    }
}
package com.ajie.beanfactoryimp.newbean;

import lombok.extern.slf4j.Slf4j;

/**
 * @author ajie
 * @version 1.0
 * @date 2023/5/2 23:08
 */
@Slf4j
public class Bean3 implements Inter{
    public Bean3() {
        log.debug("构造 bean3 ");
    }
}

package com.ajie.beanfactoryimp.newbean;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author ajie
 * @version 1.0
 * @date 2023/5/2 15:55
 */
@Configuration
public class Config {
    @Bean
    public Bean1 bean1(){
        return  new Bean1();
    }
    
    @Bean
    public Bean2 bean2(){
        return  new Bean2();
    }
    
    @Bean
    public Bean3 bean3(){
        return  new Bean3();
    }
    @Bean
    public Bean4 bean4(){
        return  new Bean4();
    }
}

bean1 中添加

@Autowired
private Inter inter;
public Inter getInter(){
    return inter;
}

这是因为@Autowired 注解会先根据名字来匹配 没有的话 再根据类型来匹配 但是bean3和bean4都实现了inter所以会出现错误

进阶Spring(2)-BeanFactory和ApplicationContext实现

只需要吧inter改成 bean3就可以避免错误

如果我们在加上 @Resource(name=“bean4”)

这个时候他注册的是那个?

进阶Spring(2)-BeanFactory和ApplicationContext实现

运行后发现注册的是 bean3 ;

在beanFactory 加载后处理器的时候处理@Autowired注解的处理器internalAutowiredAnnotationProcessor比internalCommonAnnotationProcessor先进入,所以@Autowired会先被解析

进阶Spring(2)-BeanFactory和ApplicationContext实现

进阶Spring(2)-BeanFactory和ApplicationContext实现文章来源地址https://www.toymoban.com/news/detail-447255.html

到了这里,关于进阶Spring(2)-BeanFactory和ApplicationContext实现的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Spring之BeanFactory与ApplicationContext区别、实例化Bean的三种⽅式、延迟加载(lazy-Init )

    BeanFactory是Spring框架中IoC容器的顶层接⼝,它只是⽤来定义⼀些基础功能,定义⼀些基础规范,⽽ApplicationContext是它的⼀个⼦接⼝,所以ApplicationContext是具备BeanFactory提供的全部功能的。 通常,我们称BeanFactory为SpringIOC的基础容器, ApplicationContext是容器的⾼级接⼝,⽐BeanFactory要拥

    2024年02月11日
    浏览(29)
  • BeanFactory与ApplicationContext基本介绍

    接口 定义能力, 抽象类 实现接口的一些重要方法,最后 实现类 可以实现自己的一些逻辑 仅仅是一个接口,Spring 的核心容器,并不是IOC容器的具体实现,它的一些具体实现类才是 BeanFactory 是 ApplicationContext 的父接口 BeanFactory才是 Spring 的核心容器, 主要的 ApplicationContext 实现

    2024年02月13日
    浏览(24)
  • BeanFactory和ApplicationContext的区别

    BeanFactory与ApplicationContext的关系 BeanFactory是Spring的早期接口,称为 Spring的Bean工厂 ,ApplicationContext是后期更高级接口,称之为 Spring容器 ; ApplicationContext在BeanFactory基础上对功能进行了扩展,例如: 监听功能、国际化功能 等。 BeanFactory 的API更 偏向底层 , ApplicationContext 的A

    2024年02月06日
    浏览(24)
  • 第一讲:BeanFactory和ApplicationContext接口

    BeanFactory是ApplicationContext的父接口,是真正的Spring核心容器,主要的ApplicationContext实现都【组合】了他的功能。 首先先看一下BeanFactory的接口定义: 表面上只有getBean功能,实际上控制反转、基本的依赖注入、直至Bean的生命周期的各种功能,都由他的实现类提供, 例如:Defau

    2024年02月12日
    浏览(25)
  • BeanFactory和ApplicationContext区别及详解

    ​ Spring 框架带有两个 IOC 容器—— BeanFactory 和 ApplicationContext 。 BeanFactory 是 IOC 容器的最基本版本, ApplicationContext 扩展了 BeanFactory 的特性。 ​ Spring容器最基本的接口就是BeanFactory。BeanFactory负责配置、创建、管理Bean,它有一个子接口ApplicationContext,也被称为Spring上下文,

    2023年04月10日
    浏览(24)
  • 解锁ApplicationContext vs BeanFactory: 谁更具选择性?

    目录 一、聚焦源码回顾 (一)源码分析和理解 (二)简短的回顾对比建议 二、ApplicationContext vs BeanFactory特性对比 (一)主要特性总结 (二)直接建议 三、案例简单说明 (一)加载少量的 Bean的案例 (二)简单的命令行工具:用于读取配置文件并生成报告 (三)启动时加

    2024年04月25日
    浏览(24)
  • Spring源码-浅识BeanFactory

    在SpringBoot出现之前,我们使用Spring需要以配置文件的方式进行启动.如果使用XML文件配置.则通过 XmlWebApplicationContext.java 进行启动.常应用在Web项目的开发中. 以此为例,通过阅读源码发现和 XmlWebApplicationContext.java \\\"平级\\\"的类如下所示 由此我们可以知道 ApplicationContext 遵循 Applicat

    2024年02月03日
    浏览(27)
  • 深度解析 Spring 源码:揭秘BeanFactory 之谜

    1.1 BeanFactory的概述 BeanFactory是Spring框架中的一个核心接口,它提供了一种灵活的方式来管理Bean,并实现了IoC(控制反转)和DI(依赖注入)等特性,为应用程序提供了灵活、可扩展的对象管理和配置机制。 BeanFactory的特性 : IoC容器: BeanFactory是Spring的IoC容器之一。IoC是一种

    2024年04月26日
    浏览(26)
  • 【Spring源码】 BeanFactory和FactoryBean是什么?

    面试官:“看过Spring源码吧,简单说说Spring中BeanFactory和FactoryBean的区别是什么?” 大神仙:“BeanFactory是bean工厂,FactoryBean是工厂bean”。 这么回答,等于面试官问你Spring是什么,你回答这个单词翻译叫春天。 首先看下C知道(ChitGPT)的回答 没错,基本上已经给出了答案。

    2023年04月19日
    浏览(27)
  • Spring源码笔记之SpringIOC--(3)什么是BeanFactory?

    什么是BeanFactory? BeanFactory 是SpringIOC的最顶层接口,涵盖了IOC容器最基本的操作。 ListableBeanFactory 、 ConfigurableBeanFactory 提供了IOC容器获取所有Bean、配置Bean的额外能力。所有 BeanFactory 的实现类持有所有Bean的定义 BeanDefinition ,用一个唯一的字符串(即Bean的名字)区分。 Bean

    2024年02月20日
    浏览(26)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包