-
Notifications
You must be signed in to change notification settings - Fork 1
Annotations
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.
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 {
...
}
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(...);
}
}
see dialog system