A.java
package com.jc.beans;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
public class A {
@Qualifier("b1")
private B b;
public void setB(B b) {
this.b = b;
}
public void start() {
System.out.println("starting..");
}
@Override
public String toString() {
return "A [b=" + b + "]";
}
}
B.java
package com.jc.beans;
public class B {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "B [id=" + id + ", name=" + name + "]";
}
}
AppConfig.java
package com.jc.test;
import com.jc.beans.A;
import com.jc.beans.B;
@Configuration
@PropertySource(value = "values.properties")
@ComponentScan(basePackages = "com.jc.beans")
public class AppConfig {
@Autowired
private Environment environment;
@Bean(name = "b1")
public B newB1() {
B b = new B();
b.setId(10);
b.setName("Anuj");
return b;
}
@Bean(name = "b2")
public B newB2() {
B b = new B();
b.setId(20);
b.setName("Awasthi");
return b;
}
@Bean(name = "a", autowire = Autowire.BY_NAME)
public A newA() {
A a = new A();
return a;
}
}