Is it possible to use dagger2 with jersey the same

2019-05-22 16:27发布

问题:

Is it possible to use dagger2 with jersey the same way it is possible with guice?

Is there any dagger2 servlet or dagger2 jersey project?

回答1:

So, short answer is no, there is not a great way to do this while getting all of the features of Jersey in your application.

However, there are ways to partially do this. You can simply register your resource in an application servlet ( see an example of that here https://github.com/cogman/gluews/blob/master/src/main/java/com/cogman/glue/GlueApplication.java )

What you lose with this approach are the Jersey injected magic things at the field level, such as @Context. You can still get at these things by adding them to the method signatures that need them, you just can't easily do this at a field level thing.

The other downside to this approach is that all resources become effectively singletons. Not a big deal if your resources are simple objects that simply service requests, a big deal if your resources are storing any sort of state on them.