Java Reference
In-Depth Information
public String getMyHelloWorld(){
return hw.getHelloWorld();
}
}
public class HelloWorld implements IHelloWorld{
@Override
public String getHelloWorld() {
return "Hello World";
}
}
The drawback with this approach is that the instance of the HelloWorld class is
created in hard code, which causes a very strong coupling between the managed
bean and implementation of IHeloWorld interface. Therefore, to change IHel-
loWorld implementation, youmusthaveanaccesstothemanagedbeanandmodi-
fy it.
With the CDI, the managed bean will just declare a dependency on an IHel-
loWorld instance and inject it. This gives us the following code:
@ManagedBean
public class MyManagedBean {
@Inject
IHelloWorld hw;
public String getMyHelloWorld(){
return hw.getHelloWorld();
}
}
The CDI will look for the implementation of the IHelloWorld interface, instantiate
and inject it. Better still, the CDI will take care of managing the lifecycle of the bean
that will be injected. Thus, to change the implementation of IHelloWorld interface,
Search WWH ::




Custom Search