Search in sources :

Example 1 with AbstractApplicationContext

use of cn.taketoday.context.support.AbstractApplicationContext in project today-infrastructure by TAKETODAY.

the class ApplicationEventsTestExecutionListener method registerListenerAndResolvableDependencyIfNecessary.

private void registerListenerAndResolvableDependencyIfNecessary(ApplicationContext applicationContext) {
    Assert.isInstanceOf(AbstractApplicationContext.class, applicationContext, "The ApplicationContext for the test must be an AbstractApplicationContext");
    AbstractApplicationContext aac = (AbstractApplicationContext) applicationContext;
    // Synchronize to avoid race condition in parallel test execution
    synchronized (applicationEventsMonitor) {
        boolean notAlreadyRegistered = aac.getApplicationListeners().stream().map(Object::getClass).noneMatch(ApplicationEventsApplicationListener.class::equals);
        if (notAlreadyRegistered) {
            // Register a new ApplicationEventsApplicationListener.
            aac.addApplicationListener(new ApplicationEventsApplicationListener());
            // Register ApplicationEvents as a resolvable dependency for @Autowired support in test classes.
            ConfigurableBeanFactory beanFactory = aac.getBeanFactory();
            beanFactory.registerDependency(ApplicationEvents.class, new ApplicationEventsObjectFactory());
        }
    }
}
Also used : ConfigurableBeanFactory(cn.taketoday.beans.factory.config.ConfigurableBeanFactory) AbstractApplicationContext(cn.taketoday.context.support.AbstractApplicationContext)

Example 2 with AbstractApplicationContext

use of cn.taketoday.context.support.AbstractApplicationContext in project today-framework by TAKETODAY.

the class ApplicationEventsTestExecutionListener method registerListenerAndResolvableDependencyIfNecessary.

private void registerListenerAndResolvableDependencyIfNecessary(ApplicationContext applicationContext) {
    Assert.isInstanceOf(AbstractApplicationContext.class, applicationContext, "The ApplicationContext for the test must be an AbstractApplicationContext");
    AbstractApplicationContext aac = (AbstractApplicationContext) applicationContext;
    // Synchronize to avoid race condition in parallel test execution
    synchronized (applicationEventsMonitor) {
        boolean notAlreadyRegistered = aac.getApplicationListeners().stream().map(Object::getClass).noneMatch(ApplicationEventsApplicationListener.class::equals);
        if (notAlreadyRegistered) {
            // Register a new ApplicationEventsApplicationListener.
            aac.addApplicationListener(new ApplicationEventsApplicationListener());
            // Register ApplicationEvents as a resolvable dependency for @Autowired support in test classes.
            ConfigurableBeanFactory beanFactory = aac.getBeanFactory();
            beanFactory.registerDependency(ApplicationEvents.class, new ApplicationEventsObjectFactory());
        }
    }
}
Also used : ConfigurableBeanFactory(cn.taketoday.beans.factory.config.ConfigurableBeanFactory) AbstractApplicationContext(cn.taketoday.context.support.AbstractApplicationContext)

Aggregations

ConfigurableBeanFactory (cn.taketoday.beans.factory.config.ConfigurableBeanFactory)2 AbstractApplicationContext (cn.taketoday.context.support.AbstractApplicationContext)2