简体   繁体   中英

Spring Boot with Jax-RS with Open Tracer gets UnsatisfiedDependencyException

Spring boot, Jax-RS + Open Tracer

In my gradle deps:

   plugins {
       id 'java'
       id 'org.springframework.boot' version '2.2.2.RELEASE'
       id "io.spring.dependency-management" version "1.0.8.RELEASE"
    }

    implementation("org.springframework.boot:spring-boot-starter-jersey") ...
    implementation("io.opentracing.contrib:opentracing-jaxrs2-discovery:1.0.0")  // <---

The problem starts when I try to inject the Tracer:

@Path("/my)
@Produces(MediaType.APPLICATION_JSON)
public class MyResource { ...
    @Inject
    Tracer tracer;

I tried several method to add the Trance:

https://github.com/opentracing-contrib/java-jaxrs

The last I tried is: (the @WebListener one I tried too)

@Provider
public class TracingInitializer implements DynamicFeature {

  private final ServerTracingDynamicFeature serverTracingDynamicFeature =
      new ServerTracingDynamicFeature.Builder(GlobalTracer.get())
          .withOperationNameProvider(ClassNameOperationName.newBuilder())
      .build();

  @Override
  public void configure(ResourceInfo resourceInfo, FeatureContext context) {
    serverTracingDynamicFeature.configure(resourceInfo, context);
  }
}

Then injecting it in:

    @Named
    public final class JerseyConfig extends ResourceConfig { ...
           @Inject
           public JerseyConfig() {

register(TracingInitializer.class);

On start I end up having this exception:

org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=Tracer,parent=MyResource,qualifiers={},position=-1,optional=false,self=false,unqualified=null,124873055)

The hk2' @Inject worked Ok before adding the open-tracing dep.

Q: Any idea how to make it work? To be able to inject the Tracer.

Ok. The what I did is:

  1. replaced all my renaming spring @Service annotations to @Named ones.
  2. added the GlobalTracerBinder class:

--

 public class GlobalTracerBinder extends AbstractBinder {

    @Override
    protected void configure() {
        bind(GlobalTracer.get()).to(Tracer.class);
    }

}
  1. registered it in class JerseyConfig extends ResourceConfig {

like this:

register(new GlobalTracerBinder());

Then I could see that the tracer object got injected. (not sure if that Tracer is what I need, i see it returns "" as trace id. But I guess it is another question.)

Please fee free to come with better solution.


Arguably, I would say: if one is not restricted: use either spring/controller or not spring + jax-rs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM