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?

8
  • 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, 2014 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. Oct 28, 2014 at 12:41
  • @Named isn't for that purpose, @Qualifier is.
    – M. Deinum
    Oct 28, 2014 at 12:43
  • But according to docs.spring.io/spring/docs/current/spring-framework-reference/… Named is equivalence to Qualifier Oct 28, 2014 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, 2014 at 13:00

1 Answer 1

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).

1
  • You can "accept" your own answer by selecting the "tick", which removes it from the list of unanswered questions. Jun 2, 2015 at 8:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.