4

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?

Interlated
  • 5,108
  • 6
  • 48
  • 79
  • Did you check in the logs to see if the implementation class was scanned? – zatopek Nov 28 '17 at 02:32
  • Marking the implementation as `@Service` is what you should do, what exactly is the problem? – Oleg Nov 28 '17 at 02:39
  • Is it? https://stackoverflow.com/questions/16351780/where-should-service-annotation-be-kept-interface-or-implementation – Interlated Nov 28 '17 at 02:42
  • Yes, it is. You found the question now all that's left is read the answers and understand them. – Oleg Nov 28 '17 at 02:53

1 Answers1

3

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

Jamie Bisotti
  • 2,605
  • 19
  • 23
  • This works. Have been annotating the services on the basis that referencing the interface is more generic. It doesn't work in this case, although I still don't quite know why. – Interlated Nov 28 '17 at 03:04
  • `@Service` is just a specialization of `@Component`; they behave exactly the same. By its name, the former communicates some additional intent. I believe the intent of `@Component` (thus `@Service`) it to annotate Spring "bean" implementations, which are typically concrete classes, not interfaces. I guess I'm saying I wouldn't expect putting it on an interface to ever work. Good luck. – Jamie Bisotti Nov 28 '17 at 23:35