@Reference Annotation not working in aem Fiddle Script for JSP and servlets | Community
Skip to main content
antarikshruhil9
New Participant
March 1, 2024

@Reference Annotation not working in aem Fiddle Script for JSP and servlets

  • March 1, 2024
  • 3 replies
  • 1119 views

It complains 

Compilation errors in /apps/acs-tools/components/aemfiddle/fiddle/fiddle.java: Line 15, column 396 : Only a type can be imported. org.osgi.service.component.annotations.Reference resolves to a package

 

Found out one solution and gets the work done:

getService(MyService.class);

 

static <T> T getService(Class<T> serviceClass) {
BundleContext bContext = FrameworkUtil.getBundle(serviceClass).getBundleContext();
ServiceReference sr = bContext.getServiceReference(serviceClass.getName());
return serviceClass.cast(bContext.getService(sr));
}

 

Any better way to do it?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

3 replies

kautuk_sahni
Employee
March 7, 2024

@antarikshruhil9 Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.

Kautuk Sahni
Imran__Khan
New Participant
March 2, 2024

@antarikshruhil9 There is some issue I am seeing with the fiddle as it is not compiling imports. Removing some keywords from import package also not giving any exception/error. That's because it is not able to resolve any Java file.

EstebanBustamante
New Participant
March 1, 2024

Hi, 


You can use it from slingBinding instead:

 

public class fiddle extends SlingAllMethodsServlet { //Any OSGI service you will normally use through MyService service = null; SlingScriptHelper scriptHelper = null; protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException { SlingBindings bindings = (SlingBindings) request.getAttribute(SlingBindings.class.getName()); scriptHelper = bindings.getSling(); //Here you get the OSGI service service = scriptHelper.getService(MyService.class);

 


Hope this helps.

Esteban Bustamante
antarikshruhil9
New Participant
March 7, 2024

Thanks @EstebanBustamante  That works as well. Seems it depends on the usecase and need. Both solutions with BundleContext and SlingBindings would work.