For example, have a class like as follows.
First XService service
in class A
is not null but second XService service
in AmountValidator
is null.I get NullPointerException
I try to create bean new
it works and then I get same exception when call AmountValidateService outsideRestService
in XService
.
How can I use XService
everywhere that I use @Autowired
annotation.
My main class:
@Service
class A extends AbstractA implements IA {
@Autowired
XService service; //first autowired definition. code go to check() method. service not null now.
public doSometing(){
validator.check();
service.methodA();
super.AbstractMethod();
}
}
Validator class used in class A :
class Validator<T> implements IValidator<T> {
public void check(){
rule.check(); // rule have a implements IValidator eg: amountValidator, dateValidator class
}
}
AmountValidator added to rule in class Validator.
@Component
class AmountValidator implements IValidator<T>{
@Autowired
XService service; // code comes here service is null. same service class mentioned above class A.
@Override
public void check(){
service.validateAmount(); // nullPointerException.
}
}
My main Service
@Component
class XService {
@Autowired
AmountValidateService outsideRestService;
public validateAmount(){
outsideRestService.validate(); // nullPointer when create XService with the `New` keyword
}
}