Home > other >  In Spring Boot, Can we add @Service Annotation for Interface? if that interface is not implemented b
In Spring Boot, Can we add @Service Annotation for Interface? if that interface is not implemented b

Time:03-20

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;

CodePudding user response:

No you can't, because Spring tries to instantiate it to create a Bean (@Service), but it's an Interface, so thats not possible.

  • Related