Search in sources :

Example 41 with ConfigurableListableBeanFactory

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

the class SpringApplication method prepareContext.

private void prepareContext(DefaultBootstrapContext bootstrapContext, ConfigurableApplicationContext context, ConfigurableEnvironment environment, SpringApplicationRunListeners listeners, ApplicationArguments applicationArguments, Banner printedBanner) {
    context.setEnvironment(environment);
    postProcessApplicationContext(context);
    applyInitializers(context);
    listeners.contextPrepared(context);
    bootstrapContext.close(context);
    if (this.logStartupInfo) {
        logStartupInfo(context.getParent() == null);
        logStartupProfileInfo(context);
    }
    // Add boot specific singleton beans
    ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
    beanFactory.registerSingleton("springApplicationArguments", applicationArguments);
    if (printedBanner != null) {
        beanFactory.registerSingleton("springBootBanner", printedBanner);
    }
    if (beanFactory instanceof AbstractAutowireCapableBeanFactory) {
        ((AbstractAutowireCapableBeanFactory) beanFactory).setAllowCircularReferences(this.allowCircularReferences);
        if (beanFactory instanceof DefaultListableBeanFactory) {
            ((DefaultListableBeanFactory) beanFactory).setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding);
        }
    }
    if (this.lazyInitialization) {
        context.addBeanFactoryPostProcessor(new LazyInitializationBeanFactoryPostProcessor());
    }
    // Load the sources
    Set<Object> sources = getAllSources();
    Assert.notEmpty(sources, "Sources must not be empty");
    load(context, sources.toArray(new Object[0]));
    listeners.contextLoaded(context);
}
Also used : DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) AbstractAutowireCapableBeanFactory(org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)

Example 42 with ConfigurableListableBeanFactory

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

the class AbstractApplicationContextRunner method createAndLoadContext.

private C createAndLoadContext() {
    C context = this.runnerConfiguration.contextFactory.get();
    ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
    if (beanFactory instanceof AbstractAutowireCapableBeanFactory) {
        ((AbstractAutowireCapableBeanFactory) beanFactory).setAllowCircularReferences(this.runnerConfiguration.allowCircularReferences);
        if (beanFactory instanceof DefaultListableBeanFactory) {
            ((DefaultListableBeanFactory) beanFactory).setAllowBeanDefinitionOverriding(this.runnerConfiguration.allowBeanDefinitionOverriding);
        }
    }
    try {
        configureContext(context);
        return context;
    } catch (RuntimeException ex) {
        context.close();
        throw ex;
    }
}
Also used : DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) AbstractAutowireCapableBeanFactory(org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)

Example 43 with ConfigurableListableBeanFactory

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

the class SpringApplicationTests method runnersAreCalledAfterStartedIsLoggedAndBeforeApplicationReadyEventIsPublished.

