Skip to content

Explicit BeanDefinition#targetType is not honoured in AOT scenarios #30689

@snicoll

Description

@snicoll

Courtesy of @wilkinsona https://github.com/snicoll-scratches/lost-target-type

To reproduce:

./gradlew bootJar
java -Dspring.aot.enabled=true -jar build/libs/lost-target-type-0.0.1-SNAPSHOT.jar

The use case is registering a bean definition on the fly with the factory method of another bean definition, but perhaps this can be generalized.

The bean definition looks like this:

public static BeanDefinition getTwoBeanDefinition() {
  Class<?> beanType = LostTargetTypeApplication.Two.class;
  RootBeanDefinition beanDefinition = new RootBeanDefinition(beanType);
  beanDefinition.setInstanceSupplier(getTwoInstanceSupplier());
  return beanDefinition;
}

Its instance supplier refers to a method that's going to produce a bean of type One. One and Two are unrelated and the factory method produces an instance that implements them both.

It looks like the bean definition's bean class is ignored in favor of the bean factory method. The following works:

public static BeanDefinition getTwoBeanDefinition() {
  Class<?> beanType = LostTargetTypeApplication.Two.class;
  RootBeanDefinition beanDefinition = new RootBeanDefinition(beanType);
  beanDefinition.setInstanceSupplier(getTwoInstanceSupplier());
  beanDefinition.setTargetType(beanType);
  return beanDefinition;
}

We should review this use case and see whether AOT has to generate a more precise definition or if the context should fallback first on the bean class.

Metadata

Metadata

Assignees

Labels

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

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions