Reputation: 4538
I have a simple Quarkus resource:
@Path("/rosters")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class RosterResource {
private final RosterService rosterService;
public RosterResource(RosterService rosterService){
this.rosterService = rosterService;
}
@GET
@Path("/{rosterId}")
public Response getRoster(@PathParam("rosterId")Long rosterId){
return Response.ok(rosterService.getRosterById(rosterId)).build();
}
}
I am trying to inject the RosterService
instance in my resource, but I am getting a javax.enterprise.inject.UnsatisfiedResolutionException
. However, if I use the @ApplicationScoped
annotation on RosterService
, then everything works just fine. Is there a way of injecting the RosterService
class in my resource without using annotations? In other words, is there a way of making RosterService
discoverable by the Quarkus container without directly annotating the class?
Edit: looking into the CDI docs, it seems that you can manually register beans using a method with a @BuildStep
annotation. However, it is not clear to me which class should contain the annotated method)
Another option would be to use a Jandex index
Upvotes: 1
Views: 2882
Reputation: 6577
To the best of my knowledge, Quarkus only implements so called annotated bean discovery. That means that all CDI beans in Quarkus have to have a bean defining annotation. @ApplicationScoped
is one of them.
EDIT: regarding a Jandex index, that allows you to scan for beans in additional JARs. In other words, it will only expand the set of classes that are scanned for a bean defining annotation.
When it comes to a @BuildStep
method -- that is only possible in a Quarkus extension. Extensions are powerful (and indeed they can define additional beans) but also complex. You can start at https://quarkus.io/guides/building-my-first-extension, but it may feel overwhelming. It may also feel like this is not the right thing to do if you want to just make your class a bean -- and that would be true. But if your class comes from an external library that you can't change, extension makes sense.
Upvotes: 2
Reputation: 51
Is there a specific reason why you don't want to annotate your service class with @ApplicationScoped
(or any other of the bean discover/scope annotations)?
The only other way that I'm aware of (instead of annotations) is - as you yourself mentioned - the use of Jandex index.
Upvotes: 0