Is it possible to construct an object partially with Spring DI and partially with Jersey DI?

368 views Asked by At

I have a class JerseyWebService which uses Jersey DI to inject dependency

@Path("/baskets")
public class JerseyWebService {
    @Inject
    ExternalApiServiceInterface api;
    ...
}

The dependency is specified in the binder

public class CustomBinder extends AbstractBinder {
    @Override
    protected void configure() {   
       bind(ExternalApiService.class).to(ExternalApiServiceInterface.class);
       ...
    }

But the problem here is that ExternalApiService has other dependencies and it uses Spring to inject them.

class ExternalApiService implements ExternalApiServiceInterface{
    @Autowired
    AnotherService aservice;

Is it possible to specify only some dependencies in binder which Jersey will Inject and other dependencies being injected by Spring ?

If not ,then if had been @Inject instead of @Autowired in ExternalApiService would it be mandatory to specify all bindings in the binder class?

Does Jersey DI has no Autowiring like feature or delegate injecting a dependency to Spring if it can't find any binding?

1

There are 1 answers

0
Paul Samsotha On BEST ANSWER

It should work. Given you have the required Spring-Jersey integration dependency[1] and have correctly configured the application[2]

1. See Spring DI support in Jersey
2. See official Jersey Spring example

What happens is HK2 (Jersey's DI framework) will look for an InjectionResolver for the @Autowired annotation, in order to resolve the dependency. The jersey-spring3 dependency has the AutowiredInjectionResolver, which holds a reference to Spring's ApplicationContext. From there it's just matter of looking it up in the application context, to resolve the dependency.