0

I have multiple spring config xml files which are being used to create one global context like this:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  <bean class="org.springframework.context.support.ClassPathXmlApplicationContext" id="global.context">
    <constructor-arg index="0">
      <list>
        <value>classpath:config/common/main-springconfig.xml</value>
        <value>classpath:config/me/main-springconfig.xml</value>
      </list>
    </constructor-arg>
  </bean>
</beans>

I create context like this:

private static final String springContext = "global.context";
private static final String beanRefContext = "classpath*:global-config.xml";
private ConfigurableApplicationContext springApplicationContext;

ClassPathXmlServiceLocator()
{
    BeanFactoryLocator beanFactoryLocator = ContextSingletonBeanFactoryLocator.getInstance(beanRefContext);
    springApplicationContext = (ConfigurableApplicationContext) beanFactoryLocator.
            useBeanFactory(springContext).getFactory();
}

The problem is that each config has defenition of bean with the same type: some.package.BeanType, but when context is fully instantiated there is only one bean of that type is available.

There is a note in ClassPathXmlApplicationContext javadoc:

In case of multiple config locations, later bean definitions will override ones defined in earlier loaded files. This can be leveraged to deliberately override certain bean definitions via an extra XML file.

But does it mean that even beans with different ids defined in separate config files will be overriden? How can I overcome this issue?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • They should only be overriden if the ids or names match else it should result in 2 different beans. How do you check if a bean is overridden? – M. Deinum Oct 28 '14 at 12:35
  • I retrieve bean from context by id. Also there is another bean which is being injected by some.package.BeanType bean. Injected bean is wrong despite of @Named annotation which specify correct bean. When I use only one sub config file with necessary for this injection bean everything is fine. – thereisnospoon Oct 28 '14 at 12:41
  • `@Named` isn't for that purpose, `@Qualifier` is. – M. Deinum Oct 28 '14 at 12:43
  • But according to http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#beans-standard-annotations-limitations Named is equivalence to Qualifier – thereisnospoon Oct 28 '14 at 12:46
  • Hmm normally you use a `@Named` to create a component `@Qualifier` (There are 2 btw!) is to mark a specific instance. – M. Deinum Oct 28 '14 at 13:00
  • The exception will only occur if there is a bean with the same id/name. If you have a bean without an idea or have one in XML and do component-scanning you might endup with multiple instances. – M. Deinum Oct 28 '14 at 13:01
  • Whatever it was, according to stacktrace bean is overriden inspite of different ids. I think in this case check is only performed by bean types but not ids. Both beans are defined with ids. – thereisnospoon Oct 28 '14 at 13:05
  • Added exception is invalid. It occured because i've tried to load the same configuration file twice. – thereisnospoon Oct 28 '14 at 13:17

1 Answers1

0

Finally, I found the root cause of the problem. Spring have been working correctly all the time. The problem was in third party class (so called: some.package.BeanType).