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

  • 相关阅读:
    centos 7 安装mysql 8.0.30
    ScrollView的OnValueChanged
    React Context源码是怎么实现的呢
    cmd运行jar包,txt文件中文乱码问题
    多角度解读新兴公链Sui:团队、架构、代币、生态等
    Arweave/ceremic.network/cyberconnect.me/lens.dev介绍
    软件测试报告有哪些内容?
    四、vue-cli 介绍与使用
    苍穹外卖day8(1)地址簿功能
    类中报错 xxx does not name a type可能因为类中修改了对象
  • 原文地址:https://blog.csdn.net/weixin_64939936/article/details/132920400