I'm trying to resolve dependency injection with Repository Pattern using Quarkus 1.6.1.Final and OpenJDK 11.
I want to achieve Inject with Interface and give them some argument(like @Named
or @Qualifier
) for specify the concrete class, but currently I've got UnsatisfiedResolutionException
and not sure how to fix it.
Here is the my portion of code.
UseCase class:
@ApplicationScoped
public class ProductStockCheckUseCase {
@Inject
@Named("dummy")
ProductStockRepository repo;
public int checkProductStock() {
ProductStock stock = repo.findBy("");
return stock.getCount();
}
}
Repository Interface:
public interface ProductStockRepository {
public ProductStock findBy(String productId);
}
Repository Implementation:
@Named("dummy")
public class ProductStockDummyRepository implements ProductStockRepository {
public ProductStock findBy(final String productId) {
final ProductStock productStock = new ProductStock();
return productStock;
}
}
And here is a portion of my build.gradle's dependencies:
dependencies {
implementation 'io.quarkus:quarkus-resteasy'
implementation 'io.quarkus:quarkus-arc'
implementation enforcedPlatform("${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}")
testImplementation 'io.quarkus:quarkus-junit5'
testImplementation 'io.rest-assured:rest-assured'
}
When I run this (e.g. ./gradlew assemble
or ./gradlew quarkusDev
), I've got the following errors:
Caused by: javax.enterprise.inject.UnsatisfiedResolutionException: Unsatisfied dependency for type ProductStockRepository and qualifiers [@Named(value = "dummy")]
- java member: ProductStockCheckUseCase#repo
- declared on CLASS bean [types=[ProductStockCheckUseCase, java.lang.Object], qualifiers=[@Default, @Any], target=ProductStockCheckUseCase]
Do you have any ideas how to fix this? or is it wrong idea to implement this kind of interface injection and specify the concrete class with argument/annotation?
I've read and tried the following articles:
Some official docs:
The other blogs and SOs:
My guess is that you need to add a scope annotation to your ProductStockDummyRepository
. Probably either @Singleton
or @ApplicationScoped
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With