Java 使用springdi和接口默认方法

Java 使用springdi和接口默认方法,java,spring,dependency-injection,spring-ioc,Java,Spring,Dependency Injection,Spring Ioc,使用SpringDI+接口默认方法而不是经典的SpringDI+接口+类安全吗 @Service public interface MessagesService { default public void send() { } } 或 事实上,除非为MessagesService提供实现,否则无法调用默认方法, 因此,这完全取决于您希望将send方法放置在何处,即,如果send算法在许多实现中是通用的,那么您可以将send方法保留为接口中的默认方法,否则它将非常特定于Me

使用SpringDI+接口默认方法而不是经典的SpringDI+接口+类安全吗

@Service
public interface MessagesService {

    default public void send() {

    }
}


事实上,除非为MessagesService提供实现,否则无法调用默认方法, 因此,这完全取决于您希望将send方法放置在何处,即,如果send算法在许多实现中是通用的,那么您可以将send方法保留为接口中的默认方法,否则它将非常特定于MessagesServiceImpl类

简单地说,这与springdi无关


我建议您阅读并理解默认方法的概念。

在应用程序消息中,Service interface将只有一个实现消息Service Impl。如果您看到send可在未来的各种实现中重用,那么它可以是默认的
public interface MessagesService {

    void send();
}

@Service
public class MessagesServiceImpl implements MessagesService {

    @Override
    public void send() {

    }
}