@Test
@SuppressWarnings("unchecked")
void runnersAreCalledAfterStartedIsLoggedAndBeforeApplicationReadyEventIsPublished(CapturedOutput output) throws Exception {
    SpringApplication application = new SpringApplication(ExampleConfig.class);
    ApplicationRunner applicationRunner = mock(ApplicationRunner.class);
    CommandLineRunner commandLineRunner = mock(CommandLineRunner.class);
    application.addInitializers((context) -> {
        ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
        beanFactory.registerSingleton("commandLineRunner", (CommandLineRunner) (args) -> {
            assertThat(output).contains("Started");
            commandLineRunner.run(args);
        });
        beanFactory.registerSingleton("applicationRunner", (ApplicationRunner) (args) -> {
            assertThat(output).contains("Started");
            applicationRunner.run(args);
        });
    });
    application.setWebApplicationType(WebApplicationType.NONE);
    ApplicationListener<ApplicationReadyEvent> eventListener = mock(ApplicationListener.class);
    application.addListeners(eventListener);
    this.context = application.run();
    InOrder applicationRunnerOrder = Mockito.inOrder(eventListener, applicationRunner);
    applicationRunnerOrder.verify(applicationRunner).run(any(ApplicationArguments.class));
    applicationRunnerOrder.verify(eventListener).onApplicationEvent(any(ApplicationReadyEvent.class));
    InOrder commandLineRunnerOrder = Mockito.inOrder(eventListener, commandLineRunner);
    commandLineRunnerOrder.verify(commandLineRunner).run();
    commandLineRunnerOrder.verify(eventListener).onApplicationEvent(any(ApplicationReadyEvent.class));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) ArgumentMatchers(org.mockito.ArgumentMatchers) ApplicationReadyEvent(org.springframework.boot.context.event.ApplicationReadyEvent) Autowired(org.springframework.beans.factory.annotation.Autowired) ApplicationContextException(org.springframework.context.ApplicationContextException) AnnotationConfigUtils(org.springframework.context.annotation.AnnotationConfigUtils) SimpleApplicationEventMulticaster(org.springframework.context.event.SimpleApplicationEventMulticaster) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Assertions.assertThatNoException(org.assertj.core.api.Assertions.assertThatNoException) BDDMockito.given(org.mockito.BDDMockito.given) ApplicationStartedEvent(org.springframework.boot.context.event.ApplicationStartedEvent) Map(java.util.Map) AvailabilityChangeEvent(org.springframework.boot.availability.AvailabilityChangeEvent) TomcatServletWebServerFactory(org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory) Resource(org.springframework.core.io.Resource) ApplicationContextInitializedEvent(org.springframework.boot.context.event.ApplicationContextInitializedEvent) SpringApplicationBuilder(org.springframework.boot.builder.SpringApplicationBuilder) Mockito.atLeastOnce(org.mockito.Mockito.atLeastOnce) Set(java.util.Set) CachedIntrospectionResults(org.springframework.beans.CachedIntrospectionResults) ApplicationStartup(org.springframework.core.metrics.ApplicationStartup) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) ApplicationStartingEvent(org.springframework.boot.context.event.ApplicationStartingEvent) CompositePropertySource(org.springframework.core.env.CompositePropertySource) AbstractApplicationContext(org.springframework.context.support.AbstractApplicationContext) BeanNameGenerator(org.springframework.beans.factory.support.BeanNameGenerator) Lazy(org.springframework.context.annotation.Lazy) ApplicationContextAware(org.springframework.context.ApplicationContextAware) Mockito.mock(org.mockito.Mockito.mock) Ordered(org.springframework.core.Ordered) CommandLinePropertySource(org.springframework.core.env.CommandLinePropertySource) PropertySource(org.springframework.core.env.PropertySource) Mockito.spy(org.mockito.Mockito.spy) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) ApplicationEnvironmentPreparedEvent(org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) BeanCreationException(org.springframework.beans.factory.BeanCreationException) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) LinkedHashSet(java.util.LinkedHashSet) ArgumentMatchers.isA(org.mockito.ArgumentMatchers.isA) ReactiveWebApplicationContext(org.springframework.boot.web.reactive.context.ReactiveWebApplicationContext) LivenessState(org.springframework.boot.availability.LivenessState) SmartApplicationListener(org.springframework.context.event.SmartApplicationListener) MultiValueMap(org.springframework.util.MultiValueMap) Mono(reactor.core.publisher.Mono) ApplicationEvent(org.springframework.context.ApplicationEvent) ConfigurableWebEnvironment(org.springframework.web.context.ConfigurableWebEnvironment) AfterEach(org.junit.jupiter.api.AfterEach) Mockito.never(org.mockito.Mockito.never) NettyReactiveWebServerFactory(org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory) Condition(org.assertj.core.api.Condition) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) CapturedOutput(org.springframework.boot.testsupport.system.CapturedOutput) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) MockEnvironment(org.springframework.mock.env.MockEnvironment) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AnnotationConfigServletWebServerApplicationContext(org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext) Mockito.mockingDetails(org.mockito.Mockito.mockingDetails) ApplicationPreparedEvent(org.springframework.boot.context.event.ApplicationPreparedEvent) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) DefaultBeanNameGenerator(org.springframework.beans.factory.support.DefaultBeanNameGenerator) ArgumentMatcher(org.mockito.ArgumentMatcher) BeanCurrentlyInCreationException(org.springframework.beans.factory.BeanCurrentlyInCreationException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Profiles(org.springframework.core.env.Profiles) ApplicationEventMulticaster(org.springframework.context.event.ApplicationEventMulticaster) ResourceLoader(org.springframework.core.io.ResourceLoader) BDDMockito.willThrow(org.mockito.BDDMockito.willThrow) ApplicationConversionService(org.springframework.boot.convert.ApplicationConversionService) StandardServletEnvironment(org.springframework.web.context.support.StandardServletEnvironment) WebApplicationContext(org.springframework.web.context.WebApplicationContext) ApplicationListener(org.springframework.context.ApplicationListener) ApplicationFailedEvent(org.springframework.boot.context.event.ApplicationFailedEvent) BeanDefinitionOverrideException(org.springframework.beans.factory.support.BeanDefinitionOverrideException) Test(org.junit.jupiter.api.Test) Configuration(org.springframework.context.annotation.Configuration) List(java.util.List) InstanceSupplier(org.springframework.boot.BootstrapRegistry.InstanceSupplier) TestPropertySourceUtils(org.springframework.test.context.support.TestPropertySourceUtils) Environment(org.springframework.core.env.Environment) MapPropertySource(org.springframework.core.env.MapPropertySource) PostConstruct(javax.annotation.PostConstruct) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) AnnotationConfigReactiveWebServerApplicationContext(org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext) ClassPathResource(org.springframework.core.io.ClassPathResource) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) UnsatisfiedDependencyException(org.springframework.beans.factory.UnsatisfiedDependencyException) ObjectProvider(org.springframework.beans.factory.ObjectProvider) ArgumentCaptor(org.mockito.ArgumentCaptor) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) OutputCaptureExtension(org.springframework.boot.testsupport.system.OutputCaptureExtension) DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) ReadinessState(org.springframework.boot.availability.ReadinessState) InOrder(org.mockito.InOrder) Iterator(java.util.Iterator) BDDMockito.then(org.mockito.BDDMockito.then) StandardEnvironment(org.springframework.core.env.StandardEnvironment) AvailabilityState(org.springframework.boot.availability.AvailabilityState) StartupStep(org.springframework.core.metrics.StartupStep) ApplicationContext(org.springframework.context.ApplicationContext) SpringApplicationEvent(org.springframework.boot.context.event.SpringApplicationEvent) Mockito(org.mockito.Mockito) ContextRefreshedEvent(org.springframework.context.event.ContextRefreshedEvent) HttpHandler(org.springframework.http.server.reactive.HttpHandler) Bean(org.springframework.context.annotation.Bean) Collections(java.util.Collections) ApplicationContextInitializer(org.springframework.context.ApplicationContextInitializer) StringUtils(org.springframework.util.StringUtils) InOrder(org.mockito.InOrder) ApplicationReadyEvent(org.springframework.boot.context.event.ApplicationReadyEvent) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) Test(org.junit.jupiter.api.Test)

