如何在Grails(2.5.5)中自动在前面带有多个大写字母的服务进行接线 [英] How to autowire a service in grails (2.5.5) with multiple capital letters at front

查看:82
本文介绍了如何在Grails(2.5.5)中自动在前面带有多个大写字母的服务进行接线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为ABCDCode的域类,并为此ABCDCodeService创建了一个服务.现在,我想在控制器中使用此服务,因此我将其编写如下:

I have a domain class named ABCDCode and created a service for the this ABCDCodeService. Now I want to use this service in controllers so I wrote it like below:

class TestController{
      ABCDCode abcdCode

      def index(int id){
           abcdCode.getData(id) //Here I am getting NullPOinterException
      }
}

我怀疑名称自动布线有问题.

I am suspecting something wrong with the autowiring by name.

推荐答案

Grails为bean命名查找前两个字符.如果控制器/服务的第二个字符为大写,则Grails不会将第一个字符转换为小写.

Grails looks first two characters for beans naming. If the second character of the controller/service is capital then Grails did not convert the first character to lower case.

例如,TestService bean名称为testService,TEstService bean名称为TEstService.

e.g., TestService bean name is testService and TEstService bean name is TEstService.

因此,您的代码变为

ABCDCode ABCDCode

def index(int id){
    ABCDCode.getData(id)
}

但是,如果要使用 abcdCode 作为bean名称,则可以在 resources.groovy 的帮助下进行.将以下内容添加到您的 resources.groovy 文件中

But if you want to user abcdCode as bean name, then you can do this with the help of resources.groovy. Add the following to your resources.groovy file--

beans = {
    springConfig.addAlias 'abcdCode', 'ABCDCode'
}

这篇关于如何在Grails(2.5.5)中自动在前面带有多个大写字母的服务进行接线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