Missing object accessible for injection error when using bindFactory

I have the following implementation for a Jersey application (2.18):

public class RootApplication extends ResourceConfig {

    public RootApplication() {
        packages("com.foo.bar");

        register(new AbstractBinder() {

            @Override
            protected void configure() {       
                bindFactory(RepositoryFactory.class).to(Repository.class);

                // if I use following line instead of bindFactory it works
                // bind(OracleRepository.class).to(Repository.class);
            }
        });
    }

    public class RepositoryFactory implements Factory<Repository> {

        private final Repository repo;

        public RepositoryFactory() {
            this.repo = new OracleRepository();
        }

        @Override
        public Repository provide() {
            return repo;
        }

        @Override
        public void dispose(Repository repo) {
        }
    }
}

      

and get below exception when hitting the service that enters the repository

javax.servlet.ServletException: A MultiException has 3 exceptions.  They are:
1. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=Repository,parent=MeasureService,qualifiers={},position=-1,optional=false,self=false,unqualified=null,56464420)
2. java.lang.IllegalArgumentException: While attempting to resolve the dependencies of com.fidelity.pi.dashboard.rest.MeasureService errors were found
3. java.lang.IllegalStateException: Unable to perform operation: resolve on com.fidelity.pi.dashboard.rest.MeasureService

      

Everything works if I comment bindFactory

and use commented binding. Am I missing something in terms of Factory implementation? The exception seems to occur even before the constructor is removed RepositoryFactory

. I need a Factory since I have a different initialization for the instance OracleRepository

.

+3


source to share


1 answer


The only way I was able to reproduce the problem (with your incomplete information i.e. missing injection point) was to try to type OracleRepository

instead Repository

. I don't have the exact reason why the injection is failing, but my guess is because you are binding Repository

, not OracleRepository

. If this is the problem, the simplest solution would be to bind the factory to, OracleRepository

or just insert instead Repository

.

For injection Repository

, if you want to qualify different implementations, you can do it by binding named

or qaulifiedBy

by binding as in the example below (where I used named

and annotated injection with @Named

).

In this example, I used the Jersey test framework

<dependency>
    <groupId>org.glassfish.jersey.test-framework.providers</groupId>
    <artifactId>jersey-test-framework-provider-inmemory</artifactId>
    <version>${jersey2.version}</version>
    <scope>test</scope>
</dependency>

      



Here's a complete test. You can change @Named

between "Sql"

and "Oracle"

to see the difference.

import javax.inject.Inject;
import javax.inject.Named;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Response;
import org.glassfish.hk2.api.Factory;
import org.glassfish.hk2.utilities.binding.AbstractBinder;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Assert;
import org.junit.Test;

public class InjectionTest extends JerseyTest {

    @Path("test")
    public static class SimpleResource {

        @Inject
        @Named("Oracle")
        private Repository repo;

        @GET
        public String getRepoName() {
            return repo.getClass().getSimpleName();
        }
    }

    @Override
    public Application configure() {
        ResourceConfig config = new ResourceConfig();
        config.register(SimpleResource.class);
        config.register(new AbstractBinder(){
            @Override
            protected void configure() {
                bindFactory(SqlFactory.class)
                        .to(Repository.class).named("Sql");
                bindFactory(OracleFactory.class)
                        .to(Repository.class).named("Oracle");
            }
        });
        return config;       
    }


    public static interface Repository {}

    public static class OracleRepository implements Repository {}

    public static class SqlRepository implements Repository {}

    public static class SqlFactory implements Factory<Repository> {

        @Override
        public Repository provide() {
            return new SqlRepository();
        }

        @Override
        public void dispose(Repository t) {}
    }

    public static class OracleFactory implements Factory<Repository> {

        @Override
        public Repository provide() {
            return new OracleRepository();
        }

        @Override
        public void dispose(Repository t) {}
    }

    /**
     * Change the `Assert` from "OracleRepository" to "SqlRepository"
     * when switching the `@Named` on the injection point.
     */
    @Test
    public void testInjectOk() {
        Response response = target("test").request().get();
        String respString = response.readEntity(String.class);
        Assert.assertEquals("OracleRepository", respString);
        System.out.println(respString);
    }
}

      

If you still have a problem, please post a complete test case of one class as shown above, which demonstrates the problem.

+2


source







All Articles