0

I'm new to Spring, so I apologize if the question seems to be "funny".

I have my PersonService component:

@Component
public class PersonService {
    private Person ceo;
    private Person vp;
    private Person secretary;

    private ArrayList<Person> db;

    public PersonService(@Autowired @Qualifier("ceo") Person ceo,
                         @Autowired @Qualifier("vp") Person vp,
                         @Autowired @Qualifier("secretary") Person secretary,
                         @Autowired ArrayList<Person> db) {
        this.ceo = ceo;
        this.vp = vp;
        this.secretary = secretary;
        this.db = db;
    }

    public Person getCeo() {
        return ceo;
    }

    public Person getVp() {
        return vp;
    }

    public Person getSecretary() {
        return secretary;
    }

    public void setCeo(Person ceo) {
        this.ceo = ceo;
    }

    public void setVp(Person vp) {
        this.vp = vp;
    }

    public void setSecretary(Person secretary) {
        this.secretary = secretary;
    }

    public List<Person> getDb() {
        return db;
    }

    public void setDb(ArrayList<Person> db) {
        this.db = db;
    }
}

and config method which creates those beans:

@Configuration
public class Config {

    @Bean
    @Qualifier("ceo")
    public Person createCeo() {
        System.out.println("Tworze beana metodą createPerson (prezes)");
        return new Person("1", "Chrystal", "Havoc", "chavocr@yahoo.com", "Mymm");
    }

    @Bean
    @Qualifier("vp")
    public Person createVp() {
        System.out.println("Tworze beana metodą createPerson (wiceprezes)");
        return new Person("2", "Halley", "Gadaud", "hgadaud9@sohu.com", "Oyope");
    }

    @Bean
    @Qualifier("secretary")
    public Person createSecretary() {
        return new Person("3", "Kirbie", "Wrettum", "kwrettumj@slideshare.net", "Browsetype");
    }

    @Bean
    public ArrayList<Person> createOtherPeople() throws IOException {
      ....
      return people
    }

The method createOtherPeople is supposed to return about 30 people from csv file. The problem is that if I change the returned type from ArrayList<Person> to List<Person> and do this:

PersonService personService = applicationContext.getBean(PersonService.class);
System.out.println(personService.getCeo());
System.out.println(personService.getVp());
System.out.println(personService.getSecretary());
System.out.println(personService.getDb());

getDb() returns not those 30 people, but 3 people that are ceo, vp, secretary. Why is that happening? Why does Spring apply those three people which are created in other methods: createCeo, createVp to my db which is supposed to return 30 people?

I can also add, that if I add @Qualifier for my db if it's List<Person> it works well, but as there is only one method returning List<Person> I should not be forced to add @Qualifier or do I misunderstand something?

funnyguy
  • 107
  • 1
  • 6
  • 2
    You can't assign a `List` to an `ArrayList`. – xehpuk Nov 08 '22 at 18:10
  • Spring will automatically create a list of beans for the same entity. Since you are Qualifying `Person` three different ways, the application context will automatically add a `List` of beans of that type, in this case `Person`. Since you didn't specify (didn't use qualifier for your `db` object) spring will default to that `List` instead. Also, always use the interface instead of the specific type, it is a better practice. – Jorge Campos Nov 08 '22 at 18:16
  • @xehpuk what I meant is that if I replace all ArrayList usages by List, it breaks. – funnyguy Nov 08 '22 at 18:22
  • @JorgeCampos so what would you suggest at this point? Always use Qualifier or? I don't understand the sense of interfaces in this case yet. As I understand, creating those three people one by one creates "in cloud" a List of Persons? – funnyguy Nov 08 '22 at 18:23
  • 1
    For this particular case, yes, you do need to "qualify" your `db` to point to the DB bean you created (the type here is kind irrelevant really) because of the way Spring itself works. Regarding the "in cloud" statement: yes, but the correct assertion to it is it creates a "List of a qualified type" in your case `Person` that has three "different" usages for it. Regarding the interface, read this post, it may give you some clarification: https://stackoverflow.com/q/383947/460557 – Jorge Campos Nov 08 '22 at 18:37
  • @funnyguy I see. For the IOC behavior for collections, see [`@Autowired`](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/beans/factory/annotation/Autowired.html). You should be able to use your bean if you give the parameter the same name (`createOtherPeople`), I think. – xehpuk Nov 08 '22 at 19:19

0 Answers0