猿问

如何初始化 MessageSource 以注入控制器?

我搜索了互联网的大部分,但没有找到明确的答案......


我想做一件简单的事情:就像在 Grails 中一样,有一个单例MessageSource类型可以在控制器中使用它来显示 i18n 消息。


我想messageSource从这个文件初始化我的:


src/main/resources/my/package/i18n/Messages*.properties


test=FooBar

初始化尝试:


class AppConfig {


  @Singleton

  MessageSource messageSource() {

    new ResourceBundleMessageSource( 'my.package.i18n.Messages' )

  }


}

要注入的控制器:


class CRUDController<T> {


  @Inject

  MessageSource messageSource


  @Transactional( readOnly=true )

  @Get( '/' )

  Map<String,Object> list() {

    log.info "$messageSource -> ${messageSource.getMessage( 'test', MessageSource.MessageContext.DEFAULT )}"

    [:]

  }


}

日志信息打印如下:


io.micronaut.runtime.context.CompositeMessageSource@79a1d030 -> Optional.empty


因此, messageSource 以某种方式(和某处)被错误初始化。


做这个简单的事情最简单直接的方法是什么?


慕侠2389804
浏览 120回答 1
1回答

紫衣仙女

请参阅https://github.com/jeffbrown/injecteermessagesource上的项目。https://github.com/jeffbrown/injecteermessagesource/blob/beba0773d41cb475d8d837acc19d0dd41f5b4431/src/main/groovy/injecteermessagesource/CRUDController.groovypackage injecteermessagesourceimport io.micronaut.context.MessageSourceimport javax.inject.Injectclass CRUDController<T> {&nbsp; &nbsp; protected final Class<T> clazz&nbsp; &nbsp; @Inject&nbsp; &nbsp; MessageSource messageSource&nbsp; &nbsp; protected CRUDController( Class<T> clazz ) {&nbsp; &nbsp; &nbsp; &nbsp; this.clazz = clazz&nbsp; &nbsp; }}https://github.com/jeffbrown/injecteermessagesource/blob/471f6436511c99511426094a8c269112e93149e6/src/main/groovy/injecteermessagesource/PersonController.groovypackage injecteermessagesourceimport io.micronaut.context.MessageSourceimport io.micronaut.http.annotation.Controllerimport io.micronaut.http.annotation.Get@Controller('/people')class PersonController&nbsp; extends CRUDController<Person> {&nbsp; &nbsp; protected PersonController() {&nbsp; &nbsp; &nbsp; &nbsp; super(Person)&nbsp; &nbsp; }&nbsp; &nbsp; @Get('/')&nbsp; &nbsp; String index() {&nbsp; &nbsp; &nbsp; &nbsp; def context = MessageSource.MessageContext.DEFAULT&nbsp; &nbsp; &nbsp; &nbsp; def message = messageSource.getMessage('test', context)&nbsp; &nbsp; &nbsp; &nbsp; message.get()&nbsp; &nbsp; }}https://github.com/jeffbrown/injecteermessagesource/blob/471f6436511c99511426094a8c269112e93149e6/src/main/groovy/injecteermessagesource/AppConfig.groovypackage injecteermessagesourceimport io.micronaut.context.MessageSourceimport io.micronaut.context.annotation.Beanimport io.micronaut.context.annotation.Factoryimport io.micronaut.context.i18n.ResourceBundleMessageSource@Factoryclass AppConfig {&nbsp; &nbsp; @Bean&nbsp; &nbsp; MessageSource messageSource() {&nbsp; &nbsp; &nbsp; &nbsp; new ResourceBundleMessageSource('my.somepackage.i18n.Messages')&nbsp; &nbsp; }}https://github.com/jeffbrown/injecteermessagesource/blob/471f6436511c99511426094a8c269112e93149e6/src/main/resources/my/somepackage/i18n/Messages.propertiestest=FooBar这一切似乎都有效:$ curl localhost:8080/peopleFooBar
随时随地看视频慕课网APP

相关分类

Java
我要回答