Java / Jersey2 / HK: не работает распознаватель времени - PullRequest
0 голосов
/ 06 ноября 2018

Я пытаюсь реализовать JIT-резолвер для Джерси 2.27 и HK, потому что я не хочу добавлять все мои подклассы DAO в мой абстрактный механизм связывания вручную. Это моя текущая реализация:

TestService:

@Path("test")
public class TestService {

    @Inject
    TestDAO testDAO; // this should be auto injected

    @GET
    public String test() {
        return testDAO.test();
    }
}

JITResolver:

@Service
@Singleton
@Visibility(DescriptorVisibility.LOCAL)
public class JITResolver implements JustInTimeInjectionResolver {

    @Inject
    ServiceLocator serviceLocator;

    @Override
    public boolean justInTimeResolution(Injectee injectee) {
        Type type = injectee.getRequiredType();
        if (type instanceof Class) {
            Class<?> cls = (Class<?>) type;

            // here I check if type is a DAO
            if (DAO.class.isAssignableFrom(cls)) {
                ServiceLocatorUtilities.addClasses(serviceLocator, cls);
                return true;
            }
        }
        return false;
    }
}

Применение:

public class Application extends ResourceConfig {

    public Application() {
        packages("de.jt");
        // register by abstract binder
        register(ApplicationBinder.class);
    }
}

ApplicationBinder:

public class ApplicationBinder extends AbstractBinder {

    @Override
    protected void configure() {
        // bind by jit resolver
        bind(JITResolver.class).to(JITResolver.class);
    }
}

План состоит в том, что JITResolver обнаруживает неудачные инъекции и возвращает true для всех подклассов DAO, которые затем будут созданы. В этом случае я хочу автоматически ввести TestDAO. Он связан в AbstractBinder, который в свою очередь зарегистрирован в Приложении. Но когда я вызываю метод теста, я получаю обычное исключение:

1. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(    requiredType=TestDAO,parent=TestService,qualifiers={},position=-1,optional=false,self=false,unqualified=null,170935276)
2. java.lang.IllegalArgumentException: While attempting to resolve the dependencies of de.jt.rest.TestService errors were found
3. java.lang.IllegalStateException: Unable to perform operation: resolve on de.jt.rest.TestService

что я делаю не так?

...