Playframework 将服务对象注入guice模块
我正在使用PlayFramework(java)和GUI for DI,以及pac4j。这就是我的Guice模块基于的外观。在代码中,我传递了一个CustomAuthentication,在其中我注入了一个seriedao对象,但是它总是null 最初的想法是因为我正在创建CustomAuthenticator,而不是让guice创建它,所以它是空的。我还尝试将CustomAuthenticator直接注入到安全模块中-CustomAuthenticator对象随后为null。不知道我做错了什么Playframework 将服务对象注入guice模块,playframework,dependency-injection,guice,playframework-2.4,Playframework,Dependency Injection,Guice,Playframework 2.4,我正在使用PlayFramework(java)和GUI for DI,以及pac4j。这就是我的Guice模块基于的外观。在代码中,我传递了一个CustomAuthentication,在其中我注入了一个seriedao对象,但是它总是null 最初的想法是因为我正在创建CustomAuthenticator,而不是让guice创建它,所以它是空的。我还尝试将CustomAuthenticator直接注入到安全模块中-CustomAuthenticator对象随后为null。不知道我做错了什么
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? :(
}
}
}
ServiceDAO已设置为guice模块
public class ServiceDAOModule extends AbstractModule {
@Override
protected void configure() {
bind(ServiceDAO.class).asEagerSingleton();
}
}
您的代码中有两个错误 首先,使用new构造的任何内容都不会使用@injectworking进行注入。 其次,不能将内容注入模块 要解决这个问题,请像这样重构代码
谢谢这帮了大忙。