Example 44 with ConfigurableListableBeanFactory

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

the class SessionAutoConfigurationJdbcTests method sessionRepositoryBeansDependOnJdbcSessionDataSourceInitializer.

@Test
void sessionRepositoryBeansDependOnJdbcSessionDataSourceInitializer() {
    this.contextRunner.withPropertyValues("spring.session.store-type=jdbc").run((context) -> {
        ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
        String[] sessionRepositoryNames = beanFactory.getBeanNamesForType(JdbcIndexedSessionRepository.class);
        assertThat(sessionRepositoryNames).isNotEmpty();
        for (String sessionRepositoryName : sessionRepositoryNames) {
            assertThat(beanFactory.getBeanDefinition(sessionRepositoryName).getDependsOn()).contains("jdbcSessionDataSourceScriptDatabaseInitializer");
        }
    });
}
Also used : ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) Test(org.junit.jupiter.api.Test)

Example 45 with ConfigurableListableBeanFactory

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

the class SessionAutoConfigurationJdbcTests method sessionRepositoryBeansDependOnLiquibase.

@Test
void sessionRepositoryBeansDependOnLiquibase() {
    this.contextRunner.withConfiguration(AutoConfigurations.of(LiquibaseAutoConfiguration.class)).withPropertyValues("spring.session.store-type=jdbc", "spring.session.jdbc.initialize-schema=never").run((context) -> {
        ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
        String[] sessionRepositoryNames = beanFactory.getBeanNamesForType(JdbcIndexedSessionRepository.class);
        assertThat(sessionRepositoryNames).isNotEmpty();
        for (String sessionRepositoryName : sessionRepositoryNames) {
            assertThat(beanFactory.getBeanDefinition(sessionRepositoryName).getDependsOn()).contains("liquibase");
        }
    });
}
Also used : LiquibaseAutoConfiguration(org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) Test(org.junit.jupiter.api.Test)

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