【问题标题】:Is there anyway to change the default Grails service naming convention?无论如何要更改默认的 Grails 服务命名约定?
【发布时间】:2015-07-24 22:22:56
【问题描述】:

我想在不使用 *Service 后缀约定的类名称的情况下创建 Grails 服务(例如,将我的类命名为 Scheduler 而不是 ScheduleService)。无论如何,是否有覆盖 Grails 的默认行为并将 grails-app/service 目录中的任何对象视为 Grails 服务?

【问题讨论】:

    标签: grails


    【解决方案1】:

    我不这么认为,但如果原因是你想为 bean 使用不同的名称,你可以尝试为 scheduleService 注册一个别名。 根据on this page 的评论,您应该可以将其添加到 resources.groovy:

    springConfig.addAlias('scheduler','scheduleService')
    

    不过,我自己没有尝试过,也没有在文档中找到任何内容。

    【讨论】:

      【解决方案2】:

      当然可以,只需在 resources.groovy 中进行

      scheduler(YourCustomSchedulerClassname){ b->
          // config
      }
      

      然后你可以在其他任何地方做

      class MyService {
          def scheduler
      }
      

      【讨论】:

        【解决方案3】:

        不要认为这是可能的。因为它是 grails 框架中隐含的“约定”的一部分。

        【讨论】:

          【解决方案4】:

          Grails 服务注册为带有插件的 bean,信不信由你,因此您应该能够通过创建自己的插件来完成此操作。

          看看grails-plugin-services source。您可以看到监视的服务与此行硬连线:

          def watchedResources = ["file:./grails-app/services/**/*Service.groovy",
                                  "file:./plugins/*/grails-app/services/**/*Service.groovy"]
          

          现在,尽管有可能,但可能需要做一些工作,而且这可能是个坏主意。我真的不知道。我不知道如果你基本上复制了服务插件,更改了watchedResources 并将其封装在你自己的插件中,Grails 是否会发疯。

          我刚刚浏览了 grails 文档,Artefacts 上的部分也应该很有用。

          如果您只是在寻找一种更简单的方法来引用您的服务,请执行以下操作:

          def painfullyLongNameService
          def getNoPain() {return painfullyLongNameService}
          
          noPain.yourServiceFunction()
          

          【讨论】:

            猜你喜欢
            • 2023-04-05
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多