Grails generate-all 生成服务 class ( 3.2.3 )

Grails generate-all generates a Service class ( 3.2.3 )

从新的 Grails 版本 3.2.3 开始,命令

generate-all <Domain Class>

生成一个名为 <Domain Class>Serviceservice grails.gorm.services.Service,它是一个接口

我可以编辑的实际实现是什么?

要编辑生成模板,您应该使用此脚本: https://docs.grails.org/latest/ref/Command%20Line/install-templates.html 在您可以编辑模板后,它们将用于生成您 services/controllers 等

来自

https://github.com/grails/grails-core/issues/11062

该服务是一个数据服务,在这里解释:

http://gorm.grails.org/latest/hibernate/manual/index.html#dataServices

@Service 注释是一个 AST 转换,它将自动为您实现服务。然后,您可以通过 Spring 自动装配获得服务。

@Service 转换将查看接口的方法签名并尽最大努力找到实现每个方法的方法。

此外,域 class 的所有 public 方法将自动包装在适当的事务处理中。

这意味着您可以定义非事务性的受保护抽象方法来组合逻辑。例如:

@Service(Book)
abstract class BookService  {

   protected abstract Book getBook(Serializable id) 

   protected abstract Author getAuthor(Serializable id) 

   Book updateBook(Serializable id, Serializable authorId) { 
      Book book = getBook(id)
      if(book != null) {
          Author author = getAuthor(authorId)
          if(author == null) {
              throw new IllegalArgumentException("Author does not exist")
          }
          book.author = author
          book.save()
      }
      return book
   }
}