-2

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
  }
}
erhan
  • 1
  • 2
  • 2
    Could you share the class definition for AmountValidateService? Also please tidy up the code and question, its a little hard to grasp at first – J11 May 30 '20 at 17:06
  • `AmountValidateService` classic service class that have `@service` annotation. Sorry for complex question. I clean the question for more understanble. – erhan May 30 '20 at 17:26
  • Do you create instance of AmountValidator yourself with new keyword or it is creating BY context? @Autowired works only for context created instance cause injection is produced BY spring context. – Alex May 30 '20 at 17:49
  • I see you've edited the code, and still notice this `class AmountValidator() ...` are the parenthesis intended? – J11 May 30 '20 at 18:00
  • I know there are some parts that make the code a bit complex, but I used a strategy patter, I didn't know how to explain it. The AmountValidator class has been added to the rules in the Starategy class. see: Validataor class rule. And I never created intance for AmountValidator. – erhan May 30 '20 at 18:06

2 Answers2

0

You have an error cause you are trying to create components/beans/services yourself. As i mentioned in comment when you create components yourself it - @Autowired doesn't work - thats you've got NPE

Alex
  • 706
  • 7
  • 16
  • what does 'you create components yourself' mean? Components doesn't create by Spring anyway. I create a component and I try to call service. @Alex – erhan May 30 '20 at 20:01
0

All classes annotated with @Component, @Service are considered special classes which are instantiated by Spring automatically via DI, instantiating them with new defeats the purpose of DI.

These special classes are named Spring Beans.

Every time the application starts, the framework instances all Spring Beans, and all @Autowired fields are injected by Spring automatically. But the Spring Beans must be defined somewhere in the class path. Else you will receive a NoSuchBeanDefinitionException

As an attempt to answer the question, since I don't have a stack trace nor all the Spring Bean definitions:

When you instantiate XService using new XService() your new instance will not actually initialize the field AmountValidateService outsideRestService, effectively leaving it as null.

You may set the field yourself but as I mentioned earlier, it defeats the purpose of DI

Your question is not complex, it is incomplete.

J11
  • 426
  • 1
  • 6
  • 17