Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » Use of DI in extension(DependencyInjection and Extensions)
Use of DI in extension [message #898660] Fri, 27 July 2012 02:31 Go to next message
Stefan Wiese is currently offline Stefan Wiese
Messages: 10
Registered: July 2011
Junior Member
I've created an extension point in plugin a and an extension in plugin b.
The extension has a dependency to ECommandService, EHandlerService and MApplication. All dependencies are null. I tried to inject an IEclipseContext in ctor but it is null.
What do I have to pay attention for when using DependencyInjection in extensions?
Is it in general possible?

Stefan
Re: Use of DI in extension [message #898668 is a reply to message #898660] Fri, 27 July 2012 02:50 Go to previous messageGo to next message
Stefan Wiese is currently offline Stefan Wiese
Messages: 10
Registered: July 2011
Junior Member
Ah, here's the solution:
When evaluating the extensions simply call ContextInjectionFactory.inject(context);

@Inject
public OpenSearchHandler(ISearchComponent searchComponent, IEclipseContext context, IExtensionRegistry registry)
	{
		IConfigurationElement[] extensions = registry
			.getConfigurationElementsFor("my.own.extensionpoint.definition.searchprovider");

		for (IConfigurationElement extension : extensions)
		{
			LOG.debug("Evaluating extension");
			try
			{
				final Object o = extension.createExecutableExtension("class");
				if (o instanceof SearchProvider)
				{
					SearchProvider searchProvider = (SearchProvider) o;
					LOG.debug("Found SearchProvider --> " + searchProvider.getLabel());
					ContextInjectionFactory.inject(searchProvider, context);
					searchComponent.addSearchProvider(searchProvider);
				}
			}
			catch (CoreException e)
			{
				LOG.error(e);
			}
		}

	}


Stefan
Re: Use of DI in extension [message #898670 is a reply to message #898660] Fri, 27 July 2012 02:52 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5296
Registered: July 2009
Senior Member
No extension points currently can't use DI. We'd need a special way to
create instances by routing the IEclipseContext to the creation.

IIRC there is a feature request in bugzilla but I can't remember the
name. As a replacement for an extension point one can see
ContextFunctions who can be contributed through DS.

Tom

Am 27.07.12 08:31, schrieb Stefan Wiese:
> I've created an extension point in plugin a and an extension in plugin b.
> The extension has a dependency to ECommandService, EHandlerService and
> MApplication. All dependencies are null. I tried to inject an
> IEclipseContext in ctor but it is null.
> What do I have to pay attention for when using DependencyInjection in
> extensions? Is it in general possible?
>
> Stefan
Re: Use of DI in extension [message #898681 is a reply to message #898668] Fri, 27 July 2012 03:52 Go to previous message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5296
Registered: July 2009
Senior Member
Yes that's a solution - but it doesn't allow to use constructor
injection, and @PostConstruct. I'd like to that the IExtensionRegistry
injected here is using the IEclipseContext to create instances using
ContextInjectionFactory.make.

Tom

Am 27.07.12 08:50, schrieb Stefan Wiese:
> Ah, here's the solution:
> When evaluating the extensions simply call
> ContextInjectionFactory.inject(context);
>
>
> @Inject
> public OpenSearchHandler(ISearchComponent searchComponent,
> IEclipseContext context, IExtensionRegistry registry)
> {
> IConfigurationElement[] extensions = registry
>
> .getConfigurationElementsFor("my.own.extensionpoint.definition.searchprovider");
>
>
> for (IConfigurationElement extension : extensions)
> {
> LOG.debug("Evaluating extension");
> try
> {
> final Object o =
> extension.createExecutableExtension("class");
> if (o instanceof SearchProvider)
> {
> SearchProvider searchProvider = (SearchProvider) o;
> LOG.debug("Found SearchProvider --> " +
> searchProvider.getLabel());
> ContextInjectionFactory.inject(searchProvider,
> context);
> searchComponent.addSearchProvider(searchProvider);
> }
> }
> catch (CoreException e)
> {
> LOG.error(e);
> }
> }
>
> }
>
>
> Stefan
Previous Topic:Missing Toolbar when migrating to e4
Next Topic:Eclipse 4.2 RC4 crashes on Ubuntu 10.10 (amd64)
Goto Forum:
  


Current Time: Sun Aug 31 00:33:55 EDT 2014

Powered by FUDForum. Page generated in 0.19587 seconds