ashwinikhaple
23-03-2021
I have created a service called MyService and implemented it, I want to call it in my servlet so how can I do that? please help
asutosh_j3
Hi @ashwinikhaple Please see below:
public class MyServlet extends SlingAllMethodsServlet {
private static final long serialVersionUID = 1L;
@Referenceprivate transient MyService myservice;
@Overrideprotected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
myservice.anything();
}}
https://sling.apache.org/documentation/the-sling-engine/servlets.html
Hope this helps!Thanks!
Ankur_Khare
MVP
Kindly use like below-
@referenceprivate transient ApiService apiService;
Bhuwan_B
Please refer below community article:
https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/calling-component-in-a-ser...
Rohit_Utreja
Hi, @ashwinikhaple,
If you are using SCR annotation, you can use @Reference annotation to inject the service into the servlet.
Something similar to this.
@Reference MyService service;
You can refer to the below thread for more details.
https://stackoverflow.com/questions/56732548/aem-6-4-x-osgi-servlet-how-to-access-an-osgi-service-fr...
If you are using R6 annotation, Please refer to below URL for the same.
http://www.nateyolles.com/blog/2017/05/osgi-declarative-services-annotations-in-aem