I have a question more on the ground on understanding why it is done so.
So I have a servlet in my project on which when I am using @Reference is throws me error "The annotation @Reference is disallowed for this location". But a similar servlet in a new project is not throwing the error. For both of them I am using OSGI annotations. I read that we have to do @Reference in a different way https://www.aemquickstart.in/2018/10/annotation-reference-is-disallowed-for.html . It works . But My question is why are we doing it in that way ? Just want to understand the reasoning and logic and technicalities. Also , why it worked in my new project . Since it worked in one and not in the other , I am bit confused and eager to know what actually controls this
Worked
Didn't work
Thanks
Veena
Solved! Go to Solution.
Views
Replies
Total Likes
Hope this would provide more clues to check the library versions in pom.xml/generated scr:component xmls under OSGI-INF folder --
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
source: Getting Started with OSGi Declarative Services | vogella blog
7.3 @Reference
It is used to specify the dependency on other services. With DS 1.2 it can only be used with Event Methods. DS 1.3 also introduced the usage of @Reference on fields and the type element reference of @Component .
The @Reference annotation needs to be applied on the bind event method. The following defaults are used in that case:
It will infer a default unset method and updated method based on the name of the bind method.
Also introduced with DS 1.3 is the Field Strategy for binding services. With this it is not necessary to specify a bind event method for a reference. Instead it is possible to apply the @Reference annotation to a field in the component implementation class. For a static reference the field will be set before the component instance is activated. Dynamic references need to be marked as volatile so that changes to that field are also visible to other threads.
When applying @Reference on a field, the following defaults are used:
Note: Only instance fields are supported. The @Reference annotation can not be applied to static fields.
source: https://blog.osoco.de/2016/05/migrating-from-the-apache-felix-scr-annotations-to-the-osgi-declarativ...
APACHE FELIX SCR ANNOTATION | DESCRIPTION | OSGI DECLARATIVE SERVICES ANNOTATION | DESCRIPTION |
---|
@Reference | Reference to services, can be used on unary fields or on class level with bind/unbind methods. | @Reference | Field references can directly be migrated, for event based references (methods), the @Reference annotation must be put on the bind method. In addition, more options for field references exist. |
This behavior could happen probably because of R4/R6 conflicts/backward-compatibility.
In the project where it works at member variable level, that project is either with R4/R6 combination or with R4 jars only. Check the pom.xml if you have scr.annotations in your pom.xml/build-path. Alternatively, check the import statements/Type Hierarchy for the jar that resolves Reference annotation.
In the project where it doesn't work, it must have strict jar imports for R6 only. You can easily find it when you check jars/versions in pom.xml
OSGI R4 would allow you to define the annotations on member variable(s) while R6 prefers it on method(s) than member variable.
You may share pom.xml/classes for more information.
what are the paths of @Compoent and @Reference?
Views
Replies
Total Likes
feike_visser Did you mean the package ? Both are OSGI annotations , in both the servlets
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
what version are you using of this?
<artifactId>maven-bundle-plugin</artifactId>
The one for which it works the pom has below reference for OSGI
The below is the OSGI references for the one in which @Reference showed "not allowed"
can you try <version>4.1.0</version>
htl-examples/pom.xml at master · heervisscher/htl-examples · GitHub
I changed it and again tried @Reference . But it gave me the same error.
can you share the code?
Did you use same maven archetype for both projects? Are both pom.xml files similar w.r.t. non-project related libraries?
Try on a fresh Maven 13-15 project. Let's rule out these later maven archetype projects. I have never seen an issue with @Reference in a servlet.
The one in which I am having issue cannot be shared Feike
I do see that some people has faced similar issues and they suggest to create bind and unbind method like below which works.
gauravb10066713 I am not sure of the archetype used for my project code as it was an upgraded code from 6.2 . The one which I created newly , I used archetype 13 . Also when that code was done in 6.2 @Reference was done like usual
But when the same code was upgraded , it has to be done as below.
As I was not part of the initial up-gradation , I am not sure what steps were followed . But the above usage was a new method for me and I was really curious to know why the other way around was not working in the upgraded code and what is the peculiarity of doing this way
On my fresh project , the @Reference works as usual scott .smacdonald2008
my best guess is that is a difference in the poms
Guess so. But Feike just to understand what is happening, what is the importance of doing the other way ? Why was it not allowing to use @Reference the way we normally use ? Does it has to do something very specific with OSGI annotations ?
I agree with Feike too. It works on Fresh Maven project. Looks like there is something wrong in the POMS.
Agree Scott . But in many posts I saw that this situation happens and the solution also was mentioned. My intent is mainly to understand why this error happens in the first place ? Is there anything to do with the latest OSGI annotation as gauravb10066713 was mentioning ?
Hope this would provide more clues to check the library versions in pom.xml/generated scr:component xmls under OSGI-INF folder --
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
source: Getting Started with OSGi Declarative Services | vogella blog
7.3 @Reference
It is used to specify the dependency on other services. With DS 1.2 it can only be used with Event Methods. DS 1.3 also introduced the usage of @Reference on fields and the type element reference of @Component .
The @Reference annotation needs to be applied on the bind event method. The following defaults are used in that case:
It will infer a default unset method and updated method based on the name of the bind method.
Also introduced with DS 1.3 is the Field Strategy for binding services. With this it is not necessary to specify a bind event method for a reference. Instead it is possible to apply the @Reference annotation to a field in the component implementation class. For a static reference the field will be set before the component instance is activated. Dynamic references need to be marked as volatile so that changes to that field are also visible to other threads.
When applying @Reference on a field, the following defaults are used:
Note: Only instance fields are supported. The @Reference annotation can not be applied to static fields.
source: https://blog.osoco.de/2016/05/migrating-from-the-apache-felix-scr-annotations-to-the-osgi-declarativ...
APACHE FELIX SCR ANNOTATION | DESCRIPTION | OSGI DECLARATIVE SERVICES ANNOTATION | DESCRIPTION |
---|
@Reference | Reference to services, can be used on unary fields or on class level with bind/unbind methods. | @Reference | Field references can directly be migrated, for event based references (methods), the @Reference annotation must be put on the bind method. In addition, more options for field references exist. |
Thanks gauravb10066713
Views
Likes
Replies
Views
Likes
Replies