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?