Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

AEM Services

Avatar

Level 6

Can we use @reference annotation in normal class or can we get osgi services in normal class in aem

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

What do you mean by "normal" class? If you mean plain Java classes POJO (Plain Old Java Object) the simple answer is No.

 

the @reference annotation is part of the OSGi Declarative Services (DS) specification. It is used to inject OSGi services into OSGi component classes, which are typically OSGi service components.

The @reference annotation cannot be used directly in a normal Java class because it is specifically meant for OSGi service components that are registered and managed by the OSGi framework. Normal Java classes are not part of the OSGi service registry, and hence, the @reference annotation is not applicable to them.

 



Esteban Bustamante

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

What do you mean by "normal" class? If you mean plain Java classes POJO (Plain Old Java Object) the simple answer is No.

 

the @reference annotation is part of the OSGi Declarative Services (DS) specification. It is used to inject OSGi services into OSGi component classes, which are typically OSGi service components.

The @reference annotation cannot be used directly in a normal Java class because it is specifically meant for OSGi service components that are registered and managed by the OSGi framework. Normal Java classes are not part of the OSGi service registry, and hence, the @reference annotation is not applicable to them.

 



Esteban Bustamante

Avatar

Employee

the @reference annotation can not be used in normal Java classes,you can use the @reference annotation to inject references to other OSGi services into the component class.

 

For example, in a Sling Model class, you can use the @reference annotation to inject an OSGi service

 

import org.osgi.service.component.annotations.Reference;
import org.apache.sling.models.annotations.Model;

@Model(adaptables = Resource.class)
public class MySlingModel {
    @reference
    private MyOsgiService myOsgiService;
    
    // ... Rest of the code ...
}