Grails 数据服务无法使用常规服务

Grails Data Service Cannot Use Regular Service

又是 Covid 日快乐。当我使用 generate-all 时,Grails 会为我创建数据服务。我开始明白什么是数据服务了。

我也有自己的服务供我的作者和书籍 class 使用。我将我的服务命名为 ImportService。在数据服务将我的书籍保存到数据库之前,我在 ImportService 中有一些方法可以清理从 CSV 文件读取的书籍数据。我还按照说明将数据服务设为摘要 Class。所以,我可以把我自己的方法放在数据服务中。

由于Author有自己的AuthorService,而Book有自己的BookService,我希望不同的Data Service访问我的ImportService中的方法。因此,我不必多次复制和粘贴导入 CSV 代码。因此,我将行 ImportService importService 放在 AuthorServie class 和 BookService class 中。那并不顺利。 importService 在数据服务 classes 中始终为 NULL。我google的问题。他们说我不能向 grails.gorm.services.Service.

注入其他服务

有个post说做豆子。我是 Grails 的新手。即使使用 posted 代码,我也不知道他们在说什么。我的部分背景是汇编语言、C 和 Pascal。我的脑袋里塞满了自上而下、子例程、库、地址和指针等行话。我不知道 Bean 是什么。

就是这样。我想知道这是一个错误还是设计使您无法向 gorm 服务注入服务。

感谢您的“指点”。

查看 https://github.com/jeffbrown/tom6502servicedi 中的项目。该项目使用 Grails 4.0.3 和 GORM 7.0.7。

https://github.com/jeffbrown/tom6502servicedi/blob/main/grails-app/services/tom6502servicedi/ImportService.groovy

package tom6502servicedi

class ImportService {

    int getSomeNumber() {
        42
    }
}

https://github.com/jeffbrown/tom6502servicedi/blob/917c51ee173e7bb6844ca7d40ced5afbb8d9063f/grails-app/services/tom6502servicedi/AuthorService.groovy

package tom6502servicedi

import grails.gorm.services.Service
import org.springframework.beans.factory.annotation.Autowired

@Service(Author)
abstract class AuthorService {

    @Autowired
    ImportService importService

    // ...

    int getSomeNumberFromImportService() {
        importService.someNumber
    }
}

https://github.com/jeffbrown/tom6502servicedi/blob/917c51ee173e7bb6844ca7d40ced5afbb8d9063f/grails-app/controllers/tom6502servicedi/AuthorController.groovy

package tom6502servicedi

import grails.validation.ValidationException
import static org.springframework.http.HttpStatus.*

class AuthorController {

    AuthorService authorService

    // ...

    def someNumber() {
        render "The Number Is ${authorService.someNumberFromImportService}"
    }
}

向该 someNumber 操作发送请求将验证 ImportService 已注入 AuthorService 并且 AuthorService 已注入 AuthorController .

$ curl http://localhost:8080/author/someNumber
The Number Is 42