Skip to content

Add Bean instantiation generator infrastructure #28047

Closed
@snicoll

Description

@snicoll

As part of our AOT effort, we need an infrastructure that lets us generate the code to instantiate a bean.

This includes some low-level code generation infrastructure to write method calls, parameters, etc, as well as an API that can be used to contribute to the bean instance.

Such contributors should namely be based on the existing BeanPostProcessor infrastructure as they augment a bean instance with some logic that can be translated into code during the AOT phase. As BPP are ordered, so can their contributions so that the order in which they are applied by executing generated code matches.

To ease code generation, we need an infrastructure that focuses on something quite basic for a first version, something like:

BeanDefinitionRegistrar.of("restTemplateClientService", RestTemplateClientService.class)
	  .withConstructor(RestTemplateBuilder.class)
	  .instanceSupplier((instanceContext) -> instanceContext.create(beanFactory, (attributes) ->
			  new RestTemplateClientService(attributes.get(0))))
	  .register(beanFactory);

This registeres a restTemplateClientService bean that requires a RestTemplateBuilder. Rather than doing all the dependency resolution at build-time, we leverage framework's dependency resolution algorithm, via the instanceContext who can provide us resolved attributes according to Executable to use to instantiate the bean (here a constructor that takes a RestTemplateBuilder).

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)theme: aotAn issue related to Ahead-of-time processingtype: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions