thom_nic
thom_nic

Reputation: 8143

Grails - override a bean property value in resources.groovy

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"

Upvotes: 4

Views: 3160

Answers (1)

codelark
codelark

Reputation: 12334

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)
}

Upvotes: 6

Related Questions