3

I've a Configuration.java singleton class (with many properties loaded by a file) that I would like to inject in some classes in my Application.

So, I bind my injection in the ResourceConfig class with an AbstractBinder.

Now, I need to use this Configuration class in this ResourceConfig.

For example, in my Configuration class there is a property named "packages", that I have to use in ResourceConfig class in order to register package.

The issue is that the injection is not starting in the ResourceConfig class.

class Configuration {

    //many properties

    String packages = "";
}

class MyResourceConfig extends ResourceConfig {

    @Inject
    Configuration configuration;

    MyResourceConfig() {
       ...

       register(MyBinder.class); //with many injection

       ...

       packages(configuration.packages); 
   }
}

So could you please advice me how to have this lifecycle ? (maybe I have to use another jersey class ?)

Guillaume
  • 694
  • 1
  • 6
  • 15

1 Answers1

0

I realize it is a sort of a workaround, but you can achieve it by using static access to the CDI singleton object.

Something like:

@ApplicationPath("/rest")
public class RestApplication extends ResourceConfig {
    public RestApplication() {
        super();
        super.packages(true, "com.package.name");
        register(JacksonFeature.class);
        register(JacksonObjectMapperProvider.class);

        ModelConverters.getInstance().addConverter(
                    new CustomObjectMapperModelResolver(getObjectMapperProvider()));
                               // get the instance here ^
        OpenApiResource openApiResource = new OpenApiResource();
        register(openApiResource);
        AcceptHeaderOpenApiResource ahoar = new AcceptHeaderOpenApiResource();
        register(ahoar);
    }

    private static JacksonObjectMapperProvider getObjectMapperProvider() {
        return CDI.current().select(JacksonObjectMapperProvider.class).get();
    }
}                                       
helvete
  • 2,455
  • 13
  • 33
  • 37