Description
Leo Liang opened SPR-8816 and commented
I create a bean to implement the ApplicationContextAware interface and print out the current context id. But the id isn't the one I specify in xml, it's a generated one like "org.springframework.context.support.ClassPathXmlApplicationContext@15e9756". At the same place I print the id of the parent context, it's the real one.
I guess that reason is: at the time when ApplicationContextAware is invoked, id isn't set yet. Can it be changed so that real context id can be retrieve in ApplicationContextAware?
== Java code:
public class ApplicationContextDumper implements ApplicationContextAware {
@Override
public void setApplicationContext(ApplicationContext context) throws BeansException {
String appContextInfo = String.format("ApplicationContext %s", context.getId());
ApplicationContext parent = context.getParent();
if (parent != null) {
appContextInfo += String.format(" -> %s", parent.getId());
}
System.out.println(appContextInfo);
}
}
public class TestMain {
public static void main(String[] args) {
String[] paths = { "classpath:**/beanRefContext.xml" };
new ClassPathXmlApplicationContext(paths);
}
}
== beanRefContext.xml:
...
<bean id="parentBeanFactory" class="org.springframework.context.support.ClassPathXmlApplicationContext">
<constructor-arg>
<list>
<value>classpath:/parentContext.xml</value>
</list>
</constructor-arg>
</bean>
<bean id="myBeanFactory" class="org.springframework.context.support.ClassPathXmlApplicationContext">
<constructor-arg>
<list>
<value>classpath:/applicationContext.xml</value>
</list>
</constructor-arg>
<constructor-arg>
<ref bean="parentBeanFactory" />
</constructor-arg>
</bean>
...
== applicationContext.xml:
...
<bean class="springtest.ApplicationContextDumper" />
...
== Expected output:
ApplicationContext myBeanFactory -> parentBeanFactory
== Actual output:
ApplicationContext org.springframework.context.support.ClassPathXmlApplicationContext@15e9756 -> parentBeanFactory
Affects: 3.0.6