erdal.karaca erdal.karaca - 1 year ago 173
Java Question

bindFactory does not inject fields

I have this code to provide a custom injectable object:

config.register(new AbstractBinder() {
protected void configure() {
delegatorFactory = ...; //custom factory to delegate to
bindFactory(new Factory<Object>() {
private Provider<ContainerRequestContext> req;

public void dispose(Object arg0) {

public Object provide() {
// req is needed but is null

Unfortunately, as the comment in provide() reveals, the
field is not injected when it is executed (I need the
at that location).
What am I missing to make it work like this, i.e. make the factory's fields injected?

Answer Source

Bind it with a class. Most of time, when you start instantiating stuff yourself, you lose the benefit of injection. So just use a class



To get the injector to manually inject the factory, you can use a Feature

public class YourFeature implements Feature {
    public void configure(FeatureContext context) {
        final ServiceLocator locator = ServiceLocatorProvider.getLocator(context);


        context.register(new YourAbstractBinder());

config.register(new YourFeature());
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download