【问题标题】:Grails - override a bean property value in resources.groovyGrails - 覆盖 resources.groovy 中的 bean 属性值
【发布时间】:2013-04-17 14:55:08
【问题描述】:

在 Grails i18n 插件 defined thusly 中定义了一个 messageSource bean:

messageSource(PluginAwareResourceBundleMessageSource) {
  basenames = baseNames.toArray()
  fallbackToSystemLocale = false
  pluginManager = manager
  ....
}

是否可以仅覆盖我的 resources.groovy 中的 fallbackToSystemLocale 值的配置,例如:

messageSource {
    fallbackToSystemLocale = true
} 

上面的不行,我得到一个错误:“Error creation bean with name 'messageSource': Bean definition is abstract”

【问题讨论】:

  • 我不知道你是否可以这样做,但你可以使用setter:messageSource.setFallbackToSystemLocale(true)

标签: spring grails groovy


【解决方案1】:

有什么理由不简单地更新 BootStrap.groovy 中的 bean 吗?

class BootStrap {
    def def messageSource
    def init = { servletContext ->
        messageSource.fallbackToSystemLocale = true
    }
}

如果您想在 BootStrap 运行之前修改 bean,您可以使用 BeanPostProcessor,如 this blog post

src/groovy/yourpkg/CustomBeanPostProcessor:

import org.springframework.beans.factory.config.BeanPostProcessor

class CustomBeanPostProcessor implements BeanPostProcessor{

    @Override
    Object postProcessBeforeInitialization(Object bean, String beanName) {
        return bean
    }

    @Override
    Object postProcessAfterInitialization(Object bean, String beanName) {
        if(beanName == 'messageSource') {
            bean.setFallbackToSystemLocale = true
        }
        return bean
    }
}

resources.groovy:

beans = {
    customBeanPostProcessor(CustomBeanPostProcessor)
}

【讨论】:

  • BeanPostProcessor 似乎是一种更有条理的方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-31
  • 2015-10-28
  • 2011-12-01
  • 2019-04-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多