Grails - 覆盖resources.groovy中的bean属性值

时间:2013-04-17 14:55:08

标签: spring grails groovy

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

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

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

messageSource {
    fallbackToSystemLocale = true
} 

以上不起作用,我收到一个错误:“创建名为'messageSource'的bean时出错:Bean定义是抽象的”

1 个答案:

答案 0 :(得分:6)

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

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

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

的src /常规/ 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)
}