Inject not working for nested objects[Jersey 2.22.1](注入不适用于嵌套对象[Jersey 2.22.1])
问题描述
我有一个 Jersey 资源,其中注入了外观对象.这是在我的 ResourceConfig
中配置的,并且外观可以很好地注入.外观包含一个 DAO 类,它也应该被注入并配置在相同的 ResourceConfig
中.现在我的问题;DAO 类为空.因此,没有注入.
I have a Jersey resource with a facade object injected. This is configured in my ResourceConfig
and the facade gets injected fine. The facade contains a DAO class which also should be injected and is configured in the same ResourceConfig
. Now to my problem; the DAO class is null. Thus, not injected.
@ApplicationPath("/service")
public class SystemSetup extends ResourceConfig {
public SystemSetup() {
packages(false, "com.foo.bar");
packages("org.glassfish.jersey.jackson");
register(JacksonFeature.class);
final LockManager manager = getLockManager();
final SessionFactory sessionFactory = getSessionFactory();
register(new AbstractBinder() {
@Override
protected void configure() {
bindFactory(InjectFactory.getDaoFactory(sessionFactory)).to(Dao.class).in(Singleton.class);
bindFactory(InjectFactory.getFacadeFactory(manager)).to(Facade.class).in(Singleton.class);
}
});
}
<小时>
@Path("/")
@Produces("text/json")
public class ViewResource {
@Inject
private Facade logic;
<小时>
public class Facade {
@Inject
private Dao dao; //Not injected
工厂实例相当简单.他们只需调用构造函数并将参数传递给它.
The factory instances are rather simple. They simply call the constructor and pass the argument to it.
奇怪的是,当我使用 bind(Class object) 而不是 bindFactory 时,这绝对可以正常工作.
The strange thing is that this worked absolut fine when I used bind(Class object) rather than bindFactory.
编辑
工厂
class InjectFactory {
static Factory<Dao> getDaoFactory() {
return new Factory<Dao>() {
@Override
public Dao provide() {
return new Dao(new Object());
}
@Override
public void dispose(Dao dao) {}
};
}
static Factory<Facade> getFacadeFactory() {
return new Factory<Facade>() {
@Override
public Facade provide() {
return new Facade();
}
@Override
public void dispose(Facade facade) {}
};
}
}
推荐答案
与大多数 Di 框架一样,当您开始自己实例化事物时,通常情况下您将框架排除在外.这适用于 Factory
实例以及工厂创建的对象.所以 Facade
实例永远不会被框架接触,除非将它注入到资源类中.
As is the case with most Di frameworks, when you start instantiating things yourself, it's often the case that you are kicking the framework out of the equation. This holds true for the Factory
instances, as well as the objects the factory creates. So the Facade
instance never gets touch by the framework, except to inject it into the resource class.
你可以持有ServiceLocator
,如果你想自己创建对象,你可以自己显式地注入对象.这里有几个选项.
You can can a hold of the ServiceLocator
, and explicitly inject objects yourself if you want to create them yourself. Here are a couple options.
1) 将ServiceLocator
注入Factory
实例,然后注入Facade
实例.
1) Inject the ServiceLocator
into the Factory
instance, then inject the Facade
instance.
static Factory<Facade> getFacadeFactory() {
return new Factory<Facade>() {
@Context
ServiceLocator locator;
@Override
public Facade provide() {
Facade facade = new Facade();
locator.inject(facade);
return facade;
}
@Override
public void dispose(Facade facade) {}
};
}
@Inject
public SystemSetup(ServiceLocator locator) {
packages("foo.bar.rest");
packages("org.glassfish.jersey.jackson");
register(JacksonFeature.class);
register(new AbstractBinder() {
@Override
protected void configure() {
bindFactory(InjectFactory.getDaoFactory()).to(Dao.class);
Factory<Facade> factory = InjectFactory.getFacadeFactory();
locator.inject(factory);
bindFactory(factory).to(Facade.class);
}
});
}
2) 或者绑定一个Factory
class,让框架注入ServiceLocator
2) Or bind a Factory
class, and let the framework inject the ServiceLocator
public static class FacadeFactory implements Factory<Facade> {
@Context
ServiceLocator locator;
@Override
public Facade provide() {
Facade facade = new Facade();
locator.inject(facade);
return facade;
}
@Override
public void dispose(Facade facade) {}
}
register(new AbstractBinder() {
@Override
protected void configure() {
bindFactory(InjectFactory.getDaoFactory()).to(Dao.class);
bindFactory(InjectFactory.FacadeFactory.class).to(Facade.class);
}
});
这篇关于注入不适用于嵌套对象[Jersey 2.22.1]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:注入不适用于嵌套对象[Jersey 2.22.1]


基础教程推荐
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 降序排序:Java Map 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01