Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to autowire Spring services that inherit?

I have 2 services, EFT and Cheque that are substantially similar.

Runs fine if I mark the implementation as @service.

Otherwise I get a no such bean definition exception. No qualifying bean of type 'EftPaymentService'.

Top level interface.

public interface PaymentService {
  public void paymentsResponse();
}

Eft service interface.

@Service
public interface EftPaymentService extends 
  PaymentService {
   public void processEft(String code) throws PaymentsException;
}

Cheque service interface

@Service
public interface ChequePaymentService extends 
  PaymentService {
   public void processCheque(String code) throws PaymentsException;
}

Top level implementation

public abstract class PaymentServiceImpl implements PaymentService {
  @Autowired
  protected SessionFactory sftpSessionFactory;

  @Autowired
  protected SftpConfig.UploadGateway gateway;

  public void paymentsResponse(){
  } 
}

Eft implementation

public class EftServiceImpl extends PaymentsServiceImpl implements EftPaymentService {
}

Cheque implementation

public class ChequeServiceImpl extends PaymentsServiceImpl implements ChequePaymentService {
}

What is going on here? Refactor using composition?

like image 294
Interlated Avatar asked Sep 01 '25 02:09

Interlated


1 Answers

Annotate implementations with @Service & use constructor-based injection.

like image 52
Jamie Bisotti Avatar answered Sep 02 '25 17:09

Jamie Bisotti