Search in sources :

Example 91 with ConfigurableListableBeanFactory

use of org.springframework.beans.factory.config.ConfigurableListableBeanFactory in project spring-boot by spring-projects.

the class ConfigurationPropertiesBean method getAll.

private static Map<String, ConfigurationPropertiesBean> getAll(ConfigurableApplicationContext applicationContext) {
    Map<String, ConfigurationPropertiesBean> propertiesBeans = new LinkedHashMap<>();
    ConfigurableListableBeanFactory beanFactory = applicationContext.getBeanFactory();
    Iterator<String> beanNames = beanFactory.getBeanNamesIterator();
    while (beanNames.hasNext()) {
        String beanName = beanNames.next();
        if (isConfigurationPropertiesBean(beanFactory, beanName)) {
            try {
                Object bean = beanFactory.getBean(beanName);
                ConfigurationPropertiesBean propertiesBean = get(applicationContext, bean, beanName);
                propertiesBeans.put(beanName, propertiesBean);
            } catch (Exception ex) {
            }
        }
    }
    return propertiesBeans;
}
Also used : ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) LinkedHashMap(java.util.LinkedHashMap)

Example 92 with ConfigurableListableBeanFactory

use of org.springframework.beans.factory.config.ConfigurableListableBeanFactory in project spring-boot by spring-projects.

the class ServletWebServerApplicationContextTests method doesNotReplaceExistingScopes.

@Test
void doesNotReplaceExistingScopes() {
    // gh-2082
    Scope scope = mock(Scope.class);
    ConfigurableListableBeanFactory factory = this.context.getBeanFactory();
    factory.registerScope(WebApplicationContext.SCOPE_REQUEST, scope);
    factory.registerScope(WebApplicationContext.SCOPE_SESSION, scope);
    addWebServerFactoryBean();
    this.context.refresh();
    assertThat(factory.getRegisteredScope(WebApplicationContext.SCOPE_REQUEST)).isSameAs(scope);
    assertThat(factory.getRegisteredScope(WebApplicationContext.SCOPE_SESSION)).isSameAs(scope);
}
Also used : SessionScope(org.springframework.web.context.request.SessionScope) Scope(org.springframework.beans.factory.config.Scope) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) Test(org.junit.jupiter.api.Test)

Example 93 with ConfigurableListableBeanFactory

use of org.springframework.beans.factory.config.ConfigurableListableBeanFactory in project spring-boot by spring-projects.

the class ResetMocksTestExecutionListener method resetMocks.

private void resetMocks(ConfigurableApplicationContext applicationContext, MockReset reset) {
    ConfigurableListableBeanFactory beanFactory = applicationContext.getBeanFactory();
    String[] names = beanFactory.getBeanDefinitionNames();
    Set<String> instantiatedSingletons = new HashSet<>(Arrays.asList(beanFactory.getSingletonNames()));
    for (String name : names) {
        BeanDefinition definition = beanFactory.getBeanDefinition(name);
        if (definition.isSingleton() && instantiatedSingletons.contains(name)) {
            Object bean = beanFactory.getSingleton(name);
            if (reset.equals(MockReset.get(bean))) {
                Mockito.reset(bean);
            }
        }
    }
    try {
        MockitoBeans mockedBeans = beanFactory.getBean(MockitoBeans.class);
        for (Object mockedBean : mockedBeans) {
            if (reset.equals(MockReset.get(mockedBean))) {
                Mockito.reset(mockedBean);
            }
        }
    } catch (NoSuchBeanDefinitionException ex) {
    // Continue
    }
    if (applicationContext.getParent() != null) {
        resetMocks(applicationContext.getParent(), reset);
    }
}
Also used : BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) HashSet(java.util.HashSet)

Example 94 with ConfigurableListableBeanFactory

use of org.springframework.beans.factory.config.ConfigurableListableBeanFactory in project spring-boot by spring-projects.

the class ConfigurationPropertiesTests method loadWhenBeanFactoryContainsSingletonForConstructorBindingTypeShouldNotFail.

// gh-18652
@Test
void loadWhenBeanFactoryContainsSingletonForConstructorBindingTypeShouldNotFail() {
    ConfigurableListableBeanFactory beanFactory = this.context.getBeanFactory();
    ((BeanDefinitionRegistry) beanFactory).registerBeanDefinition("test", new RootBeanDefinition(ConstructorParameterProperties.class));
    beanFactory.registerSingleton("test", new ConstructorParameterProperties("bar", 5));
    load(TestConfiguration.class);
}
Also used : BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) Test(org.junit.jupiter.api.Test)

Example 95 with ConfigurableListableBeanFactory

use of org.springframework.beans.factory.config.ConfigurableListableBeanFactory in project spring-boot by spring-projects.

the class WebDriverScope method registerWith.

/**
 * Register this scope with the specified context and reassign appropriate bean
 * definitions to used it.
 * @param context the application context
 */
static void registerWith(ConfigurableApplicationContext context) {
    if (!ClassUtils.isPresent(WEB_DRIVER_CLASS, null)) {
        return;
    }
    ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
    if (beanFactory.getRegisteredScope(NAME) == null) {
        beanFactory.registerScope(NAME, new WebDriverScope());
    }
    context.addBeanFactoryPostProcessor(WebDriverScope::postProcessBeanFactory);
}
Also used : ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)

Aggregations

ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)104 Test (org.junit.Test)16 Test (org.junit.jupiter.api.Test)15 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)13 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)12 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)10 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)10 ApplicationContext (org.springframework.context.ApplicationContext)9 BeanDefinitionRegistry (org.springframework.beans.factory.support.BeanDefinitionRegistry)8 GenericBeanDefinition (org.springframework.beans.factory.support.GenericBeanDefinition)7 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)7 XmlWebApplicationContext (org.springframework.web.context.support.XmlWebApplicationContext)7 Map (java.util.Map)6 BeanFactory (org.springframework.beans.factory.BeanFactory)6 ListableBeanFactory (org.springframework.beans.factory.ListableBeanFactory)6 Collectors (java.util.stream.Collectors)5 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)5 HashMap (java.util.HashMap)4 BeanFactoryPostProcessor (org.springframework.beans.factory.config.BeanFactoryPostProcessor)4 SimpleApplicationEventMulticaster (org.springframework.context.event.SimpleApplicationEventMulticaster)4