Announcement Announcement Module
No announcement yet.
coding to an interface and spring Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • coding to an interface and spring


    I had a heated discussion with my colleagues about coding to an interface.
    I have 2 questions :

    - coding to an interface is a good practice, is this statement correct ? why ?
    - coding to an interface is good especially with spring, why ?


  • #2
    Interfaces are (as they name describe) interface between two components, so if you have two components, it is much better to couple them using interfaces, in these interfaces you only define the contract between them and nothing more. So there is no no knowledge of the implementation. It can be mocked easily and ...

    Spring likes interfaces more, because Spring AOP is based on dynamic proxies, if you don't define proxies Spring shall use some liberary such as CGLIB to extend the class and weave the aspect, but in the case it can not weave the aspects on finale methods, or final classes.

    So using Spring AOP, if you want to weave aspects to final methods or classes, the only solution is using interfaces.