问题描述
我有一个泽西资源注入了一个立面对象。这在我的 ResourceConfig
中配置,并且正面被注入罚款。该外观包含一个DAO类,它也应该被注入,并且在相同的 ResourceConfig
中配置。现在我的问题DAO类为null。因此,没有注入。 @ApplicationPath(/ service)
public class SystemSetup扩展ResourceConfig {
public SystemSetup(){
packages(false,com.foo.bar);
packages(org.glassfish.jersey.jackson);
注册(JacksonFeature.class);
final LockManager manager = getLockManager();
final SessionFactory sessionFactory = getSessionFactory();
注册(新的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门面{
@Inject
private Dao dao; //未注入
出厂实例相当简单。他们只是调用构造函数并将参数传递给它。
奇怪的是,当我使用bind(Class对象)而不是bindFactory时,这个工作绝对正常。 p>
编辑
工厂
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工厂&Facade> getFacadeFactory(){
return new Factory< Facade>(){
@Override
public Facade provide(){
return new Facade();
}
@Override
public void dispose(Facade facade){}
};
}
}
大多数Di框架就是这种情况,当你自己开始实例化的时候,你通常会把这个框架从这个方程式中踢出来。这对于 Factory
实例以及工厂创建的对象也是如此。因此, Facade
实例从来没有被框架触摸,除了将其注入资源类。
你可以可以持有 ServiceLocator
,如果您想要自己创建,则可以自己明确地注入对象。这里有几个选项。
1)将 ServiceLocator
注入 Factory
实例,然后注入
Facade
实例。
静态工厂< Facade> getFacadeFactory(){
return new Factory< Facade>(){
@Context
ServiceLocator locator;
@Override
public Facade provide(){
Facade facade = new Facade();
locator.inject(facade);
返回门面;
}
@Override
public void dispose(Facade facade){}
};
}
@Inject
public SystemSetup(ServiceLocator locator){
packages(foo.bar.rest);
packages(org.glassfish.jersey.jackson);
注册(JacksonFeature.class);
注册(新的AbstractBinder(){
@Override
protected void configure(){
bindFactory(InjectFactory.getDaoFactory())。(Dao.class) ;
工厂&Facect> factory = InjectFactory.getFacadeFactory();
locator.inject(factory);
bindFactory(factory).to(Facade.class);
}
});
}
2)或绑定
工厂
class ,让框架注入 ServiceLocator
public static class FacadeFactory implements Factory&Facade> {
@Context
ServiceLocator定位器;
@Override
public Facade provide(){
Facade facade = new Facade();
locator.inject(facade);
返回门面;
}
@Override
public void dispose(Facade facade){}
}
注册(新的AbstractBinder(){
@Override
protected void configure(){
bindFactory(InjectFactory.getDaoFactory())。(Dao.class);
bindFactory(InjectFactory.FacadeFactory.class).to(Facade .class);
}
});
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.
The strange thing is that this worked absolut fine when I used bind(Class object) rather than bindFactory.
EDIT
Factories
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) {}
};
}
}
解决方案
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.
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) 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) 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);
}
});
这篇关于注入不适用于嵌套对象[泽西2.22.1]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!