読者です 読者をやめる 読者になる 読者になる

GrailsでBeanFactoryPostProcessorを使ってSpringBeanの初期化をごにょごにょする

元ネタ: http://www.intelligrape.com/blog/2012/05/12/overriding-properties-in-a-spring-bean-with-beanfactorypostprocessor/

BeanPostProcessorの実装クラスを登録すると、SpringBeanの初期化時に処理をフック出来るみたいです。結構簡単。

BeanPostProcessorの実装クラスを作る。

class CustomBeanProcessor implements BeanPostProcessor {

    @Override
    Object postProcessBeforeInitialization(Object bean, String name) {
        println "post process ${name}"
        // beanに対してゴニョゴニョ
        bean
    }

    @Override
    Object postProcessAfterInitialization(Object bean, String name) {
        println "after process: ${name}"
        // beanに対してゴニョゴニョ
        bean
    }
}

それをresources.groovyで登録する

beans = {
    customBeanProcessor(CustomBeanProcessor)
}

試しに起動してみる

grails> run-app
| Running Grails application
post process messageSource
after process: messageSource
post process org.springframework.transaction.interceptor.TransactionInterceptor#
0
after process: org.springframework.transaction.interceptor.TransactionIntercepto
r#0
post process viewNameTranslator
after process: viewNameTranslator
post process localeChangeInterceptor
after process: localeChangeInterceptor
post process custombeanpostprocessor.test.BookDomainClass
after process: custombeanpostprocessor.test.BookDomainClass
after process: custombeanpostprocessor.test.BookDomainClass
post process custombeanpostprocessor.test.BookValidator
after process: custombeanpostprocessor.test.BookValidator
post process mimeTypes
after process: mimeTypes
post process xmlParsingParameterCreationListener
after process: xmlParsingParameterCreationListener
post process custombeanpostprocessor.test.BookPersistentClass
after process: custombeanpostprocessor.test.BookPersistentClass
post process org.grails.internal.URL_MAPPINGS_HOLDER
after process: org.grails.internal.URL_MAPPINGS_HOLDER
after process: org.grails.internal.URL_MAPPINGS_HOLDER
post process urlMappingsTargetSource
after process: urlMappingsTargetSource
post process grailsUrlMappingsHolder
after process: grailsUrlMappingsHolder
post process org.codehaus.groovy.grails.web.filters.JavascriptLibraryFiltersClas
s
after process: org.codehaus.groovy.grails.web.filters.JavascriptLibraryFiltersCl
ass
post process JQueryServiceServiceClass
after process: JQueryServiceServiceClass
post process classLoader
after process: classLoader
post process gspTagLibraryLookup
post process org.codehaus.groovy.grails.plugins.web.taglib.SitemeshTagLib
after process: org.codehaus.groovy.grails.plugins.web.taglib.SitemeshTagLib
post process org.codehaus.groovy.grails.plugins.web.taglib.PluginTagLib
after process: org.codehaus.groovy.grails.plugins.web.taglib.PluginTagLib
post process org.codehaus.groovy.grails.plugins.web.taglib.ValidationTagLib
after process: org.codehaus.groovy.grails.plugins.web.taglib.ValidationTagLib
post process grailsResourceLocator
after process: grailsResourceLocator
post process errorsViewStackTracePrinter
after process: errorsViewStackTracePrinter
after process: classLoader
post process groovyPageResourceLoader
after process: groovyPageResourceLoader
post process groovyPageLocator
after process: groovyPageLocator
post process jspTagLibraryResolver
after process: jspTagLibraryResolver
post process groovyPagesTemplateEngine
after process: groovyPagesTemplateEngine
post process controllerToScaffoldedDomainClassMap
after process: controllerToScaffoldedDomainClassMap
post process scaffoldedActionMap
after process: scaffoldedActionMap
post process scaffoldingTemplateGenerator
after process: scaffoldingTemplateGenerator
post process groovyPagesTemplateRenderer
after process: groovyPagesTemplateRenderer
post process org.codehaus.groovy.grails.plugins.web.taglib.RenderTagLib
after process: org.codehaus.groovy.grails.plugins.web.taglib.RenderTagLib
post process org.codehaus.groovy.grails.plugins.web.taglib.JavascriptTagLib
after process: org.codehaus.groovy.grails.plugins.web.taglib.JavascriptTagLib
post process org.codehaus.groovy.grails.plugins.web.taglib.FormTagLib
after process: org.codehaus.groovy.grails.plugins.web.taglib.FormTagLib
post process org.codehaus.groovy.grails.plugins.web.taglib.FormatTagLib
after process: org.codehaus.groovy.grails.plugins.web.taglib.FormatTagLib
post process org.codehaus.groovy.grails.plugins.web.taglib.CountryTagLib
after process: org.codehaus.groovy.grails.plugins.web.taglib.CountryTagLib
after process: grailsUrlMappingsHolder
post process grailsUrlConverter
after process: grailsUrlConverter
post process grailsLinkGenerator
after process: grailsLinkGenerator
post process org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib
after process: org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib
post process jQueryConfig
after process: jQueryConfig
post process JQueryTagLib
after process: JQueryTagLib
post process grailsResourceProcessor
after process: grailsResourceProcessor
post process org.grails.plugin.resource.ResourceTagLib
after process: org.grails.plugin.resource.ResourceTagLib
post process JQueryResourceTagLib
after process: JQueryResourceTagLib
after process: gspTagLibraryLookup
post process instanceTagLibraryApi
after process: instanceTagLibraryApi
post process instanceConvertersControllersApi
after process: instanceConvertersControllersApi
post process localeResolver
after process: localeResolver
post process jspViewResolver
after process: jspViewResolver
post process groovyPageLayoutFinder
after process: groovyPageLayoutFinder
post process openSessionInViewInterceptor
after process: openSessionInViewInterceptor
post process filterInterceptor
after process: filterInterceptor
post process controllerHandlerMappings
after process: controllerHandlerMappings
post process proxyHandler
after process: proxyHandler
post process convertersConfigurationInitializer
after process: convertersConfigurationInitializer
post process annotationHandlerMapping
after process: annotationHandlerMapping
post process org.codehaus.groovy.grails.web.filters.JavascriptLibraryFilters
after process: org.codehaus.groovy.grails.web.filters.JavascriptLibraryFilters
post process instanceControllerTagLibraryApi
after process: instanceControllerTagLibraryApi
post process flushingRedirectEventListener
after process: flushingRedirectEventListener
post process exceptionHandler
after process: exceptionHandler
post process (inner bean)
after process: (inner bean)
post process errorsJsonMarshallerRegisterer
after process: errorsJsonMarshallerRegisterer
post process grailsControllerHelper
after process: grailsControllerHelper
post process mainSimpleController
after process: mainSimpleController
post process xmlErrorsMarshaller
after process: xmlErrorsMarshaller
post process (inner bean)#1
after process: (inner bean)#1
post process errorsXmlMarshallerRegisterer
after process: errorsXmlMarshallerRegisterer
post process persistenceInterceptor
after process: persistenceInterceptor
post process simpleControllerHandlerAdapter
after process: simpleControllerHandlerAdapter
post process annotationHandlerAdapter
after process: annotationHandlerAdapter
post process instanceControllersApi
after process: instanceControllersApi
post process jsonParsingParameterCreationListener
after process: jsonParsingParameterCreationListener
post process jsonErrorsMarshaller
after process: jsonErrorsMarshaller
post process shutdownHook
after process: shutdownHook
post process multipartResolver
after process: multipartResolver
after process: mimeTypes
post process grailsMimeUtility
after process: grailsMimeUtility
post process grailsDomainClassMappingContext
after process: grailsDomainClassMappingContext
post process responseMimeTypesApi
after process: responseMimeTypesApi
post process requestMimeTypesApi
after process: requestMimeTypesApi
after process: org.codehaus.groovy.grails.web.filters.JavascriptLibraryFiltersCl
ass
post process org.springframework.web.servlet.theme.FixedThemeResolver
after process: org.springframework.web.servlet.theme.FixedThemeResolver
post process org.springframework.web.servlet.support.DefaultFlashMapManager
after process: org.springframework.web.servlet.support.DefaultFlashMapManager
| Server running. Browse to http://localhost:8080/CustomBeanPostprocessor-test
| Application loaded in interactive mode. Type 'exit' to shutdown.
| Enter a script name to run. Use TAB for completion:
g