Skip to content

Commit

Permalink
Polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Aug 5, 2022
1 parent 2cc4486 commit 853407b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@
public class ApplicationContextAotGenerator {

/**
* Process the specified {@link GenericApplicationContext} instance
* ahead-of-time using the specified {@link GenerationContext}.
* Process the specified {@link GenericApplicationContext} ahead-of-time using
* the specified {@link GenerationContext}.
* <p>Return the {@link ClassName} of the {@link ApplicationContextInitializer}
* to use to restore an optimized state of the application context.
* @param applicationContext the non-refreshed application context to handle
* @param applicationContext the non-refreshed application context to process
* @param generationContext the generation context to use
* @return the class name of the {@link ApplicationContextInitializer} entry point
* @return the {@code ClassName} of the {@code ApplicationContextInitializer}
* entry point
*/
public ClassName processAheadOfTime(GenericApplicationContext applicationContext,
GenerationContext generationContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,12 @@
import org.springframework.javapoet.TypeSpec;

/**
* Internal code generator to create the application context initializer.
* Internal code generator to create the {@link ApplicationContextInitializer}.
*
* @author Phillip Webb
* @since 6.0
*/
class ApplicationContextInitializationCodeGenerator
implements BeanFactoryInitializationCode {
class ApplicationContextInitializationCodeGenerator implements BeanFactoryInitializationCode {

private static final String INITIALIZE_METHOD = "initialize";

Expand Down Expand Up @@ -70,13 +69,11 @@ private void generateType(TypeSpec.Builder type) {
type.addMethod(generateInitializeMethod());
}


private MethodSpec generateInitializeMethod() {
MethodSpec.Builder method = MethodSpec.methodBuilder(INITIALIZE_METHOD);
method.addAnnotation(Override.class);
method.addModifiers(Modifier.PUBLIC);
method.addParameter(GenericApplicationContext.class,
APPLICATION_CONTEXT_VARIABLE);
method.addParameter(GenericApplicationContext.class, APPLICATION_CONTEXT_VARIABLE);
method.addCode(generateInitializeCode());
return method.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,40 +57,31 @@ class ApplicationContextAotGeneratorTests {
@Test
void generateApplicationContextWhenHasSimpleBean() {
GenericApplicationContext applicationContext = new GenericApplicationContext();
applicationContext.registerBeanDefinition("test",
new RootBeanDefinition(SimpleComponent.class));
applicationContext.registerBeanDefinition("test", new RootBeanDefinition(SimpleComponent.class));
testCompiledResult(applicationContext, (initializer, compiled) -> {
GenericApplicationContext freshApplicationContext = toFreshApplicationContext(
initializer);
assertThat(freshApplicationContext.getBeanDefinitionNames())
.containsOnly("test");
assertThat(freshApplicationContext.getBean("test"))
.isInstanceOf(SimpleComponent.class);
GenericApplicationContext freshApplicationContext = toFreshApplicationContext(initializer);
assertThat(freshApplicationContext.getBeanDefinitionNames()).containsOnly("test");
assertThat(freshApplicationContext.getBean("test")).isInstanceOf(SimpleComponent.class);
});
}

@Test
void generateApplicationContextWhenHasAutowiring() {
GenericApplicationContext applicationContext = new GenericApplicationContext();
applicationContext.registerBeanDefinition(
AnnotationConfigUtils.AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME,
applicationContext.registerBeanDefinition(AnnotationConfigUtils.AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME,
BeanDefinitionBuilder
.rootBeanDefinition(AutowiredAnnotationBeanPostProcessor.class)
.setRole(BeanDefinition.ROLE_INFRASTRUCTURE).getBeanDefinition());
applicationContext.registerBeanDefinition("autowiredComponent",
new RootBeanDefinition(AutowiredComponent.class));
.rootBeanDefinition(AutowiredAnnotationBeanPostProcessor.class)
.setRole(BeanDefinition.ROLE_INFRASTRUCTURE).getBeanDefinition());
applicationContext.registerBeanDefinition("autowiredComponent", new RootBeanDefinition(AutowiredComponent.class));
applicationContext.registerBeanDefinition("number",
BeanDefinitionBuilder.rootBeanDefinition(Integer.class, "valueOf")
.addConstructorArgValue("42").getBeanDefinition());
BeanDefinitionBuilder
.rootBeanDefinition(Integer.class, "valueOf")
.addConstructorArgValue("42").getBeanDefinition());
testCompiledResult(applicationContext, (initializer, compiled) -> {
GenericApplicationContext freshApplicationContext = toFreshApplicationContext(
initializer);
assertThat(freshApplicationContext.getBeanDefinitionNames())
.containsOnly("autowiredComponent", "number");
AutowiredComponent bean = freshApplicationContext
.getBean(AutowiredComponent.class);
assertThat(bean.getEnvironment())
.isSameAs(freshApplicationContext.getEnvironment());
GenericApplicationContext freshApplicationContext = toFreshApplicationContext(initializer);
assertThat(freshApplicationContext.getBeanDefinitionNames()).containsOnly("autowiredComponent", "number");
AutowiredComponent bean = freshApplicationContext.getBean(AutowiredComponent.class);
assertThat(bean.getEnvironment()).isSameAs(freshApplicationContext.getEnvironment());
assertThat(bean.getCounter()).isEqualTo(42);
});
}
Expand All @@ -101,17 +92,14 @@ void generateApplicationContextWhenHasInitDestroyMethods() {
applicationContext.registerBeanDefinition(
AnnotationConfigUtils.COMMON_ANNOTATION_PROCESSOR_BEAN_NAME,
BeanDefinitionBuilder
.rootBeanDefinition(CommonAnnotationBeanPostProcessor.class)
.setRole(BeanDefinition.ROLE_INFRASTRUCTURE).getBeanDefinition());
.rootBeanDefinition(CommonAnnotationBeanPostProcessor.class)
.setRole(BeanDefinition.ROLE_INFRASTRUCTURE).getBeanDefinition());
applicationContext.registerBeanDefinition("initDestroyComponent",
new RootBeanDefinition(InitDestroyComponent.class));
testCompiledResult(applicationContext, (initializer, compiled) -> {
GenericApplicationContext freshApplicationContext = toFreshApplicationContext(
initializer);
assertThat(freshApplicationContext.getBeanDefinitionNames())
.containsOnly("initDestroyComponent");
InitDestroyComponent bean = freshApplicationContext
.getBean(InitDestroyComponent.class);
GenericApplicationContext freshApplicationContext = toFreshApplicationContext(initializer);
assertThat(freshApplicationContext.getBeanDefinitionNames()).containsOnly("initDestroyComponent");
InitDestroyComponent bean = freshApplicationContext.getBean(InitDestroyComponent.class);
assertThat(bean.events).containsExactly("init");
freshApplicationContext.close();
assertThat(bean.events).containsExactly("init", "destroy");
Expand All @@ -124,33 +112,27 @@ void generateApplicationContextWhenHasMultipleInitDestroyMethods() {
applicationContext.registerBeanDefinition(
AnnotationConfigUtils.COMMON_ANNOTATION_PROCESSOR_BEAN_NAME,
BeanDefinitionBuilder
.rootBeanDefinition(CommonAnnotationBeanPostProcessor.class)
.setRole(BeanDefinition.ROLE_INFRASTRUCTURE).getBeanDefinition());
RootBeanDefinition beanDefinition = new RootBeanDefinition(
InitDestroyComponent.class);
.rootBeanDefinition(CommonAnnotationBeanPostProcessor.class)
.setRole(BeanDefinition.ROLE_INFRASTRUCTURE).getBeanDefinition());
RootBeanDefinition beanDefinition = new RootBeanDefinition(InitDestroyComponent.class);
beanDefinition.setInitMethodName("customInit");
beanDefinition.setDestroyMethodName("customDestroy");
applicationContext.registerBeanDefinition("initDestroyComponent", beanDefinition);
testCompiledResult(applicationContext, (initializer, compiled) -> {
GenericApplicationContext freshApplicationContext = toFreshApplicationContext(
initializer);
assertThat(freshApplicationContext.getBeanDefinitionNames())
.containsOnly("initDestroyComponent");
InitDestroyComponent bean = freshApplicationContext
.getBean(InitDestroyComponent.class);
GenericApplicationContext freshApplicationContext = toFreshApplicationContext(initializer);
assertThat(freshApplicationContext.getBeanDefinitionNames()).containsOnly("initDestroyComponent");
InitDestroyComponent bean = freshApplicationContext.getBean(InitDestroyComponent.class);
assertThat(bean.events).containsExactly("customInit", "init");
freshApplicationContext.close();
assertThat(bean.events).containsExactly("customInit", "init", "customDestroy",
"destroy");
assertThat(bean.events).containsExactly("customInit", "init", "customDestroy", "destroy");
});
}

@Test
void generateApplicationContextWhenHasNoAotContributions() {
GenericApplicationContext applicationContext = new GenericApplicationContext();
testCompiledResult(applicationContext, (initializer, compiled) -> {
GenericApplicationContext freshApplicationContext = toFreshApplicationContext(
initializer);
GenericApplicationContext freshApplicationContext = toFreshApplicationContext(initializer);
assertThat(freshApplicationContext.getBeanDefinitionNames()).isEmpty();
assertThat(compiled.getSourceFile()).contains(
"beanFactory.setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver())");
Expand All @@ -163,8 +145,7 @@ void generateApplicationContextWhenHasBeanFactoryInitializationAotProcessorExclu
applicationContext.registerBeanDefinition("test",
new RootBeanDefinition(NoOpBeanFactoryInitializationAotProcessor.class));
testCompiledResult(applicationContext, (initializer, compiled) -> {
GenericApplicationContext freshApplicationContext = toFreshApplicationContext(
initializer);
GenericApplicationContext freshApplicationContext = toFreshApplicationContext(initializer);
assertThat(freshApplicationContext.getBeanDefinitionNames()).isEmpty();
});
}
Expand All @@ -175,8 +156,7 @@ void generateApplicationContextWhenHasBeanRegistrationAotProcessorExcludesProces
applicationContext.registerBeanDefinition("test",
new RootBeanDefinition(NoOpBeanRegistrationAotProcessor.class));
testCompiledResult(applicationContext, (initializer, compiled) -> {
GenericApplicationContext freshApplicationContext = toFreshApplicationContext(
initializer);
GenericApplicationContext freshApplicationContext = toFreshApplicationContext(initializer);
assertThat(freshApplicationContext.getBeanDefinitionNames()).isEmpty();
});
}
Expand Down Expand Up @@ -205,13 +185,11 @@ static class NoOpBeanFactoryInitializationAotProcessor
implements BeanFactoryPostProcessor, BeanFactoryInitializationAotProcessor {

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
throws BeansException {
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
}

@Override
public BeanFactoryInitializationAotContribution processAheadOfTime(
ConfigurableListableBeanFactory beanFactory) {
public BeanFactoryInitializationAotContribution processAheadOfTime(ConfigurableListableBeanFactory beanFactory) {
return null;
}

Expand All @@ -222,8 +200,7 @@ static class NoOpBeanRegistrationAotProcessor
implements BeanPostProcessor, BeanRegistrationAotProcessor {

@Override
public BeanRegistrationAotContribution processAheadOfTime(
RegisteredBean registeredBean) {
public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean) {
return null;
}

Expand Down

0 comments on commit 853407b

Please sign in to comment.