Results 1 to 4 of 4

Thread: Access parent application context from custom BeanDefinitionParser

  1. #1
    Join Date
    Nov 2009
    Posts
    2

    Question 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:

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

    Code:
    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?

    Thanks,
    Andreas

  2. #2
    Join Date
    Apr 2008
    Location
    Seville, Spain
    Posts
    132

    Default

    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
    Code:
    ApplicationContext parent =((ApplicationContext) pc.getReaderContext().getReader().getResourceLoader()).getParent();
    Cheers
    Last edited by chelu; Jan 10th, 2013 at 04:15 PM.
    Jose Luis Martin
    Freelance Senior Consultant
    JDAL - Java Database Application Library

  3. #3
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  4. #4
    Join Date
    Nov 2009
    Posts
    2

    Default

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •