I am trying to learn constructor injection using Spring 4.3.
I am having class structure is stated below.
@Component
public class Student {
private Address address;
private City city;
public Student(Address address, City city) {
this.address = address;
this.city = city;
}
public Student(City city, Address address) {
this.address = address;
this.city = city;
}
public Student(City city) {
this.city = city;
}
public Student(Address address) {
this.address = address;
}
}
Java Based configuration class:
@Configuration
@ComponentScan(basePackages = "com.spring.ConstructorInjection")
public class StudentConfiguration {
}
Client code:
ApplicationContext context = new AnnotationConfigApplicationContext(StudentConfiguration.class);
Student student = context.getBean(Student.class);
System.out.println(student.getCity());
System.out.println(student.getAddress());
How can I structure my java based configuration class to assure that a specific constructor will be injected?