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

Annotations

Lars B edited this page Sep 5, 2018 · 14 revisions

Annotations used for Dependency Injection

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.

@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(...);
    }
}

Annotations used in the Speech Class of Plugins

see dialog system