如何在Grails服务中将接口与实现分开? [英] How to separate interface from implementation in Grails services?
问题描述
我想知道是否可以在Grails上创建一个服务接口,并且找不到正确的方法。
这个解释并不令人满意,因为它似乎混合了Java和Groovy:
http://www.grails.org/doc/latest/guide/8.%20The%20Service%20Layer.html 在我看来,框架的设计缺陷很糟糕,因为接口机制是Java(以及大多数OO语言)的最佳功能之一, 。 有什么想法澄清这个问题吗? 谢谢! 您可以拥有一个界面,但实际上并不需要界面。如果我正确理解了你的话,你希望有两个服务实现并能够选择使用哪一个。 只需实现两个名为 甚至: 这就是你如何告诉Spring实际为 和Spring将自动布线一个适当的实施。这允许您根据某些配置配置使用哪个服务实现。 I was wondering if it's possible to create a service interface on Grails and I can't find a proper way of doing it.
This explanation isn't satisfactory, since it seems to mix Java and Groovy: http://www.grails.org/doc/latest/guide/8.%20The%20Service%20Layer.html It seems to me like a bad design flaw of the framework, given that the interface mechanism is one of the best features of Java (and most OO languages). Any idea to clarify this issue? Thanks!
Mulone You can have an interface, but actually you don't need one. If I understand you correctly you would like to have two implementations of a service and be able to choose which one to use. Simply implement two services named for example or even: This is how you tell Spring which service to actually inject for and Spring will auto wire a proper implementation. This allows you to configure which service implementation to use based for example on some configuration. 这篇关于如何在Grails服务中将接口与实现分开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
Mulone
MyService1
和 MyService2
,然后在 grails-app / conf / spring / resource.groovy
您可以指定:
beans = {
...
//语法是beanId (implementsClassName){properties}
myService(MyService1)
...
}
beans = {
...
if(someConfigurationOption) {
myService(MyService1)
} else {
myService(MyService2)
}
}
myService
注入的服务。现在您可以使用 myService
,例如:
public MyController {
def myService
...
}
MyService1
and MyService2
, then in grails-app/conf/spring/resource.groovy
you can specify:beans = {
...
// syntax is beanId(implementingClassName) { properties }
myService(MyService1)
...
}
beans = {
...
if (someConfigurationOption) {
myService(MyService1)
} else {
myService(MyService2)
}
}
myService
. Now you will be able to use myService
like:public MyController {
def myService
...
}