Skip to content

Autowiring of generic beans with array types is broken since 6.2.0-M1 #33535

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

Closed
genuss opened this issue Sep 13, 2024 · 0 comments
Closed

Autowiring of generic beans with array types is broken since 6.2.0-M1 #33535

genuss opened this issue Sep 13, 2024 · 0 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: regression A bug that is also a regression
Milestone

Comments

@genuss
Copy link
Contributor

genuss commented Sep 13, 2024

Affects: 6.2.0-SNAPSHOT. It starts on 6.2.0-M1

Let's start with a MRE:

package com.example.demo;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

class Demo {
  public static void main(String[] args) {
    var context = new AnnotationConfigApplicationContext();
    context.registerBean("genericBean", GenericClass.class, GenericClass::new);
    context.registerBean("objectGenericConsumer", ObjectConsumer.class);

    context.registerBean("arrayGenericConsumer", ArrayConsumer.class); // This is the problematic line

    context.refresh();
  }
}

class GenericClass<T> {}
record ObjectConsumer(GenericClass<Object> genericClass) {}
record ArrayConsumer(GenericClass<byte[]> genericClass) {}

Here a bean is registered as GenericClass<?>. Later it is autowired as a dependency of other beans. It works when a dependency is defined as GenericClass<Object> but doesn't work for arrays. I'm using byte[] as an example, but any array won't work including Object arrays. I suppose it's a bug as this example works on earlier versions.

An example when it might be important

Spring boot registers KafkaTemplate<?,?> bean as it can be seen in KafkaAutoConfiguration. When used it's declared with concrete generic parameters defined by used key and value serializers. Value serializer org.apache.kafka.common.serialization.ByteArraySerializer works with byte arrays, so requires the second generic parameter to be byte[].

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Sep 13, 2024
@jhoeller jhoeller added type: regression A bug that is also a regression in: core Issues in core modules (aop, beans, core, context, expression) and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Sep 13, 2024
@jhoeller jhoeller self-assigned this Sep 13, 2024
@jhoeller jhoeller added this to the 6.2.0-RC2 milestone Sep 13, 2024
@jhoeller jhoeller changed the title Autowiring of generic beans is broken since 6.2.0-M1 Autowiring of generic beans with array types is broken since 6.2.0-M1 Sep 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: regression A bug that is also a regression
Projects
None yet
Development

No branches or pull requests

3 participants