- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 Guice 3.0 AssistedInject
,它不会实例化工厂。
SSCCE代码:
public class ParentClass() {
@Inject private MyFactory myFactory;
private final Foo foo;
private final Bar bar;
public ParentClass() {
if(myFactory == null) System.err.println("Error: I should have injected by now!");
foo = myFactory.create(new Map<String, Object>());
// etc.
}
}
public interface MyFactory {
Foo create(Map<String, Object> mapA);
Bar create(Map<String, Object> mapB, Map<String, Object> mapC);
}
public class ParentModule extends AbstractModule {
@Override
protected void configure() {
install(new FactoryModuleBuilder()
.implement(Foo.class, FooImpl.class)
.implement(Bar.class, BarImpl.class)
.build(MyFactory.class));
}
public class FooImpl implements Foo {
private final Map<String, Object> mapA;
@AssistedInject
public FooImpl(@Assisted Map<String, Object> mapA) {
this.mapA = mapA;
}
}
BarImpl
与 FooImpl
非常相似。这里出了什么问题?另请注意,我在这里尝试了 @AssistedInject
和 @Inject
,两者都会导致错误。
输出:
Error: I should have injected by now!
Exception in thread "main" com.google.inject.ProvisionException: Guice provision errors:
1) Error injecting constructor, java.lang.NullPointerException
at ParentClass.<init>(ParentClass.java:7)
while locating com.my.package.ParentClass
1 error
at com.google.inject.internal.InjectorImpl$4.get(InjectorImpl.java:987)
at com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1013)
at com.my.package.ParentMain.main(ParentMain.java:16)
Caused by: java.lang.NullPointerException
at com.my.package.ParentClass.<init>(ParentClass.java:9)
at com.my.package.ParentClass$$FastClassByGuice$$d4b3063a.newInstance(<generated>)
at com.google.inject.internal.cglib.reflect.$FastConstructor.newInstance(FastConstructor.java:40)
... 8 more
请注意第 9 行是第一次调用 myFactory.create()
最佳答案
根据 Guice 的 javadoc ,字段注入(inject)在构造函数注入(inject)之后执行。
我假设您的 ParentClass
实例是由 Guice 创建的。当您的 ParentClass
的构造函数被执行时,它的 myFactory
字段还没有被注入(inject)。
关于java - Guice AssistedInject 不会注入(inject)工厂,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14760219/
我有很多 TabularResultSet 类的带注释的命名实例,我想将它们传递给多个类并传递给静态最终属性。我该如何注入(inject)?我认为它不喜欢该属性的最终和静态性质。必须在属性声明中直接调
我是 Guice 的新手,我想知道我能走多远。 我有一个接口(interface)UserInfo具有多个实现类 GoogleUserInfo , FacebookUserInfo , Twitter
我的应用程序中绑定(bind)了两个类型的命名实例: bind(Foo.class).toProvider(FooProvider.class); bind(Foo.class).annotatedW
我有一个 Guice 模块,它有一个 @Provides 方法,它接受 2 个参数并返回接口(interface)的实现: public class **ClientModule** extends
请解释在以下使用工厂的场景中使用 Google-Guice 自动连接的正确方法。我正在使用 XML 工厂进行 XSLT 处理。 StringWriter strWriter = new StringW
我有一个 Guice Module提供 List使用 @Provides - 带注释的方法。 class TestModule() : Module { override fun configur
为什么Guice 3.0针对错误的配置组件(例如,缺少@Inject)抛出此异常,而不是格式化消息? Exception in thread "main" com.google.inject.inte
我有以下供应商: public class GuiceResourceProvider implements Provider { @Inject private Configur
使用 Guice-servlet,我们可以轻松地进行 servlet 映射,如下所示: filter(*.jsp).through(MyFilter.class) 但是,谁能告诉我如何将过滤器映射到
我正在尝试向我的项目添加 Swagger 。我们的设置与示例项目略有不同。我们使用 guice 和 guice-servlet 来注入(inject)并启动我们的 JerseyServletModul
假设我有一个 Guice 模块 ProdModule,我想依赖其他 GuiceModule、ProdDbModule 和 ProdPubSubModule。我将如何实现 ProdModule 的 co
我正在考虑在我的应用程序中使用 Guice for DI,我应该能够在运行时交换实现。下面提供了一个示例来说明要求: class ValidationEngine { public void v
我想设置 Guice 绑定(bind),所以我用 Java 创建了一个完美运行的模块: public class CrashLoggerModule extends AbstractModule {
所以我有一个模块将接口(interface)与实现类绑定(bind)。 bind(ILocalStore.class).to(LocalStore.class); 此绑定(bind)是否还会注入(in
guice 4.0 是否向后兼容 3.x?无法从发行说明或常见问题解答中弄清楚... 如果没有,是否有兼容性问题列表? 最佳答案 我没有任何官方来源但根据我的经验,没有任何兼容性问题。 我使用了几个
我正在尝试使用 Guice,并且我来自 Spring。 我想知道 @Inject 是否相当于 Spring 中的 @Autowired 以及我是否可以在 Web 应用程序中使用它,就像在 Spring
我是 Guice DI 的新手。我想弄清楚我的情况。 简单来说,有没有通过Guice @annotations来替代MapBinder的? 我的场景: Interface A{} Class A1 i
我的项目正在使用 Guice作为负责为大型对象图(主要是单例)提供依赖项(服务类)的 IOC 容器。有时,如果在构造过程中依赖项失败,并且许多对象都需要此依赖项,则失败将一遍又一遍地发生,并将异常添加
我有一个类 (CustomConnectionProvider),它将由第三方库 (hibernate) 使用 class.forName().newInstance() 实例化。我需要注入(inje
删除 guice servlet 后,我需要进行一些清理。使用 guice servlet 时是否可以 Hook 到 servlet 破坏?我需要使用喷油器进行清理工作。 我可以覆盖 contex
我是一名优秀的程序员,十分优秀!