Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

The annotation @Reference is disallowed for this location

Avatar

Community Advisor

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

1680373_pastedImage_3.png

Didn't work

1680401_pastedImage_5.png

Thanks

Veena

1 Accepted Solution

Avatar

Correct answer by
Level 10

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:

  • The name of the bind method is used for the name of the reference. That means the method name after the prefix (e.g. setStringInverter() -> StringInverter). Mind the case sensitivity, as the name in that case starts with an upper case letter.
  • 1:1 cardinality.
  • Static reluctant policy.
  • The requested service is the type of the first argument of the bind method.

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:

  • The name of the field is used for the name of the reference.
  • 1:1 cardinality if the field is not a collection. 0..n cardinality if the field is a collection.
  • Static reluctant policy if the field is not declared volatile. Dynamic reluctant policy if the field is declared volatile.
  • The requested service is the type of the field in case the field type is a service type.

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
@ReferenceReference to services, can be used on unary fields or on class level with bind/unbind methods.@ReferenceField 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.

View solution in original post

22 Replies

Avatar

Level 10

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.

Avatar

Employee

what are the paths of @Compoent and @Reference?

Avatar

Community Advisor

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;

Avatar

Employee

what version are you using of this?

<artifactId>maven-bundle-plugin</artifactId>

Avatar

Community Advisor

gauravb10066713

The one for which it works the pom has below reference for OSGI

1680617_pastedImage_0.png

The below is the OSGI references for the one in which @Reference showed "not allowed"

1680618_pastedImage_1.png

Avatar

Community Advisor

I changed it and again tried @Reference . But it gave me the same error.

Avatar

Level 10

Did you use same maven archetype for both projects? Are both pom.xml files similar w.r.t. non-project related libraries?

Avatar

Level 10

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.

Avatar

Community Advisor

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.

1681382_pastedImage_0.png

Avatar

Community Advisor

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

1681383_pastedImage_0.png

But when the same code was upgraded , it has to be done as below.

1681384_pastedImage_1.png

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

Avatar

Community Advisor

On my fresh project , the @Reference works as usual scott .smacdonald2008

Avatar

Employee

my best guess is that is a difference in the poms

Avatar

Community Advisor

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 ?

Avatar

Level 10

I agree with Feike too. It works on Fresh Maven project. Looks like there is something wrong in the POMS.

Avatar

Community Advisor

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 ?

Avatar

Correct answer by
Level 10

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:

  • The name of the bind method is used for the name of the reference. That means the method name after the prefix (e.g. setStringInverter() -> StringInverter). Mind the case sensitivity, as the name in that case starts with an upper case letter.
  • 1:1 cardinality.
  • Static reluctant policy.
  • The requested service is the type of the first argument of the bind method.

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:

  • The name of the field is used for the name of the reference.
  • 1:1 cardinality if the field is not a collection. 0..n cardinality if the field is a collection.
  • Static reluctant policy if the field is not declared volatile. Dynamic reluctant policy if the field is declared volatile.
  • The requested service is the type of the field in case the field type is a service type.

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
@ReferenceReference to services, can be used on unary fields or on class level with bind/unbind methods.@ReferenceField 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.