java ee - CDI: ContainerRequestFilter properties not injected -
i've created containerrequestfilter
implementation. i'm facing misunderstanding don't quite solve.
this implementation:
@provider @prematching @secured public class bearerfilter implements containerrequestfilter { @context private httpservletrequest request; @override public void filter(containerrequestcontext requestcontext) throws ioexception { //this.request null here } }
in order register on jaxrs application:
@applicationpath(value = "cmng") public class restapplication extends application { @override public set<class<?>> getclasses() { set<class<?>> resources = new hashset<class<?>>(); resources.add(accountendpoint.class); //... return resources; } @override public set<object> getsingletons() { set<object> singletons = new hashset<object>(); singletons.add(new bearerfilter()); <<<<< manually created object. return singletons; } }
so, bearerfilter
object created manually, code. problem appears here since dependency injection works on instances created , managed container itself.
so i'm not able inject objects inside containerrequestfilter
since it's not created or managed object container itself.
how solve that?
i'm using jaxrs.
the @context
annotation jax-rs , it's not related cdi in way. available types can injected @context
can seen in answer.
according getsingletons()
method documentation, injection should work:
get set of root resource, provider , feature instances. fields , properties of returned instances injected declared dependencies (see
context
) runtime prior use.
but can use getclasses()
method register filter:
@override public set<class<?>> getclasses() { set<class<?>> classes = new hashset<class<?>>(); classes.add(accountendpoint.class); classes.add(bearerfilter.class) return classes; }
from documentation:
get set of root resource, provider , feature classes. default life-cycle resource class instances per-request. the default life-cycle providers (registered directly or via feature) singleton.
Comments
Post a Comment