diff --git a/spring-boot/src/main/java/org/springframework/boot/context/initializer/EnvironmentDelegateApplicationContextInitializer.java b/spring-boot/src/main/java/org/springframework/boot/context/initializer/EnvironmentDelegateApplicationContextInitializer.java index 38c1f77bca..e9e5ef4d08 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/initializer/EnvironmentDelegateApplicationContextInitializer.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/initializer/EnvironmentDelegateApplicationContextInitializer.java @@ -49,64 +49,71 @@ public class EnvironmentDelegateApplicationContextInitializer implements private int order = 0; @Override - public void initialize(ConfigurableApplicationContext applicationContext) { - ConfigurableEnvironment env = applicationContext.getEnvironment(); - List>> initializerClasses = determineContextInitializerClasses(env); - - if (initializerClasses.size() == 0) { - // no ApplicationContextInitializers have been declared -> nothing to do - return; - } - - Class contextClass = applicationContext.getClass(); - ArrayList> initializerInstances = new ArrayList>(); - - for (Class> initializerClass : initializerClasses) { - Class initializerContextClass = GenericTypeResolver.resolveTypeArgument( - initializerClass, ApplicationContextInitializer.class); - Assert.isAssignable( - initializerContextClass, - contextClass, - String.format( - "Could not add context initializer [%s] as 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(BeanUtils.instantiateClass(initializerClass)); - } - - Collections.sort(initializerInstances, new AnnotationAwareOrderComparator()); - for (ApplicationContextInitializer initializer : initializerInstances) { - initializer.initialize(applicationContext); + public void initialize(ConfigurableApplicationContext context) { + ConfigurableEnvironment environment = context.getEnvironment(); + List> initializerClasses = getInitializerClasses(environment); + if (initializerClasses.size() > 0) { + applyInitializerClasses(context, initializerClasses); } } - @SuppressWarnings("unchecked") - private List>> determineContextInitializerClasses( - ConfigurableEnvironment env) { + private List> getInitializerClasses(ConfigurableEnvironment env) { String classNames = env.getProperty(PROPERTY_NAME); - List>> classes = new ArrayList>>(); + List> classes = new ArrayList>(); if (StringUtils.hasLength(classNames)) { for (String className : StringUtils.tokenizeToStringArray(classNames, ",")) { - try { - Class clazz = ClassUtils.forName(className, - ClassUtils.getDefaultClassLoader()); - Assert.isAssignable(ApplicationContextInitializer.class, clazz, - "class [" + className - + "] must implement ApplicationContextInitializer"); - classes.add((Class>) clazz); - } - catch (ClassNotFoundException ex) { - throw new ApplicationContextException( - "Failed to load context initializer class [" + className - + "]", ex); - } + classes.add(getInitializerClass(className)); } } return classes; } + private Class getInitializerClass(String className) throws LinkageError { + try { + Class 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); + } + } + + private void applyInitializerClasses(ConfigurableApplicationContext context, + List> initializerClasses) { + Class contextClass = context.getClass(); + List> initializers = new ArrayList>(); + for (Class initializerClass : initializerClasses) { + initializers.add(instantiateInitializer(contextClass, initializerClass)); + } + applyInitializers(context, initializers); + } + + private ApplicationContextInitializer instantiateInitializer( + Class contextClass, Class initializerClass) { + Class requireContextClass = GenericTypeResolver.resolveTypeArgument( + initializerClass, ApplicationContextInitializer.class); + Assert.isAssignable(requireContextClass, contextClass, String.format( + "Could not add context initializer [%s]" + + " as its generic parameter [%s] is not assignable " + + "from the type of application context used by this " + + "context loader [%s]: ", initializerClass.getName(), + requireContextClass.getName(), contextClass.getName())); + return (ApplicationContextInitializer) BeanUtils + .instantiateClass(initializerClass); + } + + @SuppressWarnings({ "unchecked", "rawtypes" }) + private void applyInitializers(ConfigurableApplicationContext context, + List> initializers) { + Collections.sort(initializers, new AnnotationAwareOrderComparator()); + for (ApplicationContextInitializer initializer : initializers) { + initializer.initialize(context); + } + } + public void setOrder(int order) { this.order = order; }