-2

Is it safe to use Spring DI + interface default method approach, instead of classic Spring DI + interface + class?

@Service
public interface MessagesService {

    default public void send() {

    }
}

OR

public interface MessagesService {

    void send();
}

@Service
public class MessagesServiceImpl implements MessagesService {

    @Override
    public void send() {

    }
}
Combo
  • 855
  • 2
  • 11
  • 22

1 Answers1

0

The fact is that you can't invoke the default method unless you provide an implementation for the MessagesService, so, it all depends upon where you wanted to place the send() method i.e., if send() algorithm is common to many implementations then you can keep the send() method as a default method inside the interface, otherwise it will be very specific to the MessagesServiceImpl class.

In simple words, it is nothing to do with Spring DI.

I suggest you read here and understand the concept of default methods.

Vasu
  • 21,832
  • 11
  • 51
  • 67
  • In application MessagesService interface will have only one implementation MessagesServiceImpl. – Combo Feb 09 '18 at 07:54
  • If you see that `send()` is reusable across various implementations for future then it can be `default` – Vasu Feb 09 '18 at 07:58