Results 1 to 2 of 2

Thread: Mixed XML/Annotation config

  1. #1
    Join Date
    Jun 2012
    Posts
    7

    Default Mixed XML/Annotation config

    I'm working on setting up a utility that lets us load an annotation-based configuration that overrides an XML configuration. I have tried a number of different setups, but this is the one that I think should work:

    Code:
    GenericApplicationContext ctx = new GenericApplicationContext();
    
    AnnotatedBeanDefinitionReader annotatedReader = new AnnotatedBeanDefinitionReader(ctx);
    annotatedReader.register(SomeConfigClass.class);
    
    XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(ctx);
    xmlReader.loadBeanDefinitions("applicationContext.xml");
    
    ctx.refresh();
    This works, except the XML configuration ALWAYS replaces the annotation-based configuration (even if the order of the two readers is swapped). Is there some way to fix this code so that the annotation configuration replaces the XML configuration where there are bean name conflicts? Alternatively, is there a better way to do this? (Besides splitting our configuration files further, which is out of the question at this point.)

    Thanks!

  2. #2
    Join Date
    Jun 2012
    Posts
    7

    Default Success (sort of)

    Ok, so I got it to work, but it's REALLY ugly. This works:

    Code:
    GenericApplicationContext firstCtx = new GenericApplicationContext();
    
    XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(firstCtx );
    xmlReader.loadBeanDefinitions("applicationContext.xml");
    
    GenericApplicationContext ctx = new GenericApplicationContext();
    
    AnnotatedBeanDefinitionReader annotatedReader = new AnnotatedBeanDefinitionReader(ctx);
    annotatedReader.register(SomeConfigClass.class);
    
    ctx.refresh();
    
    for (String currBeanName : firstCtx.getBeanDefinitionNames())
    {
        if (!ctx.containsBeanDefinition(currBeanName))
        {
            ctx.registerBeanDefinition(currBeanName, firstCtx.getBeanDefinition(currBeanName));
        }
    }
    I'd still prefer to use a cleaner way to do this, if one exists. Suggestions?

Posting Permissions

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