Search in sources :

Example 1 with ApplicationContextInitializer

use of org.springframework.context.ApplicationContextInitializer in project spring-boot by spring-projects.

the class RemoteSpringApplication method getInitializers.

private Collection<ApplicationContextInitializer<?>> getInitializers() {
    List<ApplicationContextInitializer<?>> initializers = new ArrayList<>();
    initializers.add(new RestartScopeInitializer());
    return initializers;
}
Also used : RestartScopeInitializer(org.springframework.boot.devtools.restart.RestartScopeInitializer) ArrayList(java.util.ArrayList) ApplicationContextInitializer(org.springframework.context.ApplicationContextInitializer)

Example 2 with ApplicationContextInitializer

use of org.springframework.context.ApplicationContextInitializer in project spring-boot by spring-projects.

the class SpringBootContextLoader method getInitializers.

private List<ApplicationContextInitializer<?>> getInitializers(MergedContextConfiguration config, SpringApplication application) {
    List<ApplicationContextInitializer<?>> initializers = new ArrayList<>();
    for (ContextCustomizer contextCustomizer : config.getContextCustomizers()) {
        initializers.add(new ContextCustomizerAdapter(contextCustomizer, config));
    }
    initializers.addAll(application.getInitializers());
    for (Class<? extends ApplicationContextInitializer<?>> initializerClass : config.getContextInitializerClasses()) {
        initializers.add(BeanUtils.instantiateClass(initializerClass));
    }
    if (config.getParent() != null) {
        initializers.add(new ParentContextApplicationContextInitializer(config.getParentApplicationContext()));
    }
    return initializers;
}
Also used : ContextCustomizer(org.springframework.test.context.ContextCustomizer) ArrayList(java.util.ArrayList) ServletContextApplicationContextInitializer(org.springframework.boot.web.servlet.support.ServletContextApplicationContextInitializer) ApplicationContextInitializer(org.springframework.context.ApplicationContextInitializer)

Example 3 with ApplicationContextInitializer

use of org.springframework.context.ApplicationContextInitializer in project spring-boot by spring-projects.

the class SpringBootContextLoader method loadContext.

@Override
public ApplicationContext loadContext(MergedContextConfiguration config) throws Exception {
    SpringApplication application = getSpringApplication();
    application.setMainApplicationClass(config.getTestClass());
    application.setSources(getSources(config));
    ConfigurableEnvironment environment = new StandardEnvironment();
    if (!ObjectUtils.isEmpty(config.getActiveProfiles())) {
        setActiveProfiles(environment, config.getActiveProfiles());
    }
    TestPropertySourceUtils.addPropertiesFilesToEnvironment(environment, application.getResourceLoader() == null ? new DefaultResourceLoader(getClass().getClassLoader()) : application.getResourceLoader(), config.getPropertySourceLocations());
    TestPropertySourceUtils.addInlinedPropertiesToEnvironment(environment, getInlinedProperties(config));
    application.setEnvironment(environment);
    List<ApplicationContextInitializer<?>> initializers = getInitializers(config, application);
    if (config instanceof WebMergedContextConfiguration) {
        application.setWebApplicationType(WebApplicationType.SERVLET);
        if (!isEmbeddedWebEnvironment(config)) {
            new WebConfigurer().configure(config, application, initializers);
        }
    } else if (config instanceof ReactiveWebMergedContextConfiguration) {
        application.setWebApplicationType(WebApplicationType.REACTIVE);
        if (!isEmbeddedWebEnvironment(config)) {
            new ReactiveWebConfigurer().configure(application);
        }
    } else {
        application.setWebApplicationType(WebApplicationType.NONE);
    }
    application.setInitializers(initializers);
    ConfigurableApplicationContext context = application.run();
    return context;
}
Also used : WebMergedContextConfiguration(org.springframework.test.context.web.WebMergedContextConfiguration) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) SpringApplication(org.springframework.boot.SpringApplication) ServletContextApplicationContextInitializer(org.springframework.boot.web.servlet.support.ServletContextApplicationContextInitializer) ApplicationContextInitializer(org.springframework.context.ApplicationContextInitializer) StandardEnvironment(org.springframework.core.env.StandardEnvironment) DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader)

