[英]Spring DSL in Grails - resources.groovy - bean configuration in a different file?
[英]Grails - override a bean property value in resources.groovy
因此定义的Grails i18n插件中定义了一个messageSource
bean:
messageSource(PluginAwareResourceBundleMessageSource) {
basenames = baseNames.toArray()
fallbackToSystemLocale = false
pluginManager = manager
....
}
是否有可能从我的resources.groovy中覆盖fallbackToSystemLocale
值的配置,例如:
messageSource {
fallbackToSystemLocale = true
}
上面的方法不起作用,我得到一个错误:“创建名称为'messageSource'的bean时出错:Bean定义是抽象的”
有没有理由不只是在BootStrap.groovy
更新bean?
class BootStrap {
def def messageSource
def init = { servletContext ->
messageSource.fallbackToSystemLocale = true
}
}
如果要在BootStrap运行之前修改bean,可以使用BeanPostProcessor
如本博文所述 。
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)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.