Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a programmatic way to find out how a Spring bean was created?

Is there a programmatic way to find out which Configuration class or xml file created a Spring bean? Instead of digging through the code to figure it out.

like image 533
Glide Avatar asked Feb 03 '16 19:02

Glide


People also ask

How is Spring bean created?

Different Methods to Create a Spring BeanCreating Bean Inside an XML Configuration File (beans. xml) Using @Component Annotation. Using @Bean Annotation.

How is a Spring bean uniquely identified?

This attribute specifies the bean identifier uniquely. In XMLbased configuration metadata, you use the id and/or name attributes to specify the bean identifier(s). This attribute specifies the scope of the objects created from a particular bean definition and it will be discussed in bean scopes chapter.

How can I tell if a bean is created in Spring boot?

You can see actuator information in, http://localhost:8080/actuator. This will show all the application information along with actuator, status, info,etc.. In that, you can find the http://localhost:8080/actuator/beans which will load all the beans internally created by springboot application.

How are Spring beans instantiated?

Bean life cycle is managed by the spring container. When we run the program then, first of all, the spring container gets started. After that, the container creates the instance of a bean as per the request, and then dependencies are injected. And finally, the bean is destroyed when the spring container is closed.


1 Answers

The following demonstrates how to obtain the source of the configuration using the bean name.

  1. Obtain the bean definition for the bean: ctx.getBeanDefinition("beanName")
  2. Invoke getResourceDescription().

Below is a working example which sets up a @Configuration based bean called 'a' defined in AppConfig, and an XML bean named "xmlBean" defined in SpringBeans.xml. In each case, the source @Configuration class, or xml file is displayed correctly.

Here is the java config class which sets up bean=a, and also loads XML config file SpringBeans.xml containing bean=xmlBean.

@Configuration
@ImportResource({"classpath:SpringBeans.xml"})
@ComponentScan(basePackages = "com.test.config")
public class AppConfig {

    @Bean
    public A a() {
        return new A();
    }

}

Here is the bean defined in SpringBeans.xml:

<bean id="xmlBean" class="com.test.HelloWorld">
    <property name="name" value="XML" />
</bean>

Here is simple code which uses getResourceDescription():

    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();

    ctx.register(AppConfig.class);
    ctx.refresh();

    BeanDefinition javaConfigBeanDefinition = ctx.getBeanDefinition("a");
    System.out.println("Creation class for a=" + javaConfigBeanDefinition.getResourceDescription());

    BeanDefinition xmlBeanDefinition = ctx.getBeanDefinition("xmlBean");
    System.out.println("Creation XML file for xmlBean=" + xmlBeanDefinition.getResourceDescription());

Output:

Creation class for a=com.test.config.AppConfig
Creation XML file for xmlBean=class path resource [SpringBeans.xml]

Probably a more practical way is to create a BeanDefinitionRegistryPostProcessor and request the information there:

@Component
public class FindBeanConfigLocation implements BeanDefinitionRegistryPostProcessor {

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory arg0) throws BeansException {
    }

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        BeanDefinition javaConfigBeanDefinition = registry.getBeanDefinition("a");
        System.out.println("Creation class for a=" + javaConfigBeanDefinition.getResourceDescription());

        BeanDefinition xmlBeanDefinition = registry.getBeanDefinition("xmlBean");
        System.out.println("Creation XML file for xmlBean=" + xmlBeanDefinition.getResourceDescription());
    }

}
like image 199
Ian Mc Avatar answered Nov 03 '22 00:11

Ian Mc