• BeanFactory实现特点


    beanFactory不会做的事情

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

    bean后处理器会有排序的逻辑,后处理都实现order接口实现的,有的是解析@Autowired注解有的是解析@Resource注解

    1. package com.example;
    2. import org.slf4j.LoggerFactory;
    3. import org.springframework.beans.factory.annotation.Autowired;
    4. import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
    5. import org.springframework.beans.factory.config.BeanPostProcessor;
    6. import org.springframework.beans.factory.support.AbstractBeanDefinition;
    7. import org.springframework.beans.factory.support.BeanDefinitionBuilder;
    8. import org.springframework.beans.factory.support.DefaultListableBeanFactory;
    9. import org.springframework.context.annotation.AnnotationConfigUtils;
    10. import org.springframework.context.annotation.Bean;
    11. import org.springframework.context.annotation.Configuration;
    12. import java.util.logging.Logger;
    13. public class TestBeanFactory {
    14. public static void main(String[] args) {
    15. DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
    16. // 为beanFactory添加一些bean的定义,beanFactory根据添加的bean的定义来传创建bean对象
    17. // 定义包括:bean的类型(class)、bean的作用域(scope)、bean是否含有初始化、销毁
    18. AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition(Config.class).setScope("singleton").getBeanDefinition();
    19. beanFactory.registerBeanDefinition("config", beanDefinition);
    20. for (String name : beanFactory.getBeanDefinitionNames()) {
    21. System.out.println(name); // config todo 目前beanFactory还缺少解析注解的功能
    22. }
    23. // 给beanFactory添加一些常用的后处理器
    24. AnnotationConfigUtils.registerAnnotationConfigProcessors(beanFactory);
    25. for (String name : beanFactory.getBeanDefinitionNames()) {
    26. System.out.println(name); /*config
    27. org.springframework.context.annotation.internalConfigurationAnnotationProcessor
    28. org.springframework.context.annotation.internalAutowiredAnnotationProcessor
    29. org.springframework.context.annotation.internalCommonAnnotationProcessor
    30. org.springframework.context.event.internalEventListenerProcessor
    31. org.springframework.context.event.internalEventListenerFactory*/
    32. }
    33. // 获取beanFactory后处理器,补充了一些bean的定义
    34. beanFactory.getBeansOfType(BeanFactoryPostProcessor.class).values().stream().forEach(beanFactoryPostProcessor -> {
    35. beanFactoryPostProcessor.postProcessBeanFactory(beanFactory);
    36. });
    37. // bean的后处理器,针对bean的生命周期的各个阶段来拓展一些功能,例如@Autowired、@Resource....
    38. beanFactory.getBeansOfType(BeanPostProcessor.class).values().stream().forEach(beanFactory::addBeanPostProcessor);
    39. for (String name : beanFactory.getBeanDefinitionNames()) {
    40. System.out.println(name);
    41. }/*config
    42. org.springframework.context.annotation.internalConfigurationAnnotationProcessor
    43. org.springframework.context.annotation.internalAutowiredAnnotationProcessor
    44. org.springframework.context.annotation.internalCommonAnnotationProcessor
    45. org.springframework.context.event.internalEventListenerProcessor
    46. org.springframework.context.event.internalEventListenerFactory
    47. bean1
    48. bean2*/
    49. beanFactory.preInstantiateSingletons();// 准备好所有单例。初始化单例对象
    50. System.out.println("+++++++++++++++++++++++++++++++++++");
    51. System.out.println(beanFactory.getBean(Bean1.class).getBean2());
    52. }
    53. @Configuration
    54. static class Config {
    55. @Bean
    56. public Bean1 bean1() {
    57. return new Bean1();
    58. }
    59. @Bean
    60. public Bean2 bean2() {
    61. return new Bean2();
    62. }
    63. }
    64. static class Bean1 {
    65. private static final org.slf4j.Logger log = LoggerFactory.getLogger(Bean1.class);
    66. public Bean1() {
    67. log.info("构造Bean1()");
    68. }
    69. @Autowired
    70. private Bean2 bean2;
    71. public Bean2 getBean2() {
    72. return bean2;
    73. }
    74. }
    75. static class Bean2 {
    76. private static final org.slf4j.Logger log = LoggerFactory.getLogger(Bean2.class);
    77. public Bean2() {
    78. log.info("构造Bean2()");
    79. }
    80. }
    81. }

  • 相关阅读:
    2022年中国儿童食品市场规模与发展趋势
    【2603. 收集树中金币】
    java+ssm+mysql高校图书管理系统
    [题解] Codeforces Global Round 22 1738 A B C D E F 题解
    java计算机毕业设计医院预约挂号系统源码+系统+mysql数据库+lw文档
    码蹄集 - MT3435 · 赋值 - 二分图问题 - 图文讲解
    你想知道的ArrayList知识都在这
    Android 10.0 Launcher3桌面显示多个相同app图标的解决办法
    数据结构:树形数据结构
    element 表格fixed列高度无法100%
  • 原文地址:https://blog.csdn.net/weixin_64939936/article/details/132920400