AEM6.4 @Reference not working in SlingServlet | Community
Skip to main content
New Participant
April 1, 2019
Solved

AEM6.4 @Reference not working in SlingServlet

  • April 1, 2019
  • 23 replies
  • 24983 views

Hi  All,

My sling servlet is not working as soon as i add

@Reference in it, otherwise it works fine

Below is the servlet code, i have create custom configuration as per the AEM6.4 standards(one @Interface class, one Interface class, one Impl class with proper annotations(org.osgi.service.component.annotations))

@Component(service = Servlet.class,

property = { Constants.SERVICE_DESCRIPTION + "=Simple Demo Servlet",

"sling.servlet.methods=" + HttpConstants.METHOD_GET,

"sling.servlet.paths=" + "/bin/testservlet" }

)

public class TestServlet extends SlingAllMethodsServlet {

  private static final long serialVersionUID = 1L;

 

  @Reference

  MySimpleService mySimpleService;

 

  private static final Logger logger =

  LoggerFactory.getLogger(TestServlet.class);

 

  @Override

  protected void doGet(final SlingHttpServletRequest req, final

  SlingHttpServletResponse resp) throws ServletException, IOException {

  logger.info("Inside doGet!!!");

  try {

  logger.info("Name ::: "+mySimpleService.getMyName());

  resp.getWriter().write(mySimpleService.getMyName()); }

  catch(Exception ex) {

  logger.error("Error in  doGet ::: ", ex);

  }

  }

}

 

Please advise, any help is highly appreciated.

Thanks,

Pradeep

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 smacdonald2008

Look at our SOLR Article. This one closely matches your requirement. We have a class named MySimpleService that reads OSGi config values.

In the Servlet in this article - we use a @Refence to get config values:

@Reference

    SolrServerConfiguration solrConfigurationService;

This shows the proper way of getting OSGI configuration information within an AEM Servlet. Never try and read OSGI config information directly from a servlet. Write a separate class - as shown in this development article.

Adobe Experience Manager Help | Integrating SOLR with Adobe Experience Manager 6.4

Hope this clear this issue up. Notice the Java code in this Servlet. We use the CONFIG values to perform SOLR actions - such as getting the URL to the SOLR server.

            final String protocol = solrConfigurationService.getSolrProtocol();

        final String serverName = solrConfigurationService.getSolrServerName();

        final String serverPort = solrConfigurationService.getSolrServerPort();

        final String coreName = solrConfigurationService.getSolrCoreName();

        final String pagesResourcePath = solrConfigurationService

I would go through this entire article so you understand.

Hope this helps you.

23 replies

New Participant
April 2, 2019

Yes, If I add @Reference sling servlet stopped working. It is in unsatisfied state due to the reference.

arunpatidar
New Participant
April 1, 2019

Hi,

Could you please check if your bundle is active or not? If yes the check if you using osgi reference annotation. e.g.

import org.osgi.service.component.annotations.Reference;

e.g.

aem63app-repo/GetFeaturesServlet.java at master · arunpatidar02/aem63app-repo · GitHub

Arun Patidar
smacdonald2008
New Participant
April 1, 2019

Do you mean when you have this line of code:

@Reference

  MySimpleService mySimpleService;

Its not working?