Search in sources :

Example 1 with ApplicationContextInitializer

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

the class DelegatingApplicationContextInitializer method applyInitializerClasses.

private void applyInitializerClasses(ConfigurableApplicationContext context, List<Class<ApplicationContextInitializer>> initializerClasses) {
    var initializers = new ArrayList<ApplicationContextInitializer>();
    for (Class<ApplicationContextInitializer> initializerClass : initializerClasses) {
        initializers.add(BeanUtils.newInstance(initializerClass));
    }
    AnnotationAwareOrderComparator.sort(initializers);
    for (ApplicationContextInitializer initializer : initializers) {
        initializer.initialize(context);
    }
}
Also used : ArrayList(java.util.ArrayList) ApplicationContextInitializer(cn.taketoday.context.ApplicationContextInitializer)

Example 2 with ApplicationContextInitializer

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

the class DelegatingApplicationContextInitializer method getInitializerClass.

private Class<ApplicationContextInitializer> getInitializerClass(String className) throws LinkageError {
    try {
        Class<ApplicationContextInitializer> initializerClass = ClassUtils.forName(className, ClassUtils.getDefaultClassLoader());
        Assert.isAssignable(ApplicationContextInitializer.class, initializerClass);
        return initializerClass;
    } catch (ClassNotFoundException ex) {
        throw new ApplicationContextException("Failed to load context initializer class [" + className + "]", ex);
    }
}
Also used : ApplicationContextInitializer(cn.taketoday.context.ApplicationContextInitializer) ApplicationContextException(cn.taketoday.context.ApplicationContextException)

Example 3 with ApplicationContextInitializer

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

the class ApplicationContextLoader method getInitializers.

/**
 * Return the {@link ApplicationContextInitializer initializers} that will be applied
 * to the context. By default this method will adapt {@link ContextCustomizer context
 * customizers}, add {@link Application#getInitializers() application
 * initializers} and add
 * {@link MergedContextConfiguration#getContextInitializerClasses() initializers
 * specified on the test}.
 *
 * @param config the source context configuration
 * @param application the application instance
 * @return the initializers to apply
 */
protected List<ApplicationContextInitializer> getInitializers(MergedContextConfiguration config, Application 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.newInstance(initializerClass));
    }
    if (config.getParent() != null) {
        initializers.add(new ParentContextApplicationContextInitializer(config.getParentApplicationContext()));
    }
    return initializers;
}
Also used : ContextCustomizer(cn.taketoday.test.context.ContextCustomizer) ArrayList(java.util.ArrayList) ServletContextApplicationContextInitializer(cn.taketoday.framework.web.servlet.support.ServletContextApplicationContextInitializer) ApplicationContextInitializer(cn.taketoday.context.ApplicationContextInitializer)

Example 4 with ApplicationContextInitializer

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

the class ApplicationContextLoader method getInitializers.

/**
 * Return the {@link ApplicationContextInitializer initializers} that will be applied
 * to the context. By default this method will adapt {@link ContextCustomizer context
 * customizers}, add {@link Application#getInitializers() application
 * initializers} and add
 * {@link MergedContextConfiguration#getContextInitializerClasses() initializers
 * specified on the test}.
 *
 * @param config the source context configuration
 * @param application the application instance
 * @return the initializers to apply
 */
protected List<ApplicationContextInitializer> getInitializers(MergedContextConfiguration config, Application 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.newInstance(initializerClass));
    }
    if (config.getParent() != null) {
        initializers.add(new ParentContextApplicationContextInitializer(config.getParentApplicationContext()));
    }
    return initializers;
}
Also used : ContextCustomizer(cn.taketoday.test.context.ContextCustomizer) ArrayList(java.util.ArrayList) ServletContextApplicationContextInitializer(cn.taketoday.framework.web.servlet.support.ServletContextApplicationContextInitializer) ApplicationContextInitializer(cn.taketoday.context.ApplicationContextInitializer)

Example 5 with ApplicationContextInitializer

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

the class AbstractContextLoader method invokeApplicationContextInitializers.

private void invokeApplicationContextInitializers(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) {
    Set<Class<? extends ApplicationContextInitializer>> initializerClasses = mergedConfig.getContextInitializerClasses();
    if (initializerClasses.isEmpty()) {
        // no ApplicationContextInitializers have been declared -> nothing to do
        return;
    }
    List<ApplicationContextInitializer> initializerInstances = new ArrayList<>();
    Class<?> contextClass = context.getClass();
    for (Class<? extends ApplicationContextInitializer> initializerClass : initializerClasses) {
        Class<?> initializerContextClass = GenericTypeResolver.resolveTypeArgument(initializerClass, ApplicationContextInitializer.class);
        if (initializerContextClass != null && !initializerContextClass.isInstance(context)) {
            throw new ApplicationContextException(String.format("Could not apply context initializer [%s] since its generic parameter [%s] " + "is not assignable from the type of application context used by this " + "context loader: [%s]", initializerClass.getName(), initializerContextClass.getName(), contextClass.getName()));
        }
        initializerInstances.add((ApplicationContextInitializer) BeanUtils.newInstance(initializerClass));
    }
    AnnotationAwareOrderComparator.sort(initializerInstances);
    for (ApplicationContextInitializer initializer : initializerInstances) {
        initializer.initialize(context);
    }
}
Also used : ArrayList(java.util.ArrayList) ApplicationContextInitializer(cn.taketoday.context.ApplicationContextInitializer) ApplicationContextException(cn.taketoday.context.ApplicationContextException)

Aggregations

ApplicationContextInitializer (cn.taketoday.context.ApplicationContextInitializer)12 ArrayList (java.util.ArrayList)6 ApplicationContextException (cn.taketoday.context.ApplicationContextException)4 ServletContextApplicationContextInitializer (cn.taketoday.framework.web.servlet.support.ServletContextApplicationContextInitializer)4 ConfigurableEnvironment (cn.taketoday.core.env.ConfigurableEnvironment)2 Application (cn.taketoday.framework.Application)2 GenericReactiveWebApplicationContext (cn.taketoday.framework.web.reactive.context.GenericReactiveWebApplicationContext)2 ContextConfigurationAttributes (cn.taketoday.test.context.ContextConfigurationAttributes)2 ContextCustomizer (cn.taketoday.test.context.ContextCustomizer)2 WebMergedContextConfiguration (cn.taketoday.test.context.web.WebMergedContextConfiguration)2 LinkedHashSet (java.util.LinkedHashSet)2