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

Gaurav-Behl
Level 10
January 31, 2019

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.

Feike_Visser1
Adobe Employee
Adobe Employee
January 31, 2019

what are the paths of @Compoent and @Reference?

VeenaVikraman
Community Advisor
Community Advisor
January 31, 2019

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;

Feike_Visser1
Adobe Employee
Adobe Employee
January 31, 2019

what version are you using of this?

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

VeenaVikraman
Community Advisor
Community Advisor
January 31, 2019

gauravb10066713

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"

VeenaVikraman
Community Advisor
Community Advisor
January 31, 2019
Feike_Visser1
Adobe Employee
Adobe Employee
January 31, 2019
VeenaVikraman
Community Advisor
Community Advisor
January 31, 2019

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

Feike_Visser1
Adobe Employee
Adobe Employee
January 31, 2019

can you share the code?

Gaurav-Behl
Level 10
January 31, 2019

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