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]


- C++ 和 Java 进程之间的共享内存 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01