这是我的主要课程:
static Logger logger;
static StringUtils stringUtils;
//java -cp ... <VM argument> main.class.Name <program arguments>
public static void main(String... args) throws ClassNotFoundException, IOException {
logger = InjectorSingleton.getInstance().guiceInjector.getInstance(Logger.class);
stringUtils = InjectorSingleton.getInstance().guiceInjector.getInstance(StringUtils.class);
}
这是我的模块类:
public class AppInjector extends AbstractModule {
@Override
protected void configure() {
bind(Logger.class);
}
}
我的记录器类是:
public class Logger {
private StringUtils stringUtils;
public boolean isDebug;
@Singleton
@Inject
public Logger(StringUtils stringUtils) {
this.stringUtils = stringUtils;
setIsVerbose();
}
public class StringUtils {
Logger logger;
@Singleton
@Inject
public StringUtils(Logger logger) {
this.logger = logger;
}
我运行主类并获得此异常:
1) Tried proxying com.w.monitoring.headlessclient.utils.Logger to support a circular dependency, but it is not an interface.
while locating com.w.monitoring.headlessclient.utils.Logger
for parameter 0 at com.w.monitoring.headlessclient.utils.StringUtils.<init>(StringUtils.java:21)
while locating com.w.monitoring.headlessclient.utils.StringUtils
for parameter 0 at com.w.monitoring.headlessclient.utils.Logger.<init>(Logger.java:14)
while locating com.w.monitoring.headlessclient.utils.Logger
1 error
at com.google.inject.internal.InjectorImpl$3.get(InjectorImpl.java:1014)
at com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1040)
我将配置更改为:
@Override
protected void configure() {
bind(Logger.class).toInstance(new Logger());
}
但是我应该如何初始化特定实例(缺少对ctor的args)
我应该在guice配置方法中修复什么?