users@jersey.java.net

[Jersey] Re: CDI Injection into JAX-RS 2 Filter

From: algermissen1971 <algermissen1971_at_mac.com>
Date: Thu, 01 Aug 2013 17:34:48 +0200

On 01.08.2013, at 16:01, Jakub Podlesak <jakub.podlesak_at_oracle.com> wrote:

>
> Otherwise CDI injection should be working for just fine in GF.

Question:

I have an EJB singleton and want to inject that into a jax-rs2 filter.

How would you do that? Injection into filter does not happen - I think I need to annotate the filter, but with what?

I know that @RequestScoped does the trick on resource classes, but filters are application scoped - mostly.

Any idea?

Jan




> I am wondering why you need a custom binder. Could you please
> provide some more details on your use case. Maybe this is a bug in Jersey.
>
> Thanks,
>
> ~Jakub
>
>
> [1]https://hk2.java.net/hk2-api/apidocs/org/glassfish/hk2/utilities/binding/AbstractBinder.html
>
> On Aug 1, 2013, at 3:38 PM, algermissen1971 <algermissen1971_at_mac.com> wrote:
>
>> Hi all,
>>
>> I am again struggling with the question of how to enable CDI for JAX-RS 2 filter classes.
>>
>> Marek (IIRC) has pointed me the solution based on AbstractBinder:
>>
>> Suppose I want to inject in a filter the instance of MyToBeInjectedSingleton.class like so:
>>
>> class MyFilter .... {
>>
>> @Inject
>> MyToBeInjectedSingleton mySingleton;
>>
>> }
>>
>> I was told to extend AbstratBinder:
>>
>> public class MyBinder extends AbstractBinder {
>>
>> @Override
>> protected void configure() {
>>
>> bindAsContract(MyToBeInjectedSingleton.class).in(Singleton.class);
>>
>> }
>>
>>
>> }
>>
>> and then register the Binder in a Feature:
>>
>> public class MyFeature implements Feature {
>>
>> @Override
>> public boolean configure(FeatureContext featureContext) {
>> featureContext.register(new MyBinder());
>> return true;
>> }
>> }
>>
>> Worked fine, but it seems the API of jersey-common changed and AbstractBinder is gone. Now, I could pull in
>> the older jersey-common, but as the code is to run in GF4 (which comes with the new version), I guess this would lead to serious conflicts somewhere.
>>
>> Hence the question: what do I need to do in the new jersey-common to register an instance for injection?
>>
>>
>> Besides that, I still cannot really understand that one needs to go through this kind of trouble (with changing APIs) just because I want to inject something in a JAX-RS component. Isn't there a better way? Maybe I am just too stupid or blind to see the light.
>>
>>
>>
>> Jan
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>