我正在尝试在 Guice 中创建一个 @Named Integer
参数,它允许在没有值适用的情况下使用 null 值:
@Singleton
public class MyClass extends SomeOtherClass {
@Inject
MyClass(Object o, @Named("the name") @Nullable Integer myParam) {
...etc...
}
}
和
@Provides
@Named("the name")
public Integer getParamName() {
// Set to null for Not Applicable
return null;
}
不幸的是,Guice 似乎不喜欢这样:
null returned by binding at my.package.ApplicationConfig$MyModule.getParamName()
but parameter 1 of my.package.MyClass.<init>() is not @Nullable
我正在使用的 @Nullable
注释是 com.google.inject.internal.Nullable
,它包含在 Guice 中并且确实有 @Retention( RetentionPolicy.RUNTIME)
.
不幸的是,我在 Stack Overflow 上发现的类似问题要么没有建设性,要么不适用。有什么想法吗?
最佳答案
我同意@yshavit。用 null
表示状态通常不是一个好主意。但是你应该能够让它工作。尝试改用 javax.annotation.Nullable
。你可以在这里找到一个实现:
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>2.0.1</version>
<type>jar</type>
</dependency>
关于java - Guice:@Nullable 拒绝与@Named 一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17551843/