Hi
On 02/10/13 16:03, Winnebeck, Jason wrote:
I am using CXF + Spring.

I'm having a lot of problems getting confused by the two separate injection 
systems. I have a resource class created by Spring (A), and it requires a 
dependency (Dep). Normally I can have Dep injected into Res with @AutoWired, 
and this works fine. But I want the Dep to have a reference to UriInfo. Ideally 
I want this:

@Path("/")
class A {
   @Autowired
   public A(Dep dep) { /* save dep */ }

   @GET
   public String get() { return ""; }
}

class Dep {
   @Autowired
   public Dep(UriInfo uriInfo) { /* ... */ }
}

I tried making a ContextResolver<Dep> and making a field @Context Dep dep, and this gave an 
error that Dep is not an interface. So I created an IDep and ContextResolver<IDep> and it is 
set to a Proxy but that proxy always throws NPE in Method.invoke (not my code). I also tried @Context 
on a setter. It seems that I cannot add arbitrary things to be injected by JAX-RS, and I can't get 
access to the JAX-RS resources in Spring injection. So I have no solution to this problem. My current 
"workaround" is:

class A {
@Context public setUriInfo(UriInfo uriInfo) { this.dep = new Dep(uriInfo); }
}

However, this defeats the use of dependency injection and would get very hard 
to work with if/when Dep needs any other things.

I guess the same can be said about the original option, right ?

In JAX-RS 2.0, the only thing that can automate the injection of JAX-RS contexts into non-root resources like Dep is

https://jax-rs-spec.java.net/nonav/2.0/apidocs/javax/ws/rs/container/ResourceContext.html

but I don't think using ResourceContext  will fit in this case...

Other option I can think of, is extending CXF JAX-RS SpringResourceFactory somehow such that it can be used to initialize all Spring beans, not only the root resources, that way it would be able to do the constructor injection of JAX-RS contexts.

Have a look please at the source code, if you will have some idea on how to enhance the runtime code then it would be great

Perhaps a much simpler option is to make sure Dep has a reverse link to A which would also have a getter for UriInfo, so Dep would simply get a (thread-safe) UriInfo via A.getUriInfo whenever it needs it...

Cheers, Sergey


Jason Winnebeck

----------------------------------------------------------------------
This email message and any attachments are for the sole use of the intended 
recipient(s). Any unauthorized review, use, disclosure or distribution is 
prohibited. If you are not the intended recipient, please contact the sender by 
reply email and destroy all copies of the original message and any attachments.


Reply via email to