Announcement Announcement Module
No announcement yet.
Access parent application context from custom BeanDefinitionParser Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Access parent application context from custom BeanDefinitionParser

    I have implemented a custom BeanDefinitionParser by extending AbstractBeanDefinitionParser.

    The BeanDefinitionParser attempts to retrieve a BeanDefinition that is defined in a parent application context.

    The custom BeanDefinitionParser is triggered in the following way:

    ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext(configLocations, parentApplicationContext);
    This is how I attempt to retrieve the BeanDefinition in the parent application context from the custom BeanDefinitionParser:

    BeanDefinitionRegistry registry = parserContext.getRegistry();
    BeanDefinition parentBeanDefinition = registry.getBeanDefinition(parentBeanReference);
    However, the registry inside the BeanDefinitionParser does not contain any of the bean definitions from the parent application context. Is there another way to accomplish this? Is there no way to reference a parent context when using a custom BeanDefinitionParser?


  • #2
    I don't know what do you want to do, but I think that don't seem a good idea that a BeanDefinitionParser depends to another bean definition defined somewhere. You could register a ComponentDefinition with the two (or more) dependant bean definitions together.

    The resourceLoader of XmlBeanDefinitionReader is the ApplicationContext, so you have a chance to get the parent contex with
    ApplicationContext parent =((ApplicationContext) pc.getReaderContext().getReader().getResourceLoader()).getParent();
    Last edited by chelu; Jan 10th, 2013, 05:15 PM.


    • #3
      To add I think it isn't a very good idea to make a BeanDefinitionParser that depends on the fact that there is a parent context. Simply refer to the name of the bean(definition) you need instead of the actual definition. Maybe you can explain a bit more about the usecase instead of the problem you are currently trying to solve.


      • #4
        I never wanted to depend on the fact that there is a parent context. I was just being too aggressive with my error checking. I was trying to make sure the bean definition exists and log a custom error message before using the bean reference in the beans created by the custom namespace.

        I removed the check and now I just let Spring take care of the error checking.