Michael Nelson
Michael Nelson

Reputation: 535

Jersey 2.x dynamic binding

Is there any way to provide a dynamic binding at runtime? AbstractBinder allows you to bind a factory but the class type has to be known at compile time.

Something to the effect of:

public class MyDynamicBinder implements DynamicBinder {
    @Override
    public boolean canBind(Class<?> someContract) {
        return iCanBindThis(someContract);
    }

    @Override
    public Object bind(Class<?> someContract) {
        return getMyInstance(someContract);
    }
}

Upvotes: 1

Views: 475

Answers (1)

jwells131313
jwells131313

Reputation: 2394

hk2 is an extremely dynamic injection container. Unlike guice and CDI it can add services to its ServiceLocator at any time (if you have appropriate privilege). With the ServiceLocator (which is a service available anywhere you have any other hk2 service) then you can use the methods in ServiceLocatorUtilities like addClasses or addOneDescriptor in order to add whatever services you need at any time at all. You can also remove services at any time, but few people ever do that (unless you are writing a dynamic container of your own)

Upvotes: 1

Related Questions