Aggregation: If two objects have there own life cycle and not tightly coupled with each other(can exists independently).
Class A {
B b;
}
The default value for b
will be null
and A's
object can exists if there is no instance injected in A.
Composition: if two objects are tightly coupled and one can not exists without second.
Class A{
B b;
A(B b){
this.b = b;
}
}
Spring DI: In spring if we are using @Autowired
annotation over a member then this member is required before instantiation of object.
Class A{
@Autowired */ now this member is required before creating of object of A.
B b;
}
Query: So from the above facts we can think Spring DI
as a composition here .I am asking this question because in a interview I had a discussion over it and as per the interviewer Spring
is using Aggregation
here.
Could any one please clarify if I am missing or not getting some thing here.