1

I'm looking for functionality in Java similar to the .NET Managed Extensibility Framework (http://mef.codeplex.com/). For those who don't know MEF, I want something like this:

Given an interface

public interface IFoo {
 ...
}    

Dynamically load an implementation of an interface by looking in loaded jars.

IFoo foo = loadClassThatImplementsInterface<IFoo>();

The point is that the programmer does not know the name of the implementation at code time, but provides in code an extentsion point.

Is this not possible in Java at all? I found some Google hits stating that it is not possible, but this seems a bit... eh?

There are plenty of examples of using a class loader when the fully qualified name of the implementation is know at compile time. That is not what I want.

2 Answers 2

5

In 'pure' Java you can use ServiceLoader: http://download.oracle.com/javase/6/docs/api/java/util/ServiceLoader.html

You simply provide meta-data in your extension jar, that's smart and extensible easily.

Or look for a DI framework like Guice or Spring...

Sign up to request clarification or add additional context in comments.

5 Comments

For this project I don't want to use a DI framework, so I'll look into ServiceLoader, thx
Ok, don't forget to accept answer if it's what you was looking for ;-) thx
Found this after knowing what to look for: java.dzone.com/news/simple-dependency-injection-wi
So it's not quite MEF like, as we have to do more than include a jar file in the classpath... so it still sucks a bit.
Thanks for link, nice tuto ! yes you must add some code... Unfortunately not full automatic :-( perhaps in some future.
0

It's quite possible, with a loadClassThatImplementsInterface(IFoo.class).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.