0 Replies Latest reply on Aug 26, 2012 11:41 AM by Christopher Piggott

    Configuring jersey subresources

    Christopher Piggott Newbie

      Hi,

       

      I have some jersey subresources that are @RequestScoped.  They need a litlte configuration before they can do their job.

       

      The way I normally like to do this with CDI is to create a factory, something like:

       

          @ApplicationScoped

          public static class SubResource.Factory {

              @Inject Instance<SubResource> subFactory;

       

               public SubResource create( String someConfigItem ) {

                      SubResource sub = subFactory.get();

                      sub.someConfigItem = subConfigItem; /* possible because this factory is nested in SubResource */

                      return sub;

                  }

             }

       

       

      However, if I create my subresource this way I don't get @Context injections.  So, as an alternative, I have been doing this instead:

       

          @Context ResourceContext resourceContext;

       

          @Path("subResource") public SubResource getSubResource() {

                SubResource sub = resourceContext.getResource( SubResource.class );

                sub.setSomeConfigItem( "value ");

                return sub;

            }

       

       

      Neither one of these is optimal.  I really don't want to add a setter; I want that configuration to be effectively immutable.  I can implement my own "set once" in the setter, but that's also a little messy.  What I really want is a way to say: "Within this request scope, when an object tries to @Inject something called X, here is what you should give it" in a programmatic way.

       

      Is this possible?