I have written an OSGi bundle to use it in my eclipse 4 rcp application. The usage of the service works fine if I add the dependencies, register these service in my activator and inject it in my class.
In activator
IUserService service = new TestUserService();
context.registerService(IUserService.class.getName(), service, null);
In my class
@Inject
IUserService service;
service.getSth();
I read that using bundles via declarative services should be the better way. So changed my implementation. I created a component definition in my bundle to provide my service:
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="usermanagement.test">
<implementation class="usermanagement.test.TestUserService"/>
<service>
<provide interface="usermanagement.IUserService"/>
</service>
</scr:component>
Then I removed the service registration from my activator and created an service consumer class:
public class UserServiceConsumer {
private IUserService service;
public synchronized void setQuote(IUserService service) {
this.service = service;
}
public synchronized void unsetQuote(IUserService service) {
if (this.service == service) {
this.service = null;
}
}
}
and another component definition:
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="UserServiceConsumer">
<implementation class="services.UserServiceConsumer"/>
<reference bind="setService" cardinality="1..1" interface="usermanagement.IUserService" name="IUserService" policy="static" unbind="unsetService"/>
</scr:component>
After these modifications the injection of my serivce does not work anymore. The problem is that the injected service reference is NULL everytime.
Does anyone know why? Did I forgot something?
Thanks a lot!