Skip to content
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

Map<String, Object> as a @Bean not working as expected even with a @Qualifier when autowired. #33537

Closed
vikuwo opened this issue Sep 13, 2024 · 5 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: bug A general bug
Milestone

Comments

@vikuwo
Copy link

vikuwo commented Sep 13, 2024

Affects: 6.1.0


Code:


import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@SpringBootApplication
public class Main implements CommandLineRunner {

  @Configuration
  public static class Config {

    @Bean("qualifiedMap")
    Map<String, Object> qualifiedMap() {
      return Map.of("k1", "v1", "k2", "v2", "k3", "v3");
    }

  }

  @Autowired
  @Qualifier("qualifiedMap")
  private Map<String, Object> qualifiedMap;

  @Autowired
  private ApplicationContext applicationContext;
  
  @Override
  public void run(String... args) {
    System.out.printf("Autowired Bean: %s%n", qualifiedMap);
    System.out.printf("Context Bean: %s%n", applicationContext.getBean("qualifiedMap", Map.class));
  }

  public static void main(String[] args) {
    SpringApplication.run(Main.class, args);
  }

}

Output:


Autowired Bean: {qualifiedMap={k1=v1, k3=v3, k2=v2}}
Context Bean: {k1=v1, k3=v3, k2=v2}

When we mark a Map<String, Object> as @bean with a qualifier, @Autowired refers to a different map. The bean seems fine when directly fetched from the application context.

@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 the in: core Issues in core modules (aop, beans, core, context, expression) label Sep 13, 2024
@jhoeller jhoeller self-assigned this Sep 13, 2024
@liua1004
Copy link

liua1004 commented Sep 13, 2024

It will be fine to use @Resource, @Autowired will find candidate by type.

Map, Collection(include Set and List) and Array are different when autowiring. They will find bean which has the same type as the element they storage.

@vikuwo
Copy link
Author

vikuwo commented Sep 14, 2024

But this shouldn't be the case when we use @Autowire along with a @Qualifier. It should autowire the exact bean. That way, if I have the multiple Maps or Collections of the same element type, I would still be able to autowire a specific bean.

@liua1004
Copy link

It depends on how to understand the annotation Qualifier. I think that it just provides a candidate, but not change the autowire logic. Maybe I am wrong.

@vikuwo
Copy link
Author

vikuwo commented Sep 14, 2024

At least the below statements should produce the same result. Otherwise the behaviour is not consistent.

    System.out.printf("Autowired Bean: %s%n", qualifiedMap);
    System.out.printf("Context Bean: %s%n", applicationContext.getBean("qualifiedMap", Map.class));

@jhoeller
Copy link
Contributor

jhoeller commented Sep 20, 2024

As far as I can tell, this works as of 6.2 due to #28122 - as a welcome side effect of that algorithmic optimization. Feel free to try the recently released 6.2.0-RC1 to double-check.

@jhoeller jhoeller added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Sep 26, 2024
@jhoeller jhoeller added this to the 6.2.0-M1 milestone Sep 26, 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: bug A general bug
Projects
None yet
Development

No branches or pull requests

4 participants