在grails(2.5.6)中,如果服务前面有多个大写字母,如何在类中自动调用服务



所以我有一个类,我需要在grails中注入一个服务。

class APIPaymentCreateCommand implements CreateCommand {
def cardTokensService 
}

我尝试了下面的模式,但它们似乎都没有注入服务。

resources.groovy:

// 1
aPIPaymentCreateCommand(APIPaymentCreateCommand) { bean ->
bean.autowire = 'byName'
}
// 2
apipaymentCreateCommand(APIPaymentCreateCommand) { bean ->
bean.autowire = 'byName'
}
// 3
APIPaymentCreateCommand(APIPaymentCreateCommand) { bean ->
bean.autowire = 'byName'
}

我已经找到了下面的链接,所以我假设问题是围绕类命名不符合grails。重命名类是最后的手段,因为它有很多依赖关系,所以尽量避免这种方法。

任何帮助或方法我应该尝试将非常感激!

如何在grails(2.5.5)中自动使用多个大写字母的服务

见项目网址:https://github.com/jeffbrown/kennanwhoserviceinjection

https://github.com/jeffbrown/kennanwhoserviceinjection/blob/188f64d7fff55a24c466c17c26763e3f489d0774/grails-app/services/kennanwhoserviceinjection/CardTokensService.groovy

package kennanwhoserviceinjection
class CardTokensService {
int getTheAnswer() {
42
}
}

https://github.com/jeffbrown/kennanwhoserviceinjection/blob/188f64d7fff55a24c466c17c26763e3f489d0774/src/groovy/kennanwhoserviceinjection/APIPaymentCreateCommand.groovy

package kennanwhoserviceinjection
class APIPaymentCreateCommand {
def cardTokensService
int getTheAnswer() {
cardTokensService.theAnswer
}
}

https://github.com/jeffbrown/kennanwhoserviceinjection/blob/188f64d7fff55a24c466c17c26763e3f489d0774/grails-app/controllers/kennanwhoserviceinjection/DemoController.groovy

package kennanwhoserviceinjection
class DemoController {
APIPaymentCreateCommand aPIPaymentCreateCommand
def index() {
int answer = aPIPaymentCreateCommand.theAnswer
render "The answer is $answer"
}
}

https://github.com/jeffbrown/kennanwhoserviceinjection/blob/398a46efe373b5f2bafbdbbb9c25f017f9be244e/grails-app/conf/spring/resources.groovy

import kennanwhoserviceinjection.APIPaymentCreateCommand
beans = {
aPIPaymentCreateCommand(APIPaymentCreateCommand) { bean ->
bean.autowire = 'byName'
}
}

都可以。我使用了resources.groovy中的第一个示例bean定义,但其他的也可以工作(假设bean名称在DemoController.groovy中保持同步)。

最新更新