All Downloads are FREE. Search and download functionalities are using the official Maven repository.

se.fortnox.reactivewizard.config.ConfigAutoBindModule Maven / Gradle / Ivy

There is a newer version: 24.6.0
Show newest version
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);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy