Hi,
I have an osgi bundle with a reference of a interface: @Reference private MyServiceInterface myserviceInterface; The bundle is built correctly and i can see the service under both system/console/services and system/console/configMgr. The MyServiceImplementation (the class that implements the interface MyServiceInterface) has the property immediate=true and his activator method runs with the build of the bundle but the variable myserviceInterface that i want to access in another class is null Have you any idea?
Thanks
Solved! Go to Solution.
Views
Replies
Total Likes
Hi,
the class containing this @Reference to MyServiceInterface has to be a component or service to make this SCR magic work.
kind regards,
Jörg
Views
Replies
Total Likes
By default a component providing a service is delayed and injection works properly. Looks like because you are using immediate=true- its not delayed and its affecting dependency injection. This is explained here:
http://dev.day.com/content/ddc/blog/2010/01/dsdelayedcomponent.html
Views
Replies
Total Likes
Hi,
the class containing this @Reference to MyServiceInterface has to be a component or service to make this SCR magic work.
kind regards,
Jörg
Views
Replies
Total Likes
This is the header of the class with the reference:
@Component(metatype=true,label="MyClass")
@Service(Runnable.class)
This is the header of the class that implements the interface:
@Component(metatype = true, label = "MyService", immediate=true)
@Service(MyServiceInterface.class)
Any idea?
Views
Replies
Total Likes
Hi samuel786431,
I don't think there is any issue with annotation attributes. May I have answers of following queries?.
If above things are fine, then would like to see if there is any exception in the log. Also, would like to see code
Jitendra
Views
Replies
Total Likes
The bundle is active, i don't know how to verify if a specific service is active (the service is listed inside the system console) I'm trying to simulate the problem inside a little use case:import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.apache.felix.scr.annotations.*; import org.apache.felix.scr.annotations.Component; import org.apache.felix.scr.annotations.Reference; import org.apache.felix.scr.annotations.Service; import FakePath.PabAuthService; @Component(metatype=true,label="MyTest",description="MyTest") @Service(Runnable.class) @Properties({ @Property(name="scheduler.runOn", value="LEADER"), @Property(name="scheduler.expression", value="0 * * * * ?"), @Property(name="scheduler.concurrent",boolValue=false) }) public class MyTest implements Runnable{ protected final Logger log = LoggerFactory.getLogger(this.getClass()); public void run() { } @Reference private PabAuthService PabAuthService; public String test(){ return ""+PabAuthService; }and<%@page import="com.day.cq.wcm.api.WCMMode"%> <%@page import="FakePath.service.*"%> <%@page import="FakePath.service.impl.*"%> <%MyTest test = new MyTest();%> <h2><%=test.test()%></h2>The h2 tag contains null...
Views
Replies
Total Likes
Hi @samuel786431,
This is the problem. You can't get the reference of a service by creating MyTest Object here. MyTest is a service and reference of this service already exist.
Try below code.
And then call myTest.test() inside h2 tag.
FYI : @Reference annotation works the way dependency injection works. Here is the doc which explains all about use of Sling API.
https://helpx.adobe.com/experience-manager/using/using-sling-apis.html
---
Jitendra
Views
Replies
Total Likes
I have another jsp with your code and it works fine but the class inside the getService is an interface
MyTest isn't an interface.
If i use <%MyTest test = sling.getService(MyTest.class);%> test will be null.
How can i assign an interface to my service? @Service(Runnable.class, MyInterface.class) doesn't work
Views
Replies
Total Likes
Oh yes. Sorry My bad. Let me know re-think.
Views
Replies
Total Likes
Hi @samuel786431,
In my view, it can't be solved like this. We might have to restructure our classes. CRON job & Service separately. We shouldn't be mixing both things together.
//Cron job with run method and a separate service with
--
Jitendra
Views
Replies
Total Likes
Views
Likes
Replies
Views
Like
Replies
Views
Likes
Replies