Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Spring with Hibernate Data-Driven Application Design Writing Your First Service

Vaibhav Yaramwar
PLUS
Vaibhav Yaramwar
Courses Plus Student 4,292 Points

What if two Implementation of Interfaces are there?

As Chris said in Service we are using Interface instated of Implementation and Spring will find Implementation of this Interface.

What if there are more than one Implementation of Interfaces ,in this case how spring will derive Implementation?

3 Answers

Daniel Vargas
Daniel Vargas
29,184 Points

I'm not sure if you should implement more than once a DAO interface, however if that's the case you could just instantiate the implementation instead of the interface.

Andrey Serebryanskiy
Andrey Serebryanskiy
5,756 Points

Here is the link from previous video's teacher's notes.

There is a precise answer to your question: if you have two or more implementations of an autowired interface you can use @Qulifier("RightImplClassName") annotation. For example:

public Example {
  @Autowired
  @Qualifier("SomeInterfaceImpl2")
  private SomeInterface someInteface;
}
Boban Talevski
Boban Talevski
24,793 Points

If you have two implementations, Spring will throw an exception, so you need to make sure you only have one. Check the teacher's notes in this previous video.

On a side note, you could have more than one implementation of an interface, but Spring will search for annotations on a class implementing that interface, so if only one class is annotated, you should still be ok and that's the class implementation which will be used for Autowiring.