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);
}
}
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);
}
}
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;
}
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;
}
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);
}
}
Aggregations