Java 如何通过给出接口名来获取实现的bean

Java 如何通过给出接口名来获取实现的bean,java,spring,Java,Spring,在Google Guice中,我们可以定义如下接口和实现: @ImplementedBy(MyImplementation.class) interface MyInterface { .... } class MyImplementation implements MyInterface { } 如果我们使用: injector.getInstance(MyInterface.class); 返回MyImplementation的实例,因为它知道MyImplementation

在Google Guice中,我们可以定义如下接口和实现:

@ImplementedBy(MyImplementation.class)
interface MyInterface {
    ....
}

class MyImplementation implements MyInterface {

}
如果我们使用:

injector.getInstance(MyInterface.class);
返回MyImplementation的实例,因为它知道MyImplementation实现了MyInterface

在春天有没有类似的方法来实现这一点


非常感谢。

在Spring中,您可以使用org.springframework.stereotype包(例如@Service)中的一个注释对您的实现进行注释

如果然后使用classpath获取实现,则可以从bean存储中获取接口的实例