-
Notifications
You must be signed in to change notification settings - Fork 38.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Factory method is lost for AOT-processed beans that do not require autowiring #28748
Comments
I believe that is a regression of how BeanDefinitions are registered. Previously, we would use the resolved factory method as an input for the bean definition and I think this has been lost for beans that do not need autowiring as it seems to be autowiring specific now. |
paging @philwebb |
So our plan is to change the signature of the method that provides a bean instance so that it has to provide the Rather than returning the bean instance and using /**
* Create the bean instance supplier for 'restTemplateClientService'.
*/
private static BeanInstanceSupplier getRestTemplateClientServiceInstanceSupplier() {
return BeanInstanceSupplier
.forConstructor(RestTemplateBuilder.class)
.withGenerator(args -> new RestTemplateClientService(args.get(0)));
} |
Unfortunately, the fix breaks other use cases as the supplier is composed in the caller and we now return a InstanceSupplier<WebMvcAutoConfiguration.EnableWebMvcConfiguration> instanceSupplier = WebMvcAutoConfiguration_EnableWebMvcConfiguration__BeanDefinitions.getEnableWebMvcConfigurationInstanceSupplier();
instanceSupplier = instanceSupplier.andThen(WebMvcAutoConfiguration_EnableWebMvcConfiguration__Autowiring::apply); Thanks @OlgaMaciaszek for the report! |
This sample showcases the problem: package org.springframework;
import org.springframework.beans.factory.aot.BeanInstanceSupplier;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.InstanceSupplier;
import org.springframework.beans.factory.support.RegisteredBean;
import org.springframework.beans.factory.support.RootBeanDefinition;
public class Sample {
/**
* Get the bean definition for 'sampleConfiguration'
*/
public static BeanDefinition getSampleConfigurationBeanDefinition() {
Class<?> beanType = SampleConfiguration.class;
RootBeanDefinition beanDefinition = new RootBeanDefinition(beanType);
beanDefinition.setAttribute("org.springframework.context.annotation.ConfigurationClassPostProcessor.configurationClass", "lite");
InstanceSupplier<SampleConfiguration> instanceSupplier = getSampleConfigurationInstanceSupplier();
instanceSupplier = instanceSupplier.andThen(SampleConfiguration__Autowiring::apply);
beanDefinition.setInstanceSupplier(instanceSupplier);
return beanDefinition;
}
private static BeanInstanceSupplier getSampleConfigurationInstanceSupplier() {
return BeanInstanceSupplier.forConstructor().withGenerator(SampleConfiguration::new);
}
static class SampleConfiguration {
}
static class SampleConfiguration__Autowiring {
static SampleConfiguration apply(RegisteredBean registeredBean, SampleConfiguration sampleConfiguration) {
// autowiring stuff
return sampleConfiguration;
}
}
} |
|
Update `InstanceSupplier.andThen` to propagate the result of `getFactoryMethod()`. See gh-28748
I've updated |
Update `BeanInstanceSupplier` to support a generic type. See gh-28748
I've added a generic to |
This commit also includes partial work to ensure framework works in full native/AOT mode. Also, PollableBean remains and will simply not work in AOT mode until spring-projects/spring-framework#28748 is resolved. That said, i will be deprecating t and it will be removed in the next release given that we already have configurable alternative Resolves #2456
Phil and I discussed this and agreed to close this issue as the reported problem is fixed. We've brainstormed on how we could make it better, see #28875. |
The
ListableBeanFactory
has these methods:Which are useful in cases like this:
After AOT has processed those beans and generated respective functional substitutions, there is no annotation info on bean definition anymore: an instance supplier just calls the target factory method via lambda:
I workaround it with some
Registration
bean definition from theBeanDefinitionRegistryPostProcessor
at the moment and that's probably will let me to avoid some potential reflection for those annotations at runtime, but I believe there might be some use-cases when the mentionedBeanFactory
API is called at runtime.The text was updated successfully, but these errors were encountered: