【发布时间】:2015-12-09 02:24:43
【问题描述】:
更新:重复。见How to separate interface from implementation in Grails services?
我想通过它与 Spring 的接口来配置一个 bean - 正如我期望在依赖注入框架中那样。
这是一个例子:
public interface MyInterface {
public void callMe();
}
public class MyImpl implements MyInterface {
public void callMe() {
println("Calling MyImpl");
}
}
我应该如何配置这个 bean,以便我可以切换实现,例如,取决于我是否在开发/生产环境中?在 Grails 中,我希望用以下内容填充我的 grails-app/config/spring/resources.groovy:
beans = {
myBean(MyInterface) {
implemented by MyImpl //sintax not supported
}
}
所以我可以在我的代码中定义和使用这个 bean:
def myBean
(...)
myBean.callMe()
我知道有一些 Spring 属性,如 factory-method 和 factory-bean 来实现这一点,但我想知道是否有一个简单直接的接口,比如在 Spring/Grails 中定义 bean 的方式,无需样板代码来实例化它们.
【问题讨论】:
-
我能找到的最佳解决方案:mrhaki.blogspot.com.br/2013/03/…