Announcement Announcement Module
Collapse
No announcement yet.
Mixed XML/Annotation config Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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?

    Comment

    Working...
    X