Skip to content
This repository has been archived by the owner on Mar 5, 2020. It is now read-only.

Dependency Injection (DI)

Leon Kiefer edited this page Jan 20, 2019 · 9 revisions

How to use

Dependency Injection manages and injects run-time dependencies like objects. To do this DI have to know which Services exists and the runtime dependencies they have. There are many ways to provide this information, e. g. config files, naming conventions, api, Decorators/Annotations. We use Annotations because they are simple to use and visible in the code.

What is a Service

We are programming in Java and in Java everything is an object, so a Service is also an object. In Java objects have a types, so a Service have a type. To work with Services we need a means to specify Services, there are service descriptions for this. The relation between Service Descriptions and Services is like the relation between interfaces and objects in Java. The Service Description describes what the Service can do, but not how. But why we don't use Java interfaces to describe Services or objects? The Service Description extends the possibility of specifying the Service, not only with its type but with custom attributes. So there can be multiple Services with the same type but different Service Descriptions.

Rules

Don't create a Service with the new keyword or other mechanism to create new objects from a class. Always use the Dependency Injection to create/get a service.

As a Service you MUST NOT pass your own (this) or any other service reference.

@Service

The @Service-Annotation is used to tell the DI that the annotated class should be managed. So other Services can have the Service as a dependency. Important to note is the separation of the implementation and the interface definition. The @Service-Annotation declares the Implementation and the parameter is the interface of the Service.

@Service(SimpleServiceAPI.class)
class MyService implements SimpleServiceAPI {
...
}

The implementation and the interface can be the same.

@Service(SimpleService.class)
class SimpleService {
...
}

@Reference

The @Reference-Annotation declares the runtime dependencies of Service implementation. The DI hide implementation details of dependencies, so a Service only has to know the interface of the dependency and not the class that implements that interface.

@Service(SimpleServiceAPI.class)
class MyService implements SimpleServiceAPI {
    @Reference
    private MailServiceAPI mailService;

    @Override
    public void sentMessage() {
        this.mailService.sendMail(...);
    }
}

@PostConstruct

The @PostConstruct-Annotation declares a method that should be executed to initialize the Service. The method is called after all dependencies are injected and before any other method is called.

@PreDestroy

The @PreDestroy-Annotation declares a method that free resources and removes all references to this Service form the system. The method is called after the DI decided that the Service is not needed anymore. It is called before all dependencies are removed and is the last method called on this Service.

Context

TODO