Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.