Java Command Annotations – @Service
Java command annotations are used for the developing Java commands in PAK.
This chapter describes the functionality of the @Service
annotation.
It also gives an example of how you can use this annotation in your implementation.
1. Basics
The @Service
annotation can be used to mark a service that should be injected.
The service must either be registered in the IServiceProvider or provided via an own connector implementation.
|
2. Properties
There is a boolean property called optional
defined for the @Service
annotation.
As the name suggests, this property marks whether the injection of the service is optional or not.
The default value is false
which means that an exception will be thrown during the injection if the desired service is not available.
In contrast, no exception is thrown if the value is set to true
.
The usage of that property is shown in listing Listing 2.
3. Usage
@Service
has to annotate a class variable that should operate as service.
Usually, that variable has the type of a connector (e.g. IToolConnector
).
In the following listings in line 13 the @Service
annotation is used.
As the default for the optional
property is false
and nothing else is defined for that property, in the example of Listing 1 an exception will be thrown if the service is not available.
/**
* @workflowDocu This command gets an element by its id.
*/
@JavaCommand
@CommandGroup("Element")
public class GetElement {
/**
* @workflowDocu IToolConnector injected as a service in order to provide useful
* methods for tool integration
*/
@Service (1)
private IToolConnector toolConnecor;
[...]
}
1 | Use of the @Service annotation with default value of optional |
In contrast to example before the following listing sets the optional
property to true
.
As a result the service is optional and no exception will be thrown if the service is not available during the injection.
Line 13 of listing Listing 2 shows the usage of the optional
property.
/**
* @workflowDocu This command gets an element by its id.
*/
@JavaCommand
@CommandGroup("Element")
public class GetElement {
private static final Logger LOG = LoggerFactory.getLogger(GetElement.class);
/**
* Service injected in order to provide useful methods for e.g. tool integration
*/
@Service(optional = true) (1)
private IService service;
[...]
}
1 | Use of the @Service annotation with optional set to true |