Close

Spring - Calling BeanFactoryUtils.beanNamesForTypeIncludingAncestors within @Bean method

[Last Updated: Feb 6, 2019]

Spring Framework 

This example shows that BeanFactoryUtils#beanNamesForTypeIncludingAncestors can be used within @Bean method, i.e. during the time when bean is being initialized and loaded to the Spring container. In Spring, beans are actually registered by their names before they are loaded.

@Configuration
public class ExampleMain {
  @Autowired
  private ApplicationContext applicationContext;

  @Bean
  MyBean myExampleBean() {
      System.out.println("initializing myExampleBean");
      String[] names = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(
              this.applicationContext, MyBean.class, true, false);
      System.out.println(Arrays.toString(names));
      return new MyBean();
  }

  @Bean
  @Lazy
  MyBean myLazyExampleBean() {
      System.out.println("initializing myExampleBean");
      String[] names = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(
              this.applicationContext, MyBean.class, true, false);
      System.out.println(Arrays.toString(names));
      return new MyBean();
  }

  public static void main(String[] args) {
      ApplicationContext context =
              new AnnotationConfigApplicationContext(ExampleMain.class);
      System.out.println("-- accessing myExampleBean --");
      Object bean = context.getBean("myExampleBean");
      System.out.println(bean);

      System.out.println("-- accessing myLazyExampleBean --");
       bean = context.getBean("myLazyExampleBean");
      System.out.println(bean);

  }
}

Output

initializing myExampleBean
[myExampleBean, myLazyExampleBean]
-- accessing myExampleBean --
com.logicbig.example.ExampleMain$MyBean@5cbc85f8
-- accessing myLazyExampleBean --
initializing myExampleBean
[myExampleBean, myLazyExampleBean]
com.logicbig.example.ExampleMain$MyBean@3f790bf9

Note that calling methods like BeanFactoryUtils#beanOfType (which actually return the instance of the bean), within @Bean method, might end up in the following exception:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myExampleBean' defined in com.logicbig.example.ExampleMain: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.logicbig.example.ExampleMain$MyBean]: Factory method 'myExampleBean' threw exception; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.logicbig.example.ExampleMain$MyBean' available
Related cause: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'myExampleBean': Requested bean is currently in creation: Is there an unresolvable circular reference?
	at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1173)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1067)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)

Example Project

Dependencies and Technologies Used:

  • spring-context 4.3.7.RELEASE: Spring Context.
  • JDK 1.8
  • Maven 3.3.9

spring-bean-registration Select All Download
  • spring-bean-registration
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • ExampleMain.java

    See Also