• Springboot - 3.BeanFactory


    ✍1. BeanFactory

    Spring Boot中,BeanFactory是Spring Framework的核心接口之一,用于管理和维护应用程序中的Bean实例。它是Spring IoC容器的基础,负责创建、初始化、装配和管理Bean。在Spring Boot中,BeanFactory的实现主要是DefaultListableBeanFactory。以下是结合Spring Boot详细解释BeanFactory的重要概念和用法

    🎷1. BeanFactory接口:

    BeanFactory接口是Spring IoC容器的根接口,用于从容器中获取Bean。

    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    @Component
    public class MyComponent {
    
        private final BeanFactory beanFactory;
    
        @Autowired
        public MyComponent(BeanFactory beanFactory) {
            this.beanFactory = beanFactory;
        }
    
        public void useBean() {
            MyService myService = beanFactory.getBean(MyService.class);
            myService.doSomething();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    🎷2. 默认实现:

    在Spring Boot中,默认的BeanFactory实现是DefaultListableBeanFactory

    🎷3. Bean的生命周期:

    BeanFactory负责管理Bean的生命周期,如实例化、属性注入和初始化。这个过程会根据配置进行。

    🎷4. Bean的获取:

    使用getBean()方法从容器中获取Bean。

    MyService myService = beanFactory.getBean(MyService.class);
    
    • 1

    🎷5. 延迟初始化:

    默认情况下,BeanFactory支持延迟初始化,只有在需要时才创建Bean实例。

    🎷6. 扩展和自定义:

    您可以实现BeanPostProcessor接口来自定义Bean的创建和初始化过程。

    import org.springframework.beans.factory.config.BeanPostProcessor;
    import org.springframework.stereotype.Component;
    
    @Component
    public class MyBeanPostProcessor implements BeanPostProcessor {
        // Override methods for customizing bean initialization
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    🎷7. 注解配置:

    使用注解定义Bean。

    import org.springframework.stereotype.Component;
    
    @Component
    public class MyService {
        // Bean implementation
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    🎷8. 注解扫描:

    使用注解扫描自动注册标记了@Component的Bean。

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.ConfigurableApplicationContext;
    
    @SpringBootApplication
    public class Application {
    
        public static void main(String[] args) {
            ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
            MyService myService = context.getBean(MyService.class);
            myService.doSomething();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    🎷9. 自动装配:

    使用@Autowired注解进行自动装配。

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    @Component
    public class MyController {
    
        private final MyService myService;
    
        @Autowired
        public MyController(MyService myService) {
            this.myService = myService;
        }
    
        // Controller logic
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    ✍2. 如何使用BeanFactory:

    在Spring Boot中,BeanFactory是Spring IoC容器的根接口。它提供了配置框架和基本功能,如获取Bean的实例。

    🎷1. 注入BeanFactory:

    • 我们可以通过实现BeanFactoryAware接口或者直接在Bean中注入BeanFactory来使用它。

      @Component
      public class ExampleBean implements BeanFactoryAware {
      
          private BeanFactory beanFactory;
      
          @Override
          public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
              this.beanFactory = beanFactory;
          }
      
          public void doSomething() {
              // 使用beanFactory
              AnotherBean anotherBean = (AnotherBean) beanFactory.getBean(AnotherBean.class);
              anotherBean.doSomething();
          }
      
      }
      
      @Component
      public class AnotherBean {
      
          public void doSomething() {
              System.out.println("AnotherBean doSomething 方法被调用");
          }
      
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26

      在这个示例中,ExampleBean实现了BeanFactoryAware接口,这样Spring容器会自动注入BeanFactory。然后,在doSomething方法中,我们使用beanFactory获取AnotherBean的实例,并调用它的doSomething方法。

    🎷2. 使用@Autowired注解:

    • 我们可以使用@Autowired注解直接在Bean中注入BeanFactory

      @Component
      public class ExampleBean {
      
          @Autowired
          private BeanFactory beanFactory;
      
          public void doSomething() {
              // 使用beanFactory
              AnotherBean anotherBean = (AnotherBean) beanFactory.getBean(AnotherBean.class);
              anotherBean.doSomething();
          }
      
      }
      
      @Component
      public class AnotherBean {
      
          public void doSomething() {
              System.out.println("AnotherBean doSomething 方法被调用");
          }
      
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22

      在这个示例中,我们使用@Autowired注解直接在ExampleBean中注入BeanFactory。然后,在doSomething方法中,我们使用beanFactory获取AnotherBean的实例,并调用它的doSomething方法。

  • 相关阅读:
    外卖小程序系统:数字化时代餐饮业的技术奇迹
    隐式转换导致索引失效的原因
    python脚本实现全景站点欧拉角转矩阵
    2023年高校大数据实验室建设方案
    JavaWeb概念视频笔记
    改造delon库的reuse-tab标签使其关联隐藏动态菜单Menu及tab切换时参数不丢失方案
    仿游戏热血江湖游戏类31
    java字符串专项训练(手机号屏蔽)
    uniapp使用华为云OBS进行上传
    外包干了2个月,技术退步明显.......
  • 原文地址:https://blog.csdn.net/yueerba126/article/details/132652522