Expand my Community achievements bar.

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 3

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>

Avatar

Level 3

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>