Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-7868

BeanDefinitionRegistryPostProcessor registered via @Bean will not be invoked

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Minor
    • Resolution: Won't Fix
    • Affects Version/s: 3.0.5
    • Fix Version/s: None
    • Component/s: Core
    • Labels:
      None
    • Last commented by a User:
      true

      Description

      I created simple bean implementing BeanDefinitionRegistryPostProcessor. All I want to do is to register some bean definition in the BeanDefinitionRegistryPostProcessor#postProcessBeanDefinitionRegistry method. The problem is that code working with ClassPathXmlApplicationContext doesn't work with AnnotationConfigApplicationContext.

      My intelligent guess would be that BeanDefinitionRegistryPostProcessor#postProcessBeanDefinitionRegistry is called after JavaConfig bean postprocessor.

      I'm attaching minimal Maven project with two unit tests (XmlTest & AnnotationTest) to demonstrate the issue.

        Issue Links

          Activity

          Hide
          cbeams Chris Beams added a comment -

          Behavior Confirmed. Notice that RegisteringBean#postProcessBeanFactory is called, but #postProcessBeanDefinitionRegistry is not.

          Good catch, thanks for filing the issue!

          Show
          cbeams Chris Beams added a comment - Behavior Confirmed. Notice that RegisteringBean#postProcessBeanFactory is called, but #postProcessBeanDefinitionRegistry is not. Good catch, thanks for filing the issue!
          Hide
          cbeams Chris Beams added a comment -

          Topic branch: https://github.com/cbeams/spring-framework/commits/SPR-7868. An early fix is there, but will need refinement.

          Show
          cbeams Chris Beams added a comment - Topic branch: https://github.com/cbeams/spring-framework/commits/SPR-7868 . An early fix is there, but will need refinement.
          Hide
          cbeams Chris Beams added a comment - - edited

          This is a fundamental limitation of the @Configuration / @Bean approach. Because @Configuration classes are actually themselves processed by a BeanDefinitionRegistryPostProcessor, they cannot in turn register additional BeanDefinitionRegistryPostProcessors. As a workaround you could register your BDRPP via XML and use @ImportResource, or perhaps more preferably, register it against the application context directly using AnnotationConfigApplicationContext#register(...) See comment thread on SPR-9464 for further explanation, workarounds and suggestions specific to MyBatis.

          Show
          cbeams Chris Beams added a comment - - edited This is a fundamental limitation of the @Configuration / @Bean approach. Because @Configuration classes are actually themselves processed by a BeanDefinitionRegistryPostProcessor, they cannot in turn register additional BeanDefinitionRegistryPostProcessors. As a workaround you could register your BDRPP via XML and use @ImportResource, or perhaps more preferably, register it against the application context directly using AnnotationConfigApplicationContext#register(...) See comment thread on SPR-9464 for further explanation, workarounds and suggestions specific to MyBatis.
          Hide
          jarleso Jarle Søberg added a comment -

          A possible work-around for this issue is to implement BeanDefinitionRegistryPostProcessor and PriorityOrdered as part of the definition of the Configuration:

          @Configuration
          public class A implements BeanDefinitionRegistryPostProcessor, PriorityOrdered {
           
              @Override
              public void postProcessBeanDefinitionRegistry(...) {
                   ...
              }
           
              @Override
              public void postProcessBeanFactory(...) {
                  ...
              }
           
              @Override
              public int getOrder() {
                  return 0;
              }
          }

          Show
          jarleso Jarle Søberg added a comment - A possible work-around for this issue is to implement BeanDefinitionRegistryPostProcessor and PriorityOrdered as part of the definition of the Configuration: @Configuration public class A implements BeanDefinitionRegistryPostProcessor, PriorityOrdered {   @Override public void postProcessBeanDefinitionRegistry(...) { ... }   @Override public void postProcessBeanFactory(...) { ... }   @Override public int getOrder() { return 0; } }

            People

            • Assignee:
              cbeams Chris Beams
              Reporter:
              hekonsek Henryk Konsek
              Last updater:
              Jarle Søberg
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Days since last comment:
                4 years, 20 weeks, 2 days ago