1.2.5. Service instantiation

Warning

You are looking at documentation for an older release. Not what you want? See the current release documentation.

As you have already learned, the services are all singletons, so that the container creates only one single instance of each service. The services are created by calling the constructors (called constructor injection). If there are only zero-arguments constructors (Foo public Foo(){}), there are no problems to be expected. That is easy.

But now look at OrganizationServiceImpl.java. This JDBC implementation of BaseOrganizationService interface has only one constructor:

public OrganizationServiceImpl(ListenerService listenerService, DatabaseService dbService);

You see this service depends on two other services. In order to be able to call this constructor, the container first needs a ListenerService and a DatabaseService. Therefore, these services must be instantiated before BaseOrganizationService, because BaseOrganizationService depends on them.

For this purpose, the container first looks at the constructors of all services and creates a matrix of service dependencies to call the services in a proper order. If for any reason there are interdependencies or circular dependencies, you will get a Java Exception. In this way, the dependencies are injected by the container.

Note

What happens if one service has more than one constructor? The container always tries first to use the constructor with a maximum number of arguments, if this is not possible the container continues step by step with constructors that have less arguments until arriving at the zero-argument constructor (if any).

Copyright ©. All rights reserved. eXo Platform SAS
blog comments powered byDisqus