I am trying to use the @Reference Felix SCR annotation in Scala
This is how it can be used in Java:
public class Foo {
@Reference
MyServiceInterface service;
// some code here
}
Here, MyService is ideally a Java Interface and not a concrete class (dependency injection)
I am using the same annotation in Scala, trying to depend on the same MyService (a Java Interface), like:
class Foo {
@Reference
val service = MyServiceInterface // ??
// some code here
}
How can I use this annotation in Scala, to refer to a Java Interface?
example:
@Reference
val MyServiceInterface service
or
@Reference
val service = MyServiceInterface
is not valid Scala code
Thank you
I was able to solve this problem by specifying the type of the variable correctly like:
@Reference
var service: MyServiceInterface = null
@Neil is right, we must use a var instead of a val as the service binding happens at runtime.
Secondly, I had to add the following configuration to the maven-scr-plugin:
<configuration>
<scanClasses>true</scanClasses>
</configuration>
Then, I had to specifically instruct the Maven Bundle plugin to NOT import the org.apache.felix.scr.annotations
package as follows:
<Import-Package>!org.apache.felix.scr.annotations, *</Import-Package>
because the manifest would otherwise include an entry for this package as something that the bundle depends on.
Once this was done, the mvn-scr-plugin would generate the XML file correctly and the mvn-bundle-plugin would generate the bundle, manifest correctly.