service injection with parameter | Community
Skip to main content
February 9, 2018
Solved

service injection with parameter

  • February 9, 2018
  • 3 replies
  • 1928 views

I am trying to call AWS S3 bucket from our AEM. I have created an S3Manager class/service to to wrap it. which will be injected/referenced from wherever needed. This is working fine, but requires to create AmazonS3Client instance each time when I need to upload or download a file.

I am trying to initialise the amazonS3Client in the constructor instead, so I don't need to create so many instance.

private AmazonS3Client amazonS3Client;

public S3Manager(){

  String externalId = sitemapService.getAssumeRoleId();
   String roleArn = sitemapService.getRoleArn();


   amazonS3Client = getAmazonS3Client(externalId, roleArn);
}

SitemapService is another service I created to read the OSGI config (from xml or the console). The way I am doing seems not working, I am getting following exception, when initialisation try to run  sitemapService.getAssumeRoleId();

Error during instantiation of the implementation object (java.lang.NullPointerException)

I assume at the point of running, sitemapservice has not been injected yet. Is there a way, I can assure that sitemapservice injection is completed before initialise S3Manager? Or there is better way to achieve what I am looking for. Thanks

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 joerghoh

When you use the standard injection (just @Reference) it has the ReferencePolicy static. That means, that this reference must be satisfied in order to acticate the component.

But I am quite sure, that your issue is different. When you implement that in the constructor, the OSGI runtime has not yet injected the references. You need to implement a service activator, see Apache Felix - Apache Felix Service Component Runtime (SCR)  for a way how to do it (note: today you are encouraged to use OSGI annotations and no longer SCR, but the activate stuff remains the same ...)

Jörg

3 replies

joerghoh
Adobe Employee
joerghohAdobe EmployeeAccepted solution
Adobe Employee
February 9, 2018

When you use the standard injection (just @Reference) it has the ReferencePolicy static. That means, that this reference must be satisfied in order to acticate the component.

But I am quite sure, that your issue is different. When you implement that in the constructor, the OSGI runtime has not yet injected the references. You need to implement a service activator, see Apache Felix - Apache Felix Service Component Runtime (SCR)  for a way how to do it (note: today you are encouraged to use OSGI annotations and no longer SCR, but the activate stuff remains the same ...)

Jörg

brolinukAuthor
February 12, 2018

Thanks Jörg. How would I able to make sure that the injection happens before the initialisation? I have already tried to set attribute immediate to true for the Component, but that doesn't make any difference.  Is there something I can configure the order or so ? Thanks

joerghoh
Adobe Employee
Adobe Employee
February 13, 2018

Make sure you understand the OSGI component livecycle. Initialization should not happen in the constructor, but rather in the activate() method (or the method annotated with @Activate).

Besides the OSGI specification (which is sometimes hard to read) I found org.osgi.service.component | osgi.enroute.site  to explain it nicely.

Jörg