我正在使用Guice
从conf文件中读取属性@Inject
@Named("test.var1")
private int var1 = 2;
这里,conf文件中test.var1的值正在成功注入var1。如果文件中缺少此属性,我希望默认情况下将其设置为2。但是,当这个属性" test.var1"在属性文件中丢失,我收到编译错误。
Explicit bindings are required and java.lang.Integer annotated with @com.google.inject.name.Named(value=test.var1) is not explicitly bound.
while locating java.lang.Integer annotated with @com.google.inject.name.Named(value=test.var1)
for parameter 6 at test1.TestParser.<init>(TestParser.java:62)
at test1.CarbonServerModule.configure(CarbonServerModule.java:41)
1 error
at com.google.inject.internal.Errors.throwCreationExceptionIfErrorsExist(Errors.java:435)
at com.google.inject.internal.InternalInjectorCreator.initializeStatically(InternalInjectorCreator.java:154)
at com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:106)
at com.google.inject.Guice.createInjector(Guice.java:95)
at com.google.inject.Guice.createInjector(Guice.java:72)
at test1.Main.<init>(Main.java:198)
at test1.Main.main(Main.java:244)
有人可以帮助我解决我想在这里做的事情吗?
答案 0 :(得分:0)
如错误消息所示,您已将Guice配置为require explicit bindings,因此如果未明确声明已知绑定,则会失败。删除该要求(即允许隐式绑定)和/或标记@Inject
注释optional就足够了。