繁体   English   中英

如何通过提供接口名称来获取实现的bean

[英]How to get bean of an implementation by giving the interface name

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

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

class MyImplementation implements MyInterface {

}

如果我们使用:

injector.getInstance(MyInterface.class);

MyImplementation的实例返回,因为它知道MyImplementation实现了MyInterface。

Spring中有没有类似的方法可以实现这一目标?

非常感谢。

在Spring中,您可以使用org.springframework.stereotype包中的注释之一(例如@Service)来注释实现。

如果然后使用类路径扫描来选择实现,则可以从Bean存储中获取接口的实例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM