...
Support for automatic generation of the compenent and metadata descriptors is embeded in the org.apache.felix:maven-scr-plugin
plugin. To use this plugin, it has to be declared in the project descriptor as a <plugin>
element:
Code Block | ||||
---|---|---|---|---|
| ||||
<project> ... <build> ... <plugins> ... <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-scr-plugin</artifactId> <executions> <execution> <id>generate-scr-scrdescriptor</id> <goals> <goal>scr</goal> </goals> </execution> </executions> </plugin> ... </plugins> ... </build> ... </project> |
...
Currently the maven-scr-plugin
only creates the component descriptor file. Adding the descriptor to the bundle and setting the Service-Component
manifest header accordingly is a different task. However, if you're using the org.apache.felix:maven-bundle-plugin
to construct the bundle and its manifest, then the maven-scr-plugin
will add the following settings automatically for the org.apache.felix:maven-bundle-plugin
(given default maven-scr-plugin
configuration), so you don't have to configure this yourself:
Code Blocknoformat | ||||
---|---|---|---|---|
| ||||
... <Include-Resource> src/main/resources, target/scr-plugin-generated </Include-Resource> <Service-Component> OSGI-INF/serviceComponents.xml </Service-Component> ... |
...
The maven-scr-plugin
uses the SCR
annotations from the corresponding subproject at Apache Felix. All annotations are in the org.apache.felix.scr.annotations
package. If you want to use the annotations in your project, you have to use a maven-scr-plugin
version >= 1.2.0 and make sure that you add a dependency to the annotations to your POM
:
Code Block | ||
---|---|---|
| ||
| ||
<dependency> <groupId>org.apache.felix</groupId> <artifactId>org.apache.felix.scr.annotations</artifactId> <version>1.2.0</version> </dependency> |
...
...
Component
The Component
annotation is the only required annotation. If this annotation is not declared for a Java class, the class is not declared as a component.
...
policy | | no | component.policy | – | The configuration policy for this component: |
Abstract Service Descriptions
If the Component
annotations contains the attribute componentAbstract
with a value of true, the containing class is regarded as an abstract class. It is not added to the service descriptor and the tags are not validated. The information about this class is added to the bundle. Classes from other bundles (or the same) can extends this abstract class and do not need to specify the references of the abstract class if they set the inherit
parameter on the scr.component
tag to true.
...
The Declarative Service version 1.1 allows to specify the name for the activate, deactivate and modified method (see the spec for more information). The Activate
, Deactivate
, and Modified
annotation can be used to mark a method to be used for the specified purpose. However, as the DS specifies a method search algorithm, there are rare cases where the marked method is not used (if there is another method with the same name, but a different signature this might happen).anchor
Service
The Service
annotation defines whether and which service interfaces are provided by the component. This is a class annotation.
...
Omitting the Service
annotation will just define (and activate if required) the component but not register it as a service. Multiple Service
annotations may be declared each with its own value
. These annotations need to be wrapped into a Services
anotation. The component is registered as a ServiceFactory
if at least on Service
annotations declares the serviceFactory
attribute as true
. Anchor
Property
The Property
annotation defines properties which are made available to the component through the ComponentContext.getProperties()
method. These tags are not strictly required but may be used by components to defined initial configuration. Additionally properties may be set here to identify the component if it is registered as a service, for example the service.description
and service.vendor
properties.
...
Name | Default Value | Required | SCR | Metatype | Description | ||
name | The name of constant | yes | | | The name of the property. If this tag is defined on a field with an initialization expression, the value of that expression is used as the name if the field is of type | ||
value | – | no | | | The string value of the property. This can either be a single value or an array. | ||
longValue | – | no | | | The long value of the property. This can either be a single value or an array. | ||
doubleValue | – | no | | | The double value of the property. This can either be a single value or an array. | ||
floatValue | – | no | | | The float value of the property. This can either be a single value or an array. | ||
intValue | – | no | | | The int value of the property. This can either be a single value or an array. | ||
byteValue | – | no | | | The byte value of the property. This can either be a single value or an array. | ||
charValue | – | no | | | The char value of the property. This can either be a single value or an array. | ||
boolValue | – | no | | | The boolean value of the property. This can either be a single value or an array. | ||
shortValue | – | no | | | The short value of the property. This can either be a single value or an array. | ||
label | | no | – | | The label to display in a form to configure this property. This name may be localized by prepending a | ||
description | | no | – | | A descriptive text to provide the client in a form to configure this property. This name may be localized by prepending a | ||
propertyPrivate | Depending on the name | no | – | See description | Boolean flag defining whether a metatype descriptor entry should be generated for this property or not. By default a metatype descriptor entry, i.e. an | ||
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="206c482a89a46955-d5adcea2-4f874ace-bb99aec4-5d785a263025645a92eeb371"><ac:plain-text-body><![CDATA[ | cardinality | Depends on property value(s) | no | – | | Defines the cardinality of the property and its collection type. If the cardinality is negative, the property is expected to be stored in a | ]]></ac:plain-text-body></ac:structured-macro> |
options | – | no | – | See below | See below for a description of the |
...
It is important to carefully define the name of properties. By using a constant of the form
Code Block | ||
---|---|---|
| ||
| ||
@Property(value="default value") static final String CONSTANT_NAME = "property.name"; |
...
If the cardinality of the property is not explicilty set with the cardinality
property, it defaults to Integer.MAX_INT
, i.e. unbound array, if multiple values are defined. Otherwise the cardinality
parameter may be set for example to a negative value to store the values in a java.util.Vector
instead. scran.reference Anchor
Reference
The Reference
annotation defines references to other services made available to the component by the Service Component Runtime.
...
...
scr.component
scr.component
The scr.component
tag is the only required tag. If this tag is not declared in the Java class comment, the class is not declared as a component.
...
This allows to create abstract classes which already provide some valuable functionality without having to deal with the details like reference definitions in each and every subclass. Anchor
scr.property
The scr.property
tag defines properties which are made available to the component through the ComponentContext.getProperties()
method. These tags are not strictly required but may be used by components to defined initial configuration. Additionally properties may be set here to identify the component if it is registered as a service, for example the service.description
and service.vendor
properties.
...
Name | Default Value | Required | SCR | Metatype | Description | ||
name | The name of constant | yes | | | The name of the property. If this tag is defined on a field with an initialization expression, the value of that expression is used as the name if the field is of type | ||
value | – | no | | | The value of the property. If the property type is not | ||
type | | no | | | The type of the property value. This must be one of | ||
label | | no | – | | The label to display in a form to configure this property. This name may be localized by prepending a | ||
description | | no | – | | A descriptive text to provide the client in a form to configure this property. This name may be localized by prepending a | ||
private | Depending on the name | no | – | See description | Boolean flag defining whether a metatype descriptor entry should be generated for this property or not. By default a metatype descriptor entry, i.e. an | ||
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="573d10682897bc86-42aa7bfb-49f84a10-8e1bbd65-b3cc19fe71ffa536a0eed503"><ac:plain-text-body><![CDATA[ | cardinality | Depends on property value(s) | no | – | | Defines the cardinality of the property and its collection type. If the cardinality is negative, the property is expected to be stored in a | ]]></ac:plain-text-body></ac:structured-macro> |
options | – | no | – | See below | See below for a description of the | ||
values* | – | no | – | See below | See below for a description of parameters starting with | ||
valueRef | – | no | – | | A constant containing the value for this property. The constant can either be declared in the same class as this property or in any class that is imported. The type of the property is derived from the constant. | ||
valueRefs | – | no | – | See below | Same as the |
...
It is important to carefully define the name of properties. By using a constant of the form
Code Block | ||
---|---|---|
| ||
| ||
/** @scr.property value="default value" */ static final String CONSTANT_NAME = "property.name"; |
...
If the cardinality of the property is not explicilty set with the cardinality
property, it defaults to Integer.MAX_INT
, i.e. unbound array, if multiple values with a series of values
parameters are defined. Otherwise the cardinality
parameter may be set for example to a negative value to store the values in a java.util.Vector
instead. Anchor
scr.service
The scr.service
tag defines whether and which service interfaces are provided by the component.
...
Omitting the scr.service
tag will just define (and activate if required) the component but not register it as a service. Multiple scr.service
tags may be declared each with its own interface
. The component is registered as a ServiceFactory
if at least on scr.service
tag declares the servicefactory
parameter as true
. Anchor
scr.reference
The scr.reference
tag defines references to other services made available to the component by the Service Component Runtime.
...