de.devsurf.injection.guice.serviceloader.SingleServiceLoaderProvider Maven / Gradle / Ivy
The newest version!
package de.devsurf.injection.guice.serviceloader;
import java.util.Iterator;
import java.util.ServiceLoader;
import com.google.inject.Inject;
import com.google.inject.Provider;
public final class SingleServiceLoaderProvider implements Provider {
private final Class type;
@Inject
com.google.inject.Injector injector;
public SingleServiceLoaderProvider(Class type) {
this.type = type;
}
/*
* Generated classes can't be used with AOP, because only instances created
* by Guice are extended with AOP. Maybe this will be fixed later in Guice.
* @see com.google.inject.Provider#get()
*/
@Override
public T get() {
ServiceLoader services = ServiceLoader.load(type);
Iterator iterator = services.iterator();
if(iterator.hasNext()){
T instance = iterator.next();
injector.injectMembers(instance);
return instance;
}
return null;
}
public static Provider of(Class type) {
return new SingleServiceLoaderProvider(type);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy