在Spring Boot中,是否可以为Interface添加@Service注解?如果该接口未被任何 class 实现
In Spring Boot, Can we add @Service Annotation for Interface? if that interface is not implemented by any class
我创建了一个没有任何实现的接口
@Service
public interface MyInterface{
default void doSomething{
System.out.print("print something");
}
}
MyInterface 可以用@Autowired 注解吗?
@Autowired
MyInterface myInterFace;
运行
时显示以下错误
***************************
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)
不,你不能,因为 Spring 试图实例化它来创建一个 Bean (@Service),但它是一个接口,所以那是不可能的。
我创建了一个没有任何实现的接口
@Service
public interface MyInterface{
default void doSomething{
System.out.print("print something");
}
}
MyInterface 可以用@Autowired 注解吗?
@Autowired
MyInterface myInterFace;
运行
时显示以下错误***************************
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)
不,你不能,因为 Spring 试图实例化它来创建一个 Bean (@Service),但它是一个接口,所以那是不可能的。