gpt4 book ai didi

spring - 如何正确获取 src/groovy 中的 'messageSource'?

转载 作者:行者123 更新时间:2023-12-02 13:48:04 24 4
gpt4 key购买 nike

我需要在 src\groovy 的类中获取 messageSourceUrlMappings.groovy中使用了这个类,在我使用这个类的阶段,应用程序还没有完全启动。

目前我正在使用以下并且它有效:

MessageSource messageSource = ApplicationHolder.application.mainContext.getBean('messageSource')
String message = messageSource.getMessage("code", null, "default", locale)

但是 ApplicationHolder 已被弃用,有没有办法在不使用 ApplicationHolder 的情况下实现相同的目标?

//我正在使用 Grails 2.0.1

最佳答案

根据我有限的经验,我发现在 POGO 中获取 messageSource 的最简单方法就是:

Holders.applicationContext.getBean("messageSource")

我尝试的其他方法(实现 MessageSourceAware 或添加 @Component/@Autowired 注释)导致了其他问题。

关于spring - 如何正确获取 src/groovy 中的 'messageSource'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14353119/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com