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);
}
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…