我有一个如下所示的构造函数:
@Inject
public MyClass(
@Named("config") String configFile,
@Named("template") String templateFile,
CachedSettings settings,
@Assisted String channelId,
@Nullable @Assisted("NetworkA") NetworkInterface localNetworkInterfaceA,
@Nullable @Assisted("NetworkB") NetworkInterface localNetworkInterfaceB) {
我收到以下错误(两次,每个参数一次)
1) null returned by binding at my.company.package.MyClassFactory.create()
but parameter 4 of my.company.package.MyClass.<init>() is not @Nullable
while locating java.net.NetworkInterface annotated with @com.google.inject.assistedinject.Assisted(value=NetworkA)
for parameter 4 at my.company.package.MyClass.<init>(MyClass.java:24)
while locating my.company.package.MyClass annotated with interface com.google.inject.assistedinject.Assisted
知道出了什么问题吗?我发现了关于这个问题的另外两个问题,其中一个说这是一个dependency issue我想我没有,另一个说这是一个 Eclipse issue ,我确实使用过,但我从头开始刷新、清理和重建了我的 Maven 项目,所以我不确定问题是什么。
我正在使用javax.annotation.Nullable ,应该在运行时保留。我还应该尝试什么?
最佳答案
@Nullable
需要在构造函数和工厂声明上设置。我们使用 FactoryModuleBuilder
来声明我们的工厂。我正在粘贴对我们有用的相关代码
构造函数:
@Inject
public AddressActions(EC2Service ec2Service,
RequestFactory requestFactory,
@Assisted("spiceManager") SpiceManager spiceManager,
@Assisted("parent") Context parent,
@Assisted("publicIp") @Nullable String publicIp) {
抽象工厂:
public static interface Factory {
AddressActions create(@Assisted("spiceManager") SpiceManager spiceManager,
@Assisted("parent") Context context,
@Assisted("publicIp") @Nullable String publicIp);
}
工厂模块生成器:
install(new FactoryModuleBuilder().build(AddressActions.Factory.class));
通话:
AddressActions actions = actionsFactory.create(spiceManager, getSherlockActivity(), null);
相关版本:
- Guice:
guice-3.0-no_aop.jar
- AssistedInject:
guice-assistedinject-3.0.jar
- JSR305:
jsr305-1.3.9.jar
-k
关于java - @Nullable 似乎不适用于 @AssistedInject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15931215/