'In Spring Boot, Can we add @Service Annotation for Interface? if that interface is not implemented by any class
I created a interface which has not any implementations
@Service
public interface MyInterface{
default void doSomething{
System.out.print("print something");
}
}
Can MyInterface be annotated by @Autowired?
@Autowired
MyInterface myInterFace;
following error displayed when it run
***************************
APPLICATION FAILED TO START
***************************
Description:
Field myInterFace in com.example.demo.controller.corejava.CoreJavaController required a bean of type 'com.example.demo.controller.corejava.MyInterface' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Solution 1:[1]
No you can't, because Spring tries to instantiate it to create a Bean (@Service), but it's an Interface, so thats not possible.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Oli |