Example 4 with ApplicationContextInitializer

use of org.springframework.context.ApplicationContextInitializer in project spring-boot by spring-projects.

the class SpringApplication method applyInitializers.

/**
	 * Apply any {@link ApplicationContextInitializer}s to the context before it is
	 * refreshed.
	 * @param context the configured ApplicationContext (not refreshed yet)
	 * @see ConfigurableApplicationContext#refresh()
	 */
@SuppressWarnings({ "rawtypes", "unchecked" })
protected void applyInitializers(ConfigurableApplicationContext context) {
    for (ApplicationContextInitializer initializer : getInitializers()) {
        Class<?> requiredType = GenericTypeResolver.resolveTypeArgument(initializer.getClass(), ApplicationContextInitializer.class);
        Assert.isInstanceOf(requiredType, context, "Unable to call initializer.");
        initializer.initialize(context);
    }
}
Also used : ApplicationContextInitializer(org.springframework.context.ApplicationContextInitializer)

Example 5 with ApplicationContextInitializer

use of org.springframework.context.ApplicationContextInitializer in project spring-framework by spring-projects.

the class ApplicationContextInitializerUtils method resolveInitializerClasses.

/**
	 * Resolve the set of merged {@code ApplicationContextInitializer} classes for the
	 * supplied list of {@code ContextConfigurationAttributes}.
	 *
	 * <p>Note that the {@link ContextConfiguration#inheritInitializers inheritInitializers}
	 * flag of {@link ContextConfiguration @ContextConfiguration} will be taken into
	 * consideration. Specifically, if the {@code inheritInitializers} flag is set to
	 * {@code true} for a given level in the class hierarchy represented by the provided
	 * configuration attributes, context initializer classes defined at the given level
	 * will be merged with those defined in higher levels of the class hierarchy.
	 *
	 * @param configAttributesList the list of configuration attributes to process; must
	 * not be {@code null} or <em>empty</em>; must be ordered <em>bottom-up</em>
	 * (i.e., as if we were traversing up the class hierarchy)
	 * @return the set of merged context initializer classes, including those from
	 * superclasses if appropriate (never {@code null})
	 * @since 3.2
	 */
static Set<Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>> resolveInitializerClasses(List<ContextConfigurationAttributes> configAttributesList) {
    Assert.notEmpty(configAttributesList, "ContextConfigurationAttributes list must not be empty");
    final //
    Set<Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>> initializerClasses = new HashSet<>();
    for (ContextConfigurationAttributes configAttributes : configAttributesList) {
        if (logger.isTraceEnabled()) {
            logger.trace(String.format("Processing context initializers for context configuration attributes %s", configAttributes));
        }
        initializerClasses.addAll(Arrays.asList(configAttributes.getInitializers()));
        if (!configAttributes.isInheritInitializers()) {
            break;
        }
    }
    return initializerClasses;
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ContextConfigurationAttributes(org.springframework.test.context.ContextConfigurationAttributes) ApplicationContextInitializer(org.springframework.context.ApplicationContextInitializer) HashSet(java.util.HashSet)

Aggregations

ApplicationContextInitializer (org.springframework.context.ApplicationContextInitializer)5 ArrayList (java.util.ArrayList)2 ServletContextApplicationContextInitializer (org.springframework.boot.web.servlet.support.ServletContextApplicationContextInitializer)2 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)2 HashSet (java.util.HashSet)1 SpringApplication (org.springframework.boot.SpringApplication)1 RestartScopeInitializer (org.springframework.boot.devtools.restart.RestartScopeInitializer)1 ConfigurableEnvironment (org.springframework.core.env.ConfigurableEnvironment)1 StandardEnvironment (org.springframework.core.env.StandardEnvironment)1 DefaultResourceLoader (org.springframework.core.io.DefaultResourceLoader)1 ContextConfigurationAttributes (org.springframework.test.context.ContextConfigurationAttributes)1 ContextCustomizer (org.springframework.test.context.ContextCustomizer)1 WebMergedContextConfiguration (org.springframework.test.context.web.WebMergedContextConfiguration)1