How to connect StaticListableBeanFactory using ClassPa thanksmlApplicationContext?

In setting up my test cases, I have this code:

    ApplicationContext context = new ClassPathXmlApplicationContext(
            "spring/common.xml"
    );
    StaticListableBeanFactory testBeanFactory = new StaticListableBeanFactory();

How to connect them so that tests can register beans in testBeanFactoryduring installation, and the rest of the application uses them instead of those defined in common.xml?

Note. I need to mix static (common.xml) and dynamic configuration. I cannot use XML for the latter because it would mean writing> 1000 XML files.

+3
source share
2 answers

You can use ConfigurableListableBeanFactory.registerSingleton()instead StaticListableBeanFactory.addBean():

ApplicationContext context = new ClassPathXmlApplicationContext(
            "spring/common.xml" 
    ); 

GenericApplicationContext child = new GenericApplicationContext(context);

child.getBeanFactory().registerSingleton("foo", ...);
+3
source

, , Test.xml bean, common.xml:

<import resource="spring/common.xml"/>

<bean id="AnIdThatOverridesSomethingInCommon"/>

bean - XML, Spring .

: , - .

0

Source: https://habr.com/ru/post/1749188/


All Articles