Java / Jersey2 / HK:即时注入解析器不起作用

时间:2018-11-06 17:16:06

标签: java jersey hk2

我正在尝试为Jersey 2.27和HK实现JIT解析器,因为我不想手动将所有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检测到失败的注入并为DAO的所有子类返回true,然后将其创建。在这种情况下,我想自动注入TestDAO。它绑定在AbstractBinder中,而AbstractBinder则在Application中注册。但是当我调用测试方法时,我得到了通常的异常:

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

我在做什么错了?

0 个答案:

没有答案