Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to know resource from which a bean is loaded in spring project.

Tags:

java

spring

I am new to spring framework. I am trying to know the list of xml files that are referenced while loading the beans.

By writing a class that is ApplicationContextAware, i am able to view the list of beans with :

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring/sample-testcontext.xml")

public class SampleClass implements ApplicationContextAware {
    @Autowired
    ApplicationContext applicationContext;

    @Test
    public void testMethod() {

        for (String beanName : applicationContext.getBeanDefinitionNames()) {
            System.out.println("BeanName " + beanName);
        }
    }
}

But i want to know from which configuration files the beans are loaded.

Say "sample-testcontext.xml" contains

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">


    <beans:import resource="classpath*:spring/sample-testOneMorecontext.xml"/>
</beans:beans>

I want to know list of file names from which beans are loaded as "sample-testOneMorecontext.xml" and "sample-testcontext.xml".

like image 293
pavan kumar chaitanya Avatar asked Apr 22 '14 09:04

pavan kumar chaitanya


1 Answers

Why would you want to do that exactly? I am not sure that the internal implementation keeps a record of that information once the context has loaded. However, there is a way to know from which resource a particular bean has been loaded. That can be useful if you have several bean definitions with the same name and you want to know which one has "won".

Taking back your example (btw, you don't need to implement ApplicationContextAware since you are autowiring it)

@ContextConfiguration
@ContextConfiguration("classpath:spring/sample-testcontext.xml")
public class SampleTest {

    @Autowired
    private ConfigurableApplicationContext context;

    @Test
    public void foo() {
        ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
        for (String beanName : context.getBeanDefinitionNames()) {
            System.out.println(beanName + " --> "+  beanFactory.getBeanDefinition(beanName).getResourceDescription());
        }
    }
}

This gives you something like (excluding the internal post processor bean definitions that the default implementation may register automatically)

beanFirst --> class org.SampleTest$Config
beanSecond --> class path resource [foobar.xml]

Where beanFirst was loaded from an inner class of the test (called Config) and beanSecond was loaded from a file called foobar.xml at the root of the classpath.

like image 75
Stephane Nicoll Avatar answered Nov 16 '22 18:11

Stephane Nicoll