se.fortnox.reactivewizard.config.ConfigAutoBindModule Maven / Gradle / Ivy
package se.fortnox.reactivewizard.config;
import com.google.inject.Binder;
import se.fortnox.reactivewizard.binding.AutoBindModule;
import se.fortnox.reactivewizard.binding.scanners.ConfigClassScanner;
import javax.inject.Inject;
import javax.inject.Provider;
/**
* Binds all classes annotated with @{@link Config} to an instance bound to the data read from the config file.
*/
public class ConfigAutoBindModule implements AutoBindModule {
private final ConfigClassScanner configClassScanner;
@Inject
public ConfigAutoBindModule(ConfigClassScanner configClassScanner) {
this.configClassScanner = configClassScanner;
}
@SuppressWarnings({"rawtypes", "unchecked"})
@Override
public void configure(Binder binder) {
Provider configFactoryProvider = binder.getProvider(ConfigFactory.class);
configClassScanner.getClasses().forEach(cls -> binder.bind(cls).toProvider(configProvider((Class) cls, configFactoryProvider)));
}
private Provider configProvider(Class configCls, Provider configFactoryProvider) {
return () -> configFactoryProvider.get().get(configCls);
}
}