Inject ApplicationScoped inside ApplicationScoped throws NullPointerException
See original GitHub issueDescribe the bug
Basically: I want the Controller class to have a generic IUseCase (constructor injected) and the UseCase to have a generic IService (constructor injection).
Controller -> UseCase -> Server Controller -> UseCase -> MockServer Controller -> UseCaseMock
public class Controller {
private final IUseCase useCase;
@Inject
public Controller(IUseCase getUseCase) {
this.useCase = getUseCase;
}
@ApplicationScoped
public class UseCase implements IUseCase {
private IService service;
@Inject
public UseCase(IService webService) {
this.service = webService;
service.init();
}
}
public class UseCaseMock implements IUseCase {
...
}
@ApplicationScoped
public class Service implements IService {
...
}
public class ServiceMock implements IService {
...
}
Expected behavior
When running the application, because of the @ApplicationScoped annotation, I expect that: Controller has the UseCase constructor injected and that the UseCase has Service constructor injected.
Actual behavior
Method threw ‘java.lang.NullPointerException’ exception. Cannot evaluate …usecases.impl.UseCase_ClientProxy.toString()
it didnt inject it, even tho it was annotated with ApplicationScoped.
How to Reproduce?
In a nutshell: have a class which constructor injects an ApplicationScoped class which constructor injects an ApplicationScoped class.
public class Controller {
private final IUseCase useCase;
@Inject
public Controller(IUseCase getUseCase) {
this.useCase = getUseCase;
}
@ApplicationScoped
public class UseCase implements IUseCase {
private IService service;
@Inject
public UseCase(IService webService) {
this.service = webService;
service.init();
}
}
public class UseCaseMock implements IUseCase {
...
}
@ApplicationScoped
public class Service implements IService {
...
}
public class ServiceMock implements IService {
...
}
Output of uname -a
or ver
No response
Output of java -version
openjdk version “1.8.0_312”
GraalVM version (if different from Java)
No response
Quarkus version or git rev
2.4.2.Final
Build tool (ie. output of mvnw --version
or gradlew --version
)
Apache Maven 3.8.5
Additional information
No response
Issue Analytics
- State:
- Created a year ago
- Comments:13 (7 by maintainers)
I see that
Controller
is not annotated with a CDI annotation. How are you creating / using it?I’m going to close this issue. @vinland-saga Feel free to reopen if you find a way to reproduce the NPE.