Skip to content

Commit

Permalink
Replace AotApplicationContextInitializer with interface
Browse files Browse the repository at this point in the history
Replace the `ApplicationContextAotInitializer` class with an
`AotApplicationContextInitializer` interface so that its use can be
detected using a simple `instanceof` check. The existing functionality
has been moved to a factory method on the interface allowing:

	`new ApplicationContextAotInitializer()
		.initialize(context, names);`

To now be written as:

	`AotApplicationContextInitializer.forInitializerClasses(names)
		.initialize(context);`

See gh-29157
  • Loading branch information
philwebb committed Sep 29, 2022
1 parent 3eaf326 commit ac13d26
Show file tree
Hide file tree
Showing 4 changed files with 238 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.context.aot;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.beans.BeanInstantiationException;
import org.springframework.beans.BeanUtils;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.log.LogMessage;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;

/**
* Specialized {@link ApplicationContextInitializer} used to initialize a
* {@link ConfigurableApplicationContext} using artifacts that were generated
* ahead-of-time.
* <p>
* Instances of this initializer are usually created using
* {@link #forInitializerClasses(String...)}, passing in the names of code
* generated initializer classes.
*
* @author Stephane Nicoll
* @author Phillip Webb
* @since 6.0
* @param <C> the application context type
*/
@FunctionalInterface
public interface AotApplicationContextInitializer<C extends ConfigurableApplicationContext>
extends ApplicationContextInitializer<C> {

/**
* Factory method to create a new {@link AotApplicationContextInitializer}
* instance that delegates to other initializers loaded from the given set
* of class names.
* @param <C> the application context type
* @param initializerClassNames the class names of the initializers to load
* @return a new {@link AotApplicationContextInitializer} instance
*/
static <C extends ConfigurableApplicationContext> ApplicationContextInitializer<C> forInitializerClasses(
String... initializerClassNames) {

Assert.noNullElements(initializerClassNames, "'initializerClassNames' must not contain null elements");
return applicationContext -> initialize(applicationContext, initializerClassNames);
}

private static <C extends ConfigurableApplicationContext> void initialize(
C applicationContext, String... initializerClassNames) {
Log logger = LogFactory.getLog(AotApplicationContextInitializer.class);
ClassLoader classLoader = applicationContext.getClassLoader();
logger.debug("Initializing ApplicationContext with AOT");
for (String initializerClassName : initializerClassNames) {
logger.trace(LogMessage.format("Applying %s", initializerClassName));
instantiateInitializer(classLoader, initializerClassName)
.initialize(applicationContext);
}
}

@SuppressWarnings("unchecked")
static <C extends ConfigurableApplicationContext> ApplicationContextInitializer<C> instantiateInitializer(
ClassLoader classLoader, String initializerClassName) {
try {
Class<?> initializerClass = ClassUtils.resolveClassName(initializerClassName, classLoader);
Assert.isAssignable(ApplicationContextInitializer.class, initializerClass);
return (ApplicationContextInitializer<C>) BeanUtils.instantiateClass(initializerClass);
}
catch (BeanInstantiationException ex) {
throw new IllegalArgumentException(
"Failed to instantiate ApplicationContextInitializer: " + initializerClassName, ex);
}
}

/**
* Return a new {@link List} containing only {@link AotApplicationContextInitializer} instances.
* @param <C> the application context type
* @param initializers the source initializers
* @return a list of the {@link AotApplicationContextInitializer} instances
*/
@SuppressWarnings("unchecked")
public static <C extends ConfigurableApplicationContext> List<AotApplicationContextInitializer<C>> getAotInitializers(
Collection<? extends ApplicationContextInitializer<? extends C>> initializers) {

Assert.notNull(initializers, "'initializers' must not be null");
List<AotApplicationContextInitializer<C>> aotInitializers = new ArrayList<>();
for (ApplicationContextInitializer<?> candidate : initializers) {
if (candidate instanceof AotApplicationContextInitializer<?> aotInitializer) {
aotInitializers.add((AotApplicationContextInitializer<C>) aotInitializer);
}
}
return aotInitializers;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,19 @@

package org.springframework.context.aot;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.beans.BeanUtils;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.lang.Nullable;
import org.springframework.util.ClassUtils;

/**
* Initializes a {@link ConfigurableApplicationContext} using AOT optimizations.
*
* @author Stephane Nicoll
* @since 6.0
* @deprecated in favor of {@link AotApplicationContextInitializer}
*/
@Deprecated(since = "6.0", forRemoval = true)
public class ApplicationContextAotInitializer {

private static final Log logger = LogFactory.getLog(ApplicationContextAotInitializer.class);

/**
* Initialize the specified application context using the specified
* {@link ApplicationContextInitializer} class names. Each class is
Expand All @@ -43,35 +37,7 @@ public class ApplicationContextAotInitializer {
* @param initializerClassNames the application context initializer class names
*/
public void initialize(ConfigurableApplicationContext context, String... initializerClassNames) {
if (logger.isDebugEnabled()) {
logger.debug("Initializing ApplicationContext with AOT");
}
for (String initializerClassName : initializerClassNames) {
if (logger.isTraceEnabled()) {
logger.trace("Applying " + initializerClassName);
}
loadInitializer(initializerClassName, context.getClassLoader()).initialize(context);
}
}

@SuppressWarnings("unchecked")
private ApplicationContextInitializer<ConfigurableApplicationContext> loadInitializer(
String className, @Nullable ClassLoader classLoader) {
Object initializer = instantiate(className, classLoader);
if (!(initializer instanceof ApplicationContextInitializer)) {
throw new IllegalArgumentException("Not an ApplicationContextInitializer: " + className);
}
return (ApplicationContextInitializer<ConfigurableApplicationContext>) initializer;
}

private static Object instantiate(String className, @Nullable ClassLoader classLoader) {
try {
Class<?> type = ClassUtils.forName(className, classLoader);
return BeanUtils.instantiateClass(type);
}
catch (Exception ex) {
throw new IllegalArgumentException("Failed to instantiate ApplicationContextInitializer: " + className, ex);
}
AotApplicationContextInitializer.forInitializerClasses(initializerClassNames).initialize(context);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.context.aot;

import java.util.List;

import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.support.GenericApplicationContext;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;

/**
* Tests for {@link AotApplicationContextInitializer}.
*
* @author Stephane Nicoll
*/
class AotApplicationContextInitializerTests {

@Test
void initializeInvokesApplicationContextInitializer() {
GenericApplicationContext context = new GenericApplicationContext();
AotApplicationContextInitializer.forInitializerClasses(
TestApplicationContextInitializer.class.getName())
.initialize(context);
assertThat(context.getBeanDefinitionNames()).containsExactly("test");
}

@Test
void initializeInvokesApplicationContextInitializersInOrder() {
GenericApplicationContext context = new GenericApplicationContext();
AotApplicationContextInitializer.forInitializerClasses(
AnotherApplicationContextInitializer.class.getName(),
TestApplicationContextInitializer.class.getName())
.initialize(context);
assertThat(context.getBeanDefinitionNames()).containsExactly("another", "test");
}

@Test
void initializeWhenClassIsNotApplicationContextInitializerThrowsException() {
GenericApplicationContext context = new GenericApplicationContext();
assertThatIllegalArgumentException()
.isThrownBy(() -> AotApplicationContextInitializer.forInitializerClasses("java.lang.String")
.initialize(context))
.withMessageContaining("not assignable")
.withMessageContaining("ApplicationContextInitializer")
.withMessageContaining("java.lang.String");
}

@Test
void initializeWhenInitializerHasNoDefaultConstructorThrowsException() {
GenericApplicationContext context = new GenericApplicationContext();
assertThatIllegalArgumentException()
.isThrownBy(() -> AotApplicationContextInitializer.forInitializerClasses(
ConfigurableApplicationContextInitializer.class.getName()).initialize(context))
.withMessageContaining("Failed to instantiate ApplicationContextInitializer: ")
.withMessageContaining(ConfigurableApplicationContextInitializer.class.getName());
}

@Test
void getAotInitializersReturnsOnlyAotInitializers() {
ApplicationContextInitializer<GenericApplicationContext> l1 = context -> { };
ApplicationContextInitializer<GenericApplicationContext> l2 = context -> { };
AotApplicationContextInitializer<GenericApplicationContext> a1 = context -> { };
AotApplicationContextInitializer<GenericApplicationContext> a2 = l2::initialize;
List<ApplicationContextInitializer<GenericApplicationContext>> initializers = List.of(l1, l2, a1, a2);
List<AotApplicationContextInitializer<GenericApplicationContext>> aotInitializers = AotApplicationContextInitializer
.getAotInitializers(initializers);
assertThat(aotInitializers).containsExactly(a1, a2);
}


static class TestApplicationContextInitializer implements ApplicationContextInitializer<GenericApplicationContext> {

@Override
public void initialize(GenericApplicationContext applicationContext) {
applicationContext.registerBeanDefinition("test", new RootBeanDefinition());
}

}

static class AnotherApplicationContextInitializer implements ApplicationContextInitializer<GenericApplicationContext> {

@Override
public void initialize(GenericApplicationContext applicationContext) {
applicationContext.registerBeanDefinition("another", new RootBeanDefinition());
}

}

static class ConfigurableApplicationContextInitializer implements ApplicationContextInitializer<GenericApplicationContext> {

public ConfigurableApplicationContextInitializer(ClassLoader classLoader) {
}

@Override
public void initialize(GenericApplicationContext applicationContext) {

}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
*
* @author Stephane Nicoll
*/
@Deprecated
class ApplicationContextAotInitializerTests {

private final ApplicationContextAotInitializer initializer = new ApplicationContextAotInitializer();
Expand All @@ -54,7 +55,8 @@ void initializeFailWithNonApplicationContextInitializer() {
GenericApplicationContext context = new GenericApplicationContext();
assertThatIllegalArgumentException()
.isThrownBy(() -> initializer.initialize(context, "java.lang.String"))
.withMessageContaining("Not an ApplicationContextInitializer: ")
.withMessageContaining("not assignable")
.withMessageContaining("ApplicationContextInitializer")
.withMessageContaining("java.lang.String");
}

Expand Down

0 comments on commit ac13d26

Please sign in to comment.