12.5.3. Types of Service Dependencies
Services are allowed to declare dependencies on other services using either of 2 approaches:
- @
org.hibernate.service.spi.InjectService
- Any method on the service implementation class accepting a single parameter and annotated with @
InjectService
is considered requesting injection of another service.By default the type of the method parameter is expected to be the service role to be injected. If the parameter type is different than the service role, theserviceRole
attribute of theInjectService
should be used to explicitly name the role.By default injected services are considered required, that is the start up will fail if a named dependent service is missing. If the service to be injected is optional, therequired
attribute of theInjectService
should be declared asfalse
(default istrue
). org.hibernate.service.spi.ServiceRegistryAwareService
- The second approach is a pull approach where the service implements the optional service interface
org.hibernate.service.spi.ServiceRegistryAwareService
which declares a singleinjectServices
method.During startup, Hibernate will inject theorg.hibernate.service.ServiceRegistry
itself into services which implement this interface. The service can then use theServiceRegistry
reference to locate any additional services it needs.