I have two beans, Parent and Child. Child bean I have declared as of Protoype scope.
I want new child object is used to call any child's method in the Parent class. For eg. in the below example,I want statement 1 calls method sayHi on different child object and statement 2 calls sayHi1 on different child object.
One way is to implement ApplicationContextAware and get new child object using context.getBean("")
before calling any child's method. But i don't want to do that.
Is there any other alternative?
@Component
public class Parent{
@Autowired
Child child;
public void sayHello(){
child.sayHi(); -------------- (1)
}
public void sayHello1(){
child.sayHi1(); --------------- (2)
}
}
@Component
@Scope(value=BeanDefinition.SCOPE_PROTOTYPE)
public class Child{
public void sayHi(){
System.out.println("Hi Spring 3.0");
}
public void sayHi1(){
System.out.println("Hi1 Spring 3.0 ");
}
}