You don't need it. Jersey uses HK2 as it's internal DI framework, and HK2 has a Spring bridge. This is what's used internally to bridge Spring components into the HK2 IoC container, so that they can be injected into Jersey components. And Jersey implements an AutowiredInjectionResolver
1 that allows for injection of Spring components using @Autowired
. You don't even need @Autowired
though. All the Spring components can be injected with the normal @Inject
.
The only drawback I've ran into, not making the Jersey components a Spring @Component
is that it doesn't support @Value
when you want to inject property values.
The one thing I don't like is that when you declare something a Spring @Component
, it automatically makes it a singleton. But Jersey resources are by default request scoped. You can add a Spring @Scope("request")
, and it should change the resource to a request scoped resource. Jersey has declared the Spring RequestScope, so we can use it. How exactly it ties in to Jersey's request scope, I am not a hundred percent sure. I ran into a problem a while back. I can't remember what it was, but that has kept me from ever using the Spring request scope again.
Assuming I want to keep all my resources request scoped, I would take sticking to the normal Jersey request scope, and not being able to inject @Value
s, over having to use Spring's request scope. Maybe I'm imagining things, and there was no issue using it, but personally I'll just stick to what I know works :-)
Read full article from Why do we need @Component spring annotation for Jersey resource in spring-boot-starter-jersey project? - Stack Overflow
No comments:
Post a Comment