2 Replies Latest reply on Aug 24, 2009 7:50 PM by David Fuchs

    Can't get a reference to parent context.

    David Fuchs Newbie

      I've got an EAR deployment with multiple JARs - each JAR contains its own Spring context via jboss-spring.xml files. The challenge I'm facing is that I want my main application to be able to instantiate Spring beans from these child contexts.

      Now, I haven't a lot of experience with Spring, but I'm assuming that through the parent/child hierarchy of contexts (using 'BeanFactory=(xxx) and ParentBeanFactory=(xxx)'), the children can get beans from the parent, but not vice versa.

      That doesn't work for me - I need access to those beans, so I'm trying to register the child beans directly into the parent context using a BeanPostProcessor that finds the parent and stuffs it in there instead.

      Maybe this isn't such a good idea (could be other dependency issues?), but that's beside the point. :) The trouble is, I can't get a reference to the parent context... NamedXmlApplicationContext.getParent() always returns null.

      I wrote a simple ApplicationListener (see below) to dump out all my beans and their context name. It also logs the parent context name for the given context, if it's there, but it never is (see log output below).

      Note that if I change my module order, I get errors in the child context that the parent BeanFactory is not bound, so I know that in my normal setup, my parent is definitely being registered (I also see it in the JNDI tree).

      public class SpringContextListener implements ApplicationListener {
       private static final Logger logger = Logger.getLogger(SpringContextListener.class);
       public void onApplicationEvent(ApplicationEvent applicationEvent) {
       if (logger.isTraceEnabled()) {
       if (applicationEvent.getSource() instanceof NamedXmlApplicationContext) {
       NamedXmlApplicationContext context = (NamedXmlApplicationContext) applicationEvent.getSource();
       NamedXmlApplicationContext parentContext = (NamedXmlApplicationContext) context.getParent();
       String[] registeredBeans = context.getBeanDefinitionNames();
       for (String bean : registeredBeans) {
       if (parentContext == null) {
       logger.trace(String.format("Spring Bean (%s,NoParent): %s", context.getName(), bean));
       } else {
       logger.trace(String.format("Spring Bean (%s,%s): %s", context.getName(), parentContext.getName(), bean));

      Log output:
      2009-08-24 15:32:48,956 TRACE [com.mydomain.listeners.SpringContextListener] Spring Bean (DiagnosticPlugin-BeanFactory,NoParent): org.springframework.context.annotation.internalPersistenceAnnotationProcessor
      2009-08-24 15:32:48,962 TRACE [com.mydomain.listeners.SpringContextListener] Spring Bean (DiagnosticPlugin-BeanFactory,NoParent): org.springframework.context.annotation.internalCommonAnnotationProcessor
      2009-08-24 15:32:48,968 TRACE [com.mydomain.listeners.SpringContextListener] Spring Bean (DiagnosticPlugin-BeanFactory,NoParent): org.springframework.context.annotation.internalAutowiredAnnotationProcessor
      2009-08-24 15:32:48,974 TRACE [com.mydomain.listeners.SpringContextListener] Spring Bean (DiagnosticPlugin-BeanFactory,NoParent): org.springframework.context.annotation.internalRequiredAnnotationProcessor
      ... etc ...

      Description for parent jboss-spring.xml:


      Description for child jboss-spring.xml:

      <description>BeanFactory=(DiagnosticPlugin-BeanFactory) ParentBeanFactory=(Main-BeanFactory)</description>

      Any ideas on what I've done wrong, and how I can gain access to the parent context?