Skip to content
This repository has been archived by the owner on Feb 23, 2023. It is now read-only.

Commit

Permalink
Polish "Handle bean initialization methods"
Browse files Browse the repository at this point in the history
Rather than invoking the initialization callback as part of the instance
supplier, we should rather post-process the bean so that we can provide
the same ordering as the regular runtime.

See gh-1059
  • Loading branch information
snicoll committed Sep 27, 2021
1 parent 26377c8 commit 83a7ef1
Show file tree
Hide file tree
Showing 11 changed files with 6 additions and 396 deletions.
11 changes: 5 additions & 6 deletions spring-aot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@
<artifactId>picocli</artifactId>
</dependency>

<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
Expand All @@ -63,12 +68,6 @@
<artifactId>asm-tree</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>jsr250-api</artifactId>
<version>1.0</version>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,12 @@
import org.springframework.context.bootstrap.generator.bean.descriptor.BeanInstanceDescriptor;
import org.springframework.context.bootstrap.generator.bean.descriptor.BeanInstanceDescriptor.InstanceCallback;
import org.springframework.context.bootstrap.generator.bean.descriptor.BeanInstanceDescriptor.MemberDescriptor;
import org.springframework.context.bootstrap.generator.bean.support.MethodInvocationWriter;
import org.springframework.util.ClassUtils;

/**
* Write the necessary statements to instantiate a bean.
*
* @author Stephane Nicoll
* @author Christoph Strobl
*/
class DefaultBeanInstanceSupplierWriter {

Expand Down Expand Up @@ -102,9 +100,6 @@ private void writeBeanInstantiation(Builder code, Constructor<?> constructor) {
for (MemberDescriptor<?> injectionPoint : this.descriptor.getInjectionPoints()) {
code.add(this.injectionPointWriter.writeInjection(injectionPoint.getMember(), injectionPoint.isRequired())).add(";\n");
}
for(MemberDescriptor<Method> initMethod : this.descriptor.getInitializationMethods()) {
code.add(MethodInvocationWriter.writeMethodInvocationOn("bean", initMethod.getMember()));
}
if (multiStatements) {
code.add("return bean;\n");
code.unindent().add("}");
Expand Down Expand Up @@ -144,9 +139,6 @@ private void writeBeanInstantiation(Builder code, Method method) {
for (MemberDescriptor<?> injectionPoint : this.descriptor.getInjectionPoints()) {
code.add(this.injectionPointWriter.writeInjection(injectionPoint.getMember(), injectionPoint.isRequired())).add(";\n");
}
for(MemberDescriptor<Method> initMethod : this.descriptor.getInitializationMethods()) {
code.add(MethodInvocationWriter.writeMethodInvocationOn("bean", initMethod.getMember()));
}
if (multiStatements) {
code.add("return bean;\n");
code.unindent().add("}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
* Describe how an instance of a bean can be supplied.
*
* @author Stephane Nicoll
* @author Christoph Strobl
*/
public final class BeanInstanceDescriptor {

Expand All @@ -48,15 +47,12 @@ public final class BeanInstanceDescriptor {

private final List<PropertyDescriptor> properties;

private final List<MemberDescriptor<Method>> initializationMethods;

private BeanInstanceDescriptor(Builder builder) {
this.beanType = builder.beanType;
this.instanceCreator = builder.instanceCreator;
this.instanceCallbacks = new ArrayList<>(builder.instanceCallbacks);
this.injectionPoints = new ArrayList<>(builder.injectionPoints);
this.properties = new ArrayList<>(builder.properties);
this.initializationMethods = new ArrayList<>(builder.initializationMethods);
}

/**
Expand Down Expand Up @@ -129,14 +125,6 @@ public List<PropertyDescriptor> getProperties() {
return this.properties;
}

/**
* Return the {@link MemberDescriptor methods} that should be honored once to initialize the bean after creation.
* @return the initialization callbacks, if any. Never {@literal null}.
*/
public List<MemberDescriptor<Method>> getInitializationMethods() {
return initializationMethods;
}

/**
* Describe a {@link Member} that is used to initialize a Bean instance.
* @param <T> the member type
Expand Down Expand Up @@ -214,8 +202,6 @@ public static class Builder {

private final List<PropertyDescriptor> properties = new ArrayList<>();

private final List<MemberDescriptor<Method>> initializationMethods = new ArrayList<>();

Builder(ResolvableType beanType) {
Assert.notNull(beanType, "BeanType must not be null");
this.beanType = beanType;
Expand Down Expand Up @@ -256,16 +242,6 @@ public Builder withProperties(List<PropertyDescriptor> propertyValues) {
return this;
}

public Builder withInitMethods(List<MemberDescriptor<Method>> initMethods) {
this.initializationMethods.addAll(initMethods);
return this;
}

public Builder withInitMethod(MemberDescriptor<Method> initMethod) {
this.initializationMethods.add(initMethod);
return this;
}

public BeanInstanceDescriptor build() {
return new BeanInstanceDescriptor(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.springframework.context.bootstrap.generator.bean.descriptor;

import java.lang.reflect.Executable;
import java.lang.reflect.Method;
import java.util.List;

import org.springframework.beans.factory.config.BeanDefinition;
Expand All @@ -32,7 +31,6 @@
* framework contract to detect the members to use to fully instantiate a bean.
*
* @author Stephane Nicoll
* @author Christoph Strobl
*/
public class DefaultBeanInstanceDescriptorFactory implements BeanInstanceDescriptorFactory {

Expand All @@ -44,14 +42,11 @@ public class DefaultBeanInstanceDescriptorFactory implements BeanInstanceDescrip

private final PropertiesSupplier propertiesSupplier;

private final InitializationMethodSupplier initializationMethodSupplier;

public DefaultBeanInstanceDescriptorFactory(ConfigurableBeanFactory beanFactory) {
this.instanceCreatorSupplier = new BeanInstanceExecutableSupplier(beanFactory);
this.instanceCallbacksSupplier = new InstanceCallbacksSupplier();
this.injectionPointsSupplier = new InjectionPointsSupplier(beanFactory.getBeanClassLoader());
this.propertiesSupplier = new PropertiesSupplier();
this.initializationMethodSupplier = new InitializationMethodSupplier();
}

@Override
Expand All @@ -63,12 +58,9 @@ public BeanInstanceDescriptor create(BeanDefinition beanDefinition) {
List<InstanceCallback> instanceCallbacks = this.instanceCallbacksSupplier.detectInstanceCallbacks(beanType);
List<MemberDescriptor<?>> injectionPoints = this.injectionPointsSupplier.detectInjectionPoints(beanType);
List<PropertyDescriptor> properties = this.propertiesSupplier.detectProperties(beanDefinition);
List<MemberDescriptor<Method>> initializationCallbacks = this.initializationMethodSupplier.getInstanceCallbacks(beanDefinition);
return BeanInstanceDescriptor.of(beanDefinition.getResolvableType())
.withInstanceCreator(instanceCreator).withInstanceCallbacks(instanceCallbacks)
.withInjectionPoints(injectionPoints).withProperties(properties)
.withInitMethods(initializationCallbacks)
.build();
.withInjectionPoints(injectionPoints).withProperties(properties).build();
}
return null;
}
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
Expand All @@ -24,7 +23,6 @@
* Register the reflection entries for each {@link BeanInstanceDescriptor}.
*
* @author Stephane Nicoll
* @author Christoph Strobl
*/
class DefaultBeanNativeConfigurationProcessor implements BeanNativeConfigurationProcessor, BeanFactoryAware {

Expand Down Expand Up @@ -62,16 +60,6 @@ else if (member instanceof Field) {
processInnerBeanDefinition((BeanDefinition) value, registry);
}
}
/*
* Register required reflection configuration for private bean initialization (post construct) methods
* that need to be called via ReflectionUtils.
*/
for (MemberDescriptor<Method> callback : descriptor.getInitializationMethods()) {
Method targetMethod = callback.getMember();
if (Modifier.isPrivate(targetMethod.getModifiers())) {
registry.reflection().forType(targetMethod.getDeclaringClass()).withMethods(targetMethod);
}
}
}

private void processInnerBeanDefinition(BeanDefinition beanDefinition, NativeConfigurationRegistry registry) {
Expand Down
Loading

0 comments on commit 83a7ef1

Please sign in to comment.