JBoss.orgCommunity Documentation

Chapter 48. Inversion Of Control

48.1. Overview
48.2. How
48.3. Injection
48.4. Side effects

The services are not responsible for the instantiation of the components they depend on.

This architecture provides a loosely coupled design where the implementation of dependant services can be transparently exchanged.

This pattern has several names :

Not to let the object create itself the instances of the object it references. This job is delegated to the container (assembler in the picture).

There are two ways to inject a dependency :

using a constructor:

public ServiceA(ServiceB serviceB)

using setter methods:

public void setServiceB(ServiceB serviceB)

When a client service can not be stored in the container then the service locator pattern is used:

public ServiceA(){
   this.serviceB =Container.getSInstance().getService(ServiceB.class);
}