Spring Autowire - Interface and implementation of DAO class required?

First, I've done some reading and I'm wondering what is the purpose of having an interface and an implementation class like Data Access Objects (DAO)? I read that this increases flexibility, but I was wondering if anyone could provide a concrete example of why we need an interface.

If we outsource, do we still need to use an interface and an implementation that implements the interface? If so, why? We just need an imp?

Thanks in advance.

+3


source to share


1 answer


You don't need an interface. But there are two reasons to prefer:

  • You can easily replace the implementation for testing purposes
  • Proxy-ing beans for aspect-oriented programming purposes or similar easier (Java supports it out of the box)


Other reasons can be added, but these are the main 2 in my opinion. However, don't feel compelled to work this way. If the business you are working in does not guarantee this flexibility, there is no need to complicate matters.

And, by all means, never call your * Impl classes . If you can't come up with the correct name for the implementation, that means you don't have to have an interface to start with.

+1


source







All Articles