Dependency Injection / IOC with Eclipse IDE Extension

Suppose I create a very simple eclipse plugin to create new java projects.

I am obviously creating a new wizard for the org.eclipse.ui.newWizards extension point. However, I really want other plugins to be able to implement the service that runs this new wizard.

So, in theory, we have three plugins:

  • My "main plugin" (with MyNewWizard)
  • My "interface plugin" (with IMyService)
  • My implementation plugin (with MyServiceImpl)

Using standard OSGI stuff, I would just use the services from ServiceTracker.

Unfortunately, Im in Eclipse OSGI land where I cannot create my wizard class by passing my ServiceTracker, but Eclipse does my plugin.

WITHOUT using a singleton in my Activator, does Eclipse provide some mechanism for inputting IoC / Dependency Injection, or at least a way to request services from these user interface classes?

thanks

+3
source share
2 answers

Here is one approach you can use, and I think this is a pretty easy way to Eclipse-y.

  • Define your IMyServiceinterface
  • In your plugin, define a new extension point, say, "myplugin.myservice"
  • (PDE ) , "myService", "class" ( ) "java". ( "" ) IMyService.

IMyService. - :

IExtensionRegistry registry = Platform.getExtensionRegistry();
for(IConfigurationElement element : registry.getConfigurationElementsFor("myplugin.myservice"))
{
    if("myService".equals(element.getName()))
    {
        return (IMyService) element.createExecutableExtension("class"));
    }
}
return new DefaultMyService();

, . , , - .

, , Eclipse , . , createExecutableExtension, .

+3

.

OSGi, ServiceTracker, . , , (.getDefault().getBundle()).

, " ", Platform.getBundle(). .

+1

Source: https://habr.com/ru/post/1717488/


All Articles