Create ApplicationContext as Spring bean (in a different application context)

How can I define one ApplicationContext as a spring bean prototype in a different application context. I also need to pass the current context as a parent to the new application context.

More details:

I have Bean, which are one user sessions in a rich client application. This class controls the lifetime of the application context and several other objects (for example, connecting to the database). This beans session itself is configured with a special "launch application context".

Now I want to unit test this beans session, but you have problems because the session-specific application context created inside the Bean session depends a lot on the “launch context”;

Code example:

public class UserDBAminSession implements ApplicationContextAware, UserSession {
    ApplicationContext startupContext;
    ApplicationContext sessionContext;

    public void setApplicationContext(ApplicationContext startupContext) {...}

    public void start() {
        createSessionContext() ;
    }

    private void createSessionContext() {
        sessionContext = new ClassPathXmlApplicationContext("admin-session.xml", startupContext);
    }
}

, createSessionContext - :

sessionContext = startupContext.getBean("adminContext", ApplicationContext.class);

mock startupContext, . DI " " bean spring, . , ClassPamlApplicationContext. - , , :

<bean id="adminContext" class="org.springframework.context.support.ClassPathXmlApplicationContext"
        scope="prototype" autowire="constructor">
    <constructor-arg type="java.lang.String">
        <value>admin-session.xml</value>
    </constructor-arg>
</bean>

setter, :

  • , . (UserSession - " " ).
  • RAII.
  • .

" factory", .

, IoC IoC. , spring?

, unit-test ?

+3
2

FactoryBean ApplicationContextAware.

public class ChildApplicationContextFactoryBean implements FactoryBean, ApplicationContextAware {

    protected String[] configLocations;

    protected ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Override
    public Object getObject() throws Exception {
        return new ClassPathXmlApplicationContext(configLocations, applicationContext);
    }

    @Override
    public Class getObjectType() {
        return ClassPathXmlApplicationContext.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }

    public void setConfigLocations(String[] configLocations) {
        this.configLocations = configLocations;
    }

}

:

<bean class="com.skg.ecg.portal.operation.transit.ChildApplicationContextFactoryBean">
    <property name="configLocations">
        <list>
            <value>applicationContext.xml</value>
        </list>
    </property>
</bean>
+3

, beans ( RIA).

Spring scoped beans. singleton prototype, webapps request session. RIA HTTP, session -scoped beans .

-, . , , .

, beans , , , , , . , , SingletonBeanFactoryLocator, Spring . , , .

0

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


All Articles