-5

Hello, I've been trying to use a service into another one but I get null. Does anyone know how can I inject it correctly? (My ClassServiceB has its @Service annotation)

This is my service class...

@Service
public class ClassServiceAImpl implements ClassServiceA {

   @Autowired
   ClassServiceB classServiceB;

   @Override
   public ClassOutDto searchDriverForOrder(InputDto inputDto ) {

   classServiceB.doSomething(); //In this part classServiceB is getting null

   ...
}
Turo
  • 4,724
  • 2
  • 14
  • 27
packo
  • 1
  • 2
  • 1
    You probably didn't activate or didn't properly configure the package scanning. – watery Mar 11 '21 at 19:05
  • Thank you guys, my mistake was that in class B I didn't have injected my DAO class... @Service public class ClassServiceBImpl implements ClassServiceB{ private ClassBDao classBDao;....} – packo Mar 12 '21 at 02:12

1 Answers1

0

Did you try to use @Autowired in the constructor class? It is easier when you want to do unit tests and the recommendation to do injection depended. Would be like this:

       @Service
       public class ClassServiceAImpl implements ClassServiceA {
    
       private ClassServiceB classServiceB;
    
       @Autowired
       public ClassServiceAImpl(ClassServiceB classServiceB) {
          this.classServiceB = classServiceB;
       }
    
       @Override
       public ClassOutDto searchDriverForOrder(InputDto inputDto ) {

       classServiceB.doSomething(); //In this part classServiceB is getting null
       ...
     }
   }

Don't forget that your ClassServiceB must also be annotated with @Service. Your interface should not be rated. If that doesn't help, share more of the properties, interface and ClassServiceB.

  • Thank you, well, my mistake was that in class B I didn't have injected my DAO class... @Service public class ClassServiceBImpl implements ClassServiceB{ private ClassBDao classBDao;....} – packo Mar 12 '21 at 02:08