简体   繁体   中英

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.

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());
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM