服务中的 Grails 消息源错误
Grails Message source bug in Service
我在 Grails 3.3.11 中遇到了一点问题,我需要帮助。
我正在 grails 3.3.11 中开发 rest-api,但我无法在服务中使用 i18n 的消息源。它只适用于我的控制器。
message = messageSource.getMessage('documentProperties.notFound', [partId,jsonRequestDoc.get("typeId")] as Object[], LocaleContextHolder.locale)
当我执行上述操作时,我收到了这个服务。
Cannot invoke method getMessage() on null object
我的 messageSource 在我的服务 class 开头定义如下:
@Autowired
def messageSource
为了解决这个问题,我手动创建了一个服务构造函数,它从控制器接收消息源。
class CmbIdService {
public CmbIdService(def messageSource){
this.messageSource = messageSource
}
}
It was working as expected, but when I started running my integration tests, I noticed that Grails did not know how to instantiate my service considering the presence of the constructor. This the message I receive when I run the Grails Integration tests.
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cmbIdService': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'java.lang.Object' available: more than one 'primary' bean found among candidates... continue.
谁能帮帮我。我不知道该怎么办。我需要使用消息源进行国际化。我需要在服务中这样做,而不仅仅是在控制器中。我的测试需要继续工作。
谢谢!
阿尔弗雷多
而不是这个...
class CmbIdService {
public CmbIdService(def messageSource){
this.messageSource = messageSource
}
}
这样做...
class CmbIdService {
def messageSource
}
如果 class 在 grails-app/services/[whatever your package is]/CmbIdService.groovy
中定义,那么将自动为您创建一个实例,作为 bean 添加到应用程序上下文中,并且该实例将受到 autowire-by -名字。
编辑
下面的评论表明 OP 仍在获得 NPE。我在 https://github.com/jeffbrown/dnunesmessagesource.
提供了一个工作示例
package dnunesmessagesource
import org.springframework.context.i18n.LocaleContextHolder
class CmbIdService {
def messageSource
String getCustomMessage() {
messageSource.getMessage('my.custom.message', [] as Object[], LocaleContextHolder.locale)
}
}
package dnunesmessagesource
class DemoController {
CmbIdService cmbIdService
def index() {
String message = cmbIdService.customMessage
[customMessage: message]
}
}
my.custom.message=This Is My Custom Message
model {
String customMessage
}
json {
message customMessage
}
一切似乎都按设计工作。
$ http :8080/demo
HTTP/1.1 200
Content-Language: en-US
Content-Type: application/json;charset=UTF-8
Date: Wed, 31 Mar 2021 18:31:37 GMT
Transfer-Encoding: chunked
X-Application-Context: application:development
{
"message": "This Is My Custom Message"
}
我在 Grails 3.3.11 中遇到了一点问题,我需要帮助。
我正在 grails 3.3.11 中开发 rest-api,但我无法在服务中使用 i18n 的消息源。它只适用于我的控制器。
message = messageSource.getMessage('documentProperties.notFound', [partId,jsonRequestDoc.get("typeId")] as Object[], LocaleContextHolder.locale)
当我执行上述操作时,我收到了这个服务。
Cannot invoke method getMessage() on null object
我的 messageSource 在我的服务 class 开头定义如下:
@Autowired
def messageSource
为了解决这个问题,我手动创建了一个服务构造函数,它从控制器接收消息源。
class CmbIdService {
public CmbIdService(def messageSource){
this.messageSource = messageSource
}
}
It was working as expected, but when I started running my integration tests, I noticed that Grails did not know how to instantiate my service considering the presence of the constructor. This the message I receive when I run the Grails Integration tests.
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cmbIdService': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'java.lang.Object' available: more than one 'primary' bean found among candidates... continue.
谁能帮帮我。我不知道该怎么办。我需要使用消息源进行国际化。我需要在服务中这样做,而不仅仅是在控制器中。我的测试需要继续工作。
谢谢!
阿尔弗雷多
而不是这个...
class CmbIdService {
public CmbIdService(def messageSource){
this.messageSource = messageSource
}
}
这样做...
class CmbIdService {
def messageSource
}
如果 class 在 grails-app/services/[whatever your package is]/CmbIdService.groovy
中定义,那么将自动为您创建一个实例,作为 bean 添加到应用程序上下文中,并且该实例将受到 autowire-by -名字。
编辑
下面的评论表明 OP 仍在获得 NPE。我在 https://github.com/jeffbrown/dnunesmessagesource.
提供了一个工作示例package dnunesmessagesource
import org.springframework.context.i18n.LocaleContextHolder
class CmbIdService {
def messageSource
String getCustomMessage() {
messageSource.getMessage('my.custom.message', [] as Object[], LocaleContextHolder.locale)
}
}
package dnunesmessagesource
class DemoController {
CmbIdService cmbIdService
def index() {
String message = cmbIdService.customMessage
[customMessage: message]
}
}
my.custom.message=This Is My Custom Message
model {
String customMessage
}
json {
message customMessage
}
一切似乎都按设计工作。
$ http :8080/demo
HTTP/1.1 200
Content-Language: en-US
Content-Type: application/json;charset=UTF-8
Date: Wed, 31 Mar 2021 18:31:37 GMT
Transfer-Encoding: chunked
X-Application-Context: application:development
{
"message": "This Is My Custom Message"
}