spring-native icon indicating copy to clipboard operation
spring-native copied to clipboard

Improve compatibility with Factory Beans

Open snicoll opened this issue 2 years ago • 3 comments

Various samples shed some lights on different use cases around factory beans that we should streamline:

  • Using a ResolvableType on a BeanDefinition that uses a factory bean is problematic as framework's resolution algorithm is not going to apply the same heuristics if a type is set.
  • Us specifying a resolvable type without generics being resolved means framework won't attempt to resolve them by, for example, looking at the return type of the factory method.

Talking to @jhoeller, the best course of action seems to use setBeanClass with the implementation of the FactoryBeancoupled with the OBJECT_TYPE_ATTRIBUTE.

This should help us remove the Spring Data specific code that we have for something more generic.

An extra case should be added as well to support Spring Plugin as it sets both a beanClass and a resolvabletype at the moment and does not use the attribute.

snicoll avatar Sep 14 '21 11:09 snicoll

Something to keep in mind is that FactoryBean could be injected in some cases so we can't lose that type.

snicoll avatar Sep 15 '21 08:09 snicoll

I've pushed a couple of commits but that's just workarounds rather than fixing anything. I'll keep this one open and revisit based on progress made in the framework issue.

snicoll avatar Sep 15 '21 15:09 snicoll

Some experience working on this. Changing the exposed type to be the target type is problematic on several fronts. We lose the ability to inject the factory bean itself if need to be and the AOT inspection uses the "bean type". Switching to the target type of the factory bean means that inspection for autowired methods and fields happen on the target bean, not the factory bean. We'd have to revisit this to be able to separate the two in BeanInstanceDescriptor.

snicoll avatar Nov 04 '21 15:11 snicoll

Spring Native is now superseded by Spring Boot 3 official native support, see the related reference documentation for more details.

As a consequence, I am closing this issue, and recommend trying your use case with latest Spring Boot 3 version. If you still experience the issue reported here, please open an issue directly on the related Spring project (Spring Framework, Data, Security, Boot, Cloud, etc.) with a reproducer.

Thanks for your contribution on the experimental Spring Native project, we hope you will enjoy the official native support introduced by Spring Boot 3.

sdeleuze avatar Jan 02 '23 12:01 sdeleuze