Grails - 覆盖resources.groovy中的bean属性值 [英] Grails - override a bean property value in resources.groovy

查看:117
本文介绍了Grails - 覆盖resources.groovy中的bean属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Grails i18n插件中定义了 messageSource bean 定义如此

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

是否可以覆盖<$ code> fallbackToSystemLocale 来自我resources.groovy的值,如下所示:

  messageSource {
fallbackToSystemLocale = true
}

以上操作无效,出现错误:创建名为'messageSource'的bean时出错:Bean定义是抽象的

解决方案

BootStrap.groovy

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

如果你要在BootStrap运行之前修改Bean,可以使用 BeanPostProcessor ,如此博客帖子

src / groovy / yourpkg / CustomBeanPostProcessor :

pre $ import $ or $ $ $ $ $ $ $ $

$ Custom $

@Override
对象postProcessBeforeInitialization(Object bean,String beanName){
返回bean
}

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

resources.groovy:

 $ b $ customBeanPostProcessor 


$

There's a messageSource bean defined in the Grails i18n plugin defined thusly:

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

Is it possible to override the configuration of just the fallbackToSystemLocale value from my resources.groovy, something like:

messageSource {
    fallbackToSystemLocale = true
} 

The above doesn't work, I get an error: "Error creating bean with name 'messageSource': Bean definition is abstract"

解决方案

Is there any reason not to simply update the bean in BootStrap.groovy?

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

If you want to modify beans before BootStrap has run, you can use a BeanPostProcessor as in 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)
}

这篇关于Grails - 覆盖resources.groovy中的bean属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