-
Notifications
You must be signed in to change notification settings - Fork 38.4k
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
Configuration class with Bean factory method on an interface generates wrong target with AOT #32609
Comments
@MartinLei thanks for the sample but the README instructs to list a file without additional information. The app starts fine so I am not really sure what you mean. |
Thank @snicoll for your prompt feedback. I hope this makes it more clear :) |
This is working fine with AOT on the JVM. It does fail on native with:
|
I've made good progress on this one with AOT now recognizing the default method from the parent and generating the appropriate code for the target class, e.g. /**
* Bean definitions for {@link BeanConfig}.
*/
@Generated
public class BeanConfig__BeanDefinitions {
/**
* Get the bean definition for 'beanConfig'.
*/
public static BeanDefinition getBeanConfigBeanDefinition() {
RootBeanDefinition beanDefinition = new RootBeanDefinition(BeanConfig.class);
beanDefinition.setTargetType(BeanConfig.class);
ConfigurationClassUtils.initializeConfigurationClass(BeanConfig.class);
beanDefinition.setInstanceSupplier(BeanConfig$$SpringCGLIB$$0::new);
return beanDefinition;
}
/**
* Get the bean instance supplier for 'createACar'.
*/
private static BeanInstanceSupplier<Car> getCreateACarInstanceSupplier() {
return BeanInstanceSupplier.<Car>forFactoryMethod(BeanConfig.class, "createACar")
.withGenerator((registeredBean) -> registeredBean.getBeanFactory().getBean(BeanConfig.class).createACar());
}
/**
* Get the bean definition for 'createACar'.
*/
public static BeanDefinition getCreateACarBeanDefinition() {
RootBeanDefinition beanDefinition = new RootBeanDefinition(Car.class);
beanDefinition.setInstanceSupplier(getCreateACarInstanceSupplier());
return beanDefinition;
}
} Unfortunately, it still does not work as the core container attempts to create a CGLIB proxy for the interface. It should not and that probably deserves its own issue. |
Previously, if a factory method is defined on a parent, the generated code would blindly use the method's declaring class for both the target of the generated code, and the signature of the method. This commit improves the resolution by considering the factory metadata in the BeanDefinition. Closes spring-projectsgh-32609
@MartinLei that fixed the sample you've shared but please give that a try on your project when you get a chance. |
I checked it on the real project. It works like a charm. Merci @snicoll |
Spring-Boot version 3.2.4
The processAot task does not generate the right BeanDefinitions files, if a configuration class uses composition of beans trough interfaces.
See showcase project
The text was updated successfully, but these errors were encountered: