Ich benutze PlayFramework (Java) und Guice für DI, zusammen mit Pac4j. So sieht mein Guice-Modul basierend auf pac4j demo app aus. Im Code übergebe ich eine CustomAuthentication, in die ich ein SericeDAO-Objekt injiziere, jedoch ist es immer null.Inject Service-Objekt in guice Modul
Zunächst dachte ich, weil ich CustomAuthenticator erstellen wollte, anstatt es zu erstellen, weshalb es null war. Ich habe auch versucht, CustomAuthenticator direkt in das Sicherheitsmodul zu injizieren - das customAuthenticator-Objekt war dann null. Ich bin mir nicht sicher, was ich falsch mache.
public class SecurityModule extends AbstractModule {
private final Environment environment;
private final Configuration configuration;
public SecurityModule(
Environment environment,
Configuration configuration) {
this.environment = environment;
this.configuration = configuration;
}
@Override
protected void configure() {
final String baseUrl = configuration.getString("baseUrl");
// HTTP
final FormClient formClient = new FormClient(baseUrl + "/loginForm", new CustomAuthenticator());
final Clients clients = new Clients(baseUrl + "/callback", formClient);
final Config config = new Config(clients);
bind(Config.class).toInstance(config);
}
}
CustomAuthenticator impl:
public class CustomAuthenticator implements UsernamePasswordAuthenticator {
@Inject
private ServiceDAO dao;
@Override
public void validate(UsernamePasswordCredentials credentials) {
Logger.info("got to custom validation");
if(dao == null) {
Logger.info("dao is null, fml"); // Why is this always null? :(
}
}
}
Die ServiceDAO ist bereits Setup als guice Modul
public class ServiceDAOModule extends AbstractModule {
@Override
protected void configure() {
bind(ServiceDAO.class).asEagerSingleton();
}
}
Danke. Das hat sehr geholfen. – Raunak