When trying to @Inject
(javax.inject.Inject) to inject MyConfigurationService
within @SlingServlet MyServlet
leads to a NullPointerError
anytime any operations are attempted on myConfigurationService
within an AEM OSGi container using Maven org.apache.felix.maven-scr-plugin
as part of the build process.
Service Implementation:
@Service({MyConfigurationService.class})
@Component(immediate = true, metatype = true, label = "My Configuration Service")
public class MyConfigurationServiceImpl implements MyConfigurationService {
@Property(unbounded = PropertyUnbounded.DEFAULT, label = "API URL", description = "API URL")
private static final String API_URL = "apiurl";
private String apiUrl;
@Activate
protected void activate(Map<String, Object> properties) {
this.apiUrl = PropertiesUtil.toString(properties.get(API_URL), "");
}
}
Servlet:
@SlingServlet(paths = "/bin/myServlet", methods = "POST", metatype = true)
public class MyServlet extends org.apache.sling.api.servlets.SlingAllMethodsServlet {
private static final long serialVersionUID = 1L;
private static final Logger logger = LoggerFactory.getLogger(MyServlet.class);
@Inject
MyConfigurationService myConfigurationService;
@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServerException, IOException {
// any attempts to use myConfigurationService results in NPE
}
}
However using @Reference
(org.apache.felix.scr.annotations.Reference) in place of @Inject
successfully injects the service and is usable within the @SlingServlet
methods such as doPost
:
@Reference
MyConfigurationService myConfigurationService;
Why does @Inject
fail to inject the service into the @SlingServlet
when @Reference
works?
Thank you for any help you can provide!
@Inject
is something really only used in @Model annotated elements, while@Reference
would be used within@Service
,@Component
or similar annotated elements to effectively accomplish dependency injection? – Shewchuk