angularjs - 如何将服务注入(inject) Grails 3 命令?

标签 angularjs grails grails-plugin grails-services grails-3.3

在 Grails 3 应用程序中,如何创建利用应用程序服务和域类的 CLI 命令?

The following did not work:


  • grails create-app test-grails3-angular-cmd --profile=angular
  • cd server
  • grails create-command MyExample
  • 实现 MyExample:
    package test.grails3.angular.cmd
    
    import grails.dev.commands.*
    
    class MyExampleCommand implements GrailsApplicationCommand {
        def testService
    
        boolean handle() {
            testService.test()
            return true
        }
    }
    
  • grails create-service TestService
  • 实现测试服务:
    package test.grails3.angular.cmd
    
    import grails.transaction.Transactional
    
    @Transactional
    class TestService {
    
        def test() {
            System.out.println("Hello, test service!")
        }
    }
    
  • grails run-command my-example

  • Command execution error: Cannot invoke method test() on null object



    我怎样才能解决这个问题?

    我正在使用 grails 3.3.0.M2。

    最佳答案

    MyExampleCommand我相信它不是一个可以注入(inject)服务的bean。但是,applicationContext可用 GrailsApplicationCommand (扩展 ApplicationCommand 特征)可以直接用于获取服务 bean。

    class MyExampleCommand implements GrailsApplicationCommand {
    
        boolean handle() {
            TestService testService = applicationContext.getBean(TestService)
            testService.test()
            return true
        }
    }
    

    关于angularjs - 如何将服务注入(inject) Grails 3 命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45335153/

    相关文章:

    javascript - angularjs 动态改变模型绑定(bind)

    hibernate - 如何在grails中检索具有唯一空键的行?

    unit-testing - 由于 View 为空,对 Controller 方法进行单元测试失败

    gwt - 加载 grails gwt 模块 xml 时出错

    grails - 在Grails中,是否可以将数据库迁移插件配置为在其他插件之前运行?

    html - 使用屏幕大小缩放字体大小?

    javascript - 如何在 html/javascript 中加速本地文件系统中大图像的显示

    Angularjs - 在 Protractor e2e 测试中模拟触摸事件

    grails - Grails,域的域类默认值

    Grails 发送邮件不起作用