Uri
Uri

Reputation: 89799

Is it possible to use Spring within Eclipse plugins?

Is it possible to use a Spring container for DI from inside Eclipse plugins?

I'm wondering because I know that Eclipse causes a lot of issues with class loading, looking up things within the plugin, etc.

The plugin is intended to be distributed as a JAR.

Upvotes: 1

Views: 1330

Answers (3)

greg
greg

Reputation:

do you have a code example for your post? This would be great, since I´m hanging around with this for a while.

Cheers!

Upvotes: 0

zvikico
zvikico

Reputation: 9825

The answer is yes. You can use Spring DM, but you don't have to. It is probably better with it.

I did it without Spring DM and the main concern is class loading issues (not sure if Spring DM solves them, but I guess it should). Assuming you bundle the Spring JAR in a separate plugin with dependencies, you will need to load the context with the class loader of the invoking plugin .

Example:

  • Plugin A - your functional plugin
  • Plugin B - The Spring lib plugin exporting the spring packages

Plugin A depends on B. When plugin A starts, it will load the application context, when invoking this load, you will need to do something like:

Thread.currentThread().setContextClassLoader(PluginAActivator.class.getClassLoader())

So that the loading of the classes will happen under your own class loader. Now you can use a ClassPathXmlApplicationContext to load configuration XMLs from your class path.

One small note: the default ClassPathXmlApplicationContext validates your XMLs upon loading. You may want to disable it or point your XMLs to a local schema (rather than the standard Spring schema on springframework.org), otherwise, you will connect to the internet to download the schema files upon loading and working offline will fail.

Upvotes: 1

ashitaka
ashitaka

Reputation: 3918

Yes but you will need Spring DM http://www.springsource.org/osgi

Upvotes: 1

Related Questions