在 Grails 3.3.3 中,当我运行 generate-all
对于域类,会生成一个服务接口(interface)(相对于 Grails 2.x 中的实际服务类)。实际上,直到我尝试向我的服务添加一个方法时,我才注意到它。
该接口(interface)被放置在服务所在的服务文件夹中。我实际上确实喜欢这个界面,但我仍然想要服务和默认实现。如果接口(interface)已经具有服务的名称,如何在服务文件夹中同时拥有接口(interface)和实现? (例如,接口(interface)被命名为 ClientService.groovy
,因此实现将具有相同的名称)
这是生成的接口(interface)示例
package project
import grails.gorm.services.Service
@Service(Client)
interface ClientService {
Client get(Serializable id)
List<Client> list(Map args)
Long count()
void delete(Serializable id)
Client save(Client client)
}
最佳答案
How can I have both an interface and implementation live in the services folder if the interface already has the name of the service?
如果您有一个标有
@Service
的接口(interface)那么你根本不应该有一个实现源文件。 GORM 数据服务在编译时为您生成实现。如果您想自己编写一些代码,那么不要编写接口(interface),而是编写一个抽象类并使用 @Service
对其进行注释。 .您没有理由将接口(interface)标记为 @Service
然后编写一个实现该接口(interface)的类。
关于Grails 3.3.3 generate-all <domain class> 只创建服务接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49326761/