Home > Java > javaTutorial > body text

How does SpringBoot choose to load its own beans first?

WBOY
Release: 2023-05-12 11:01:14
forward
1433 people have browsed it

    Implementation method

    DependsOn annotation

    There are many ways to implement @DependsOn online. If the number of dependent beans is small, it is easier to do. , but when the number increases, it becomes more troublesome. Each class needs to be rewritten, so the second method is recommended.

    ApplicationContextInitializer

    After registering ApplicationContextInitializer, you can register BeanDefinitionRegistryPostProcessor in Spring. Finally, implement BeanDefinitionRegistryPostProcessor to register the target bean.

     class DemoApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
      @Override
      public void initialize(ConfigurableApplicationContext applicationContext) {
          applicationContext.addBeanFactoryPostProcessor(new DemoBeanDefinitionRegistryPostProcessor());
      }
    }
    Copy after login

    Implementing BeanDefinitionRegistryPostProcessor:

    public class DemoBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor, PriorityOrdered {
      // from BeanDefinitionRegistryPostProcessor interface
      @Override
      public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
    // 重点在这里,这里可以把自己的 想要提起 实现的 或者初始化的 bean  加到这里
    beanDefinitionRegistry.registerBeanDefinition("DemoService",new RootBeanDefinition(DemoService.class));
      }
      // from BeanDefinitionRegistryPostProcessor interface
      @Override
      public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
      }
    // from PriorityOrdered  interface
      @Override
      public int getOrder() {
      // 排在 ConfigurationClassPostProcessor 之前即可
          return Ordered.LOWEST_PRECEDENCE - 1;
      }
    }
    Copy after login

    It should be noted that @Componet or other annotations cannot be used to register BeanDefinitionRegistryPostProcessor.

    Because the @Componet annotation method can be registered only if it is scanned by ConfigurationClassPostProcessor. Now, we need to consider our bean registration before these beans, so it must not be managed by "it-ConfigurationClassPostProcessor". Thinking about it from another angle, if the beans registered by "it" management class must not be ranked in front of ConfigurationClassPostProcessor.

    Note: @Order can only control the order of spring's own beans. It cannot control the annotations @Service @Component and @Repository.

    Simple Demo

    Requirements: The author wants TestService to be registered in advance, and only after execution can other beans be registered.

    public class TestService {
        // 存放系统配置
        private static Map<String, String> GLOBAL_CONF = new HashMap<>();
        @PostConstruct
        public void init() {
            // 先做初始化 GLOBAL_CONF 或者其他 IO操作
            // GLOBAL_CONF.put(key, value);
        }
        //其他 bean 通过这个方法获得系统配置
        public static String getGlobalConfig(String key) {
            return GLOBAL_CONF.get(key);
        }
    }
    Copy after login

    Implementation: (For simplicity, directly implement the above three interfaces)

    public class DemoBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor, 
    PriorityOrdered, 
    ApplicationContextInitializer<ConfigurableApplicationContext> {
        /**
         *  第二步: 注册 自己的 bean
         *
         * @param beanDefinitionRegistry
         */
        // from BeanDefinitionRegistryPostProcessor interface
        @Override
        public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
            beanDefinitionRegistry.registerBeanDefinition("TestService",new RootBeanDefinition(TestService.class));
        }
        // from BeanDefinitionRegistryPostProcessor interface
        @Override
        public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
        }
    // from PriorityOrdered  interface
        @Override
        public int getOrder() {
            return Ordered.LOWEST_PRECEDENCE - 1;
        }
        /**
         *  第一步 先注册 到 configurableApplicationContext 中
         *
         * @param configurableApplicationContext
         */
        // from ApplicationContextInitializer  interface
        @Override
        public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
            configurableApplicationContext.addBeanFactoryPostProcessor(new DemoBeanDefinitionRegistryPostProcessor());
        }
    }
    Copy after login

    The above is the detailed content of How does SpringBoot choose to load its own beans first?. For more information, please follow other related articles on the PHP Chinese website!

    Related labels:
    source:yisu.com
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template