Eclipse RCP 4 use bundle via declarative service

纵然是瞬间 提交于 2019-12-05 11:44:55

I can suggest a few things you can do to debug.

  1. Have you actually got an scr implementation in your runtime? SCR (another name for declarative services) isn't included in Equinox core, so you'll need to include it. Most people use the Felix SCR bundle - it will sit very happily on top of Equinox.

  2. Since Declarative Services just use services, you can change one half of your app at a time, to identify whether it's the service consumption or registration which isn't working.

  3. You can also use the Equinox console to inspect your service registration. Use 'ss' to identify your bundle, then 'bundle [no]' to see what services are registered and consumed. If you're using Felix SCR, there are also Equinox console extensions, so you can use 'scr list' to see all services which a bundle attempts to register (and their state), and 'scr info' for more details on a particular service.

I have found a solution.

In the manifest file of my service I have added the following line:

Bundle-ActivationPolicy: lazy

After that the userServiceConsumer and component definition in my application are unneeded.

In a view class I can do the following now:

public class MyPart {

    private IUserService uServ;

    @Inject
    public MyPart(IUserService uServ) {
        this.uServ = uServ;
        if (uServ != null)
            System.out.println("UserService available");
        else
            System.out.println("UserService == null");
    }

Via DI my service is injected in constructor of the view. That works for me!

Christian Schneider

I think declarative services will not work in your UI class as it will not be created by the SCR but by Eclipse. So what you can try is to leave the UI as it was with @Inject and only change the bundle providing the service to use DS.

Basically I would not even try to change the UI side. The @Inject notations is much less overhead than the declarative services.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!