The annotation @Reference is disallowed for this location | Community
Skip to main content
VeenaVikraman
Community Advisor
Community Advisor
January 31, 2019
Solved

The annotation @Reference is disallowed for this location

  • January 31, 2019
  • 21 replies
  • 18389 views

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Gaurav-Behl

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-declarative-services-annotatio…

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.

21 replies

smacdonald2008
Level 10
January 31, 2019

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.

VeenaVikraman
Community Advisor
Community Advisor
February 1, 2019

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.

VeenaVikraman
Community Advisor
Community Advisor
February 1, 2019

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

VeenaVikraman
Community Advisor
Community Advisor
February 1, 2019

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

Feike_Visser1
Adobe Employee
Adobe Employee
February 1, 2019

my best guess is that is a difference in the poms

VeenaVikraman
Community Advisor
Community Advisor
February 1, 2019

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 ?

smacdonald2008
Level 10
February 1, 2019

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

VeenaVikraman
Community Advisor
Community Advisor
February 2, 2019

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 ?

Gaurav-Behl
Gaurav-BehlAccepted solution
Level 10
February 2, 2019

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-declarative-services-annotatio…

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.
VeenaVikraman
Community Advisor
Community Advisor
February 2, 2019

Thanks gauravb10066713

Level 2
March 3, 2022

Hi All,

 

I my case, I removed the below dependency from pom.xml files and it solved the issue.

<dependency>
            <groupId>biz.aQute</groupId>
            <artifactId>bndlib</artifactId>
        </dependency>