Japanese Runtime Configuration Plugi...

Last updated by admin 4 years ago

????????????? {excerpt:hidden=true} Runtime Configuration & Plugins {excerpt}

Plugin???????? {excerpt:hidden=true} Understanding the Plugin class {excerpt}

{excerpt:hidden=true} There are a number of other properties you can specific on a plugin class and in the next few sections we look at how you can use those. {excerpt} ???????????????????????????????????????????????????????????????

class ExampleGrailsPlugin {
   def version = 0.1

… }

Grails Spring ?????? {excerpt:hidden=true} Hooking into the Grails Spring configuration {excerpt}

{excerpt:hidden=true} First, you can hook in Grails runtime configuration by providing a closure property called {{doWithSpring}}. For example the following snippet is from one of the core Grails plugins that provides i18n support: {excerpt} ???? {{doWithSpring}} ??????????????????????Grails??????????????????????????????i18n?????Grails?????????????

import org.springframework.web.servlet.i18n.CookieLocaleResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;

class I18nGrailsPlugin {

def version = 0.1

def doWithSpring = { messageSource(ReloadableResourceBundleMessageSource) { basename = "WEB-INF/grails-app/i18n/messages" } localeChangeInterceptor(LocaleChangeInterceptor) { paramName = "lang" } localeResolver(CookieLocaleResolver) } }

{excerpt:hidden=true} This plugin sets up the Grails {{messageSource}} bean and a couple of other beans to manage Locale resolution and switching. It using the Spring Bean Builder syntax to do so. {excerpt} ????????Grails? {{messageSource}} ???????2,3?Locale???????????????????? Spring Bean Builder ???????????????

web.xml?????? {excerpt:hidden=true} Participating in web.xml Generation {excerpt}

{excerpt:hidden=true} Grails generates the {{WEB-INF/web.xml}} file at load time, and although plugins cannot change this file directly, they can participate in the generation of the file. Essentially a plugin can provide a {{doWithWebDescriptor}} closure that gets passed the {{web.xml}} as a XmlSlurper GPathResult. {excerpt} Grails?????? {{WEB-INF/web.xml}} ????????????????????????????????????????????????????????????XmlSlurperGPathResult???web.xml???? {{doWithWebDescriptor}} ?????????????

{excerpt:hidden=true} Consider the below example from the ControllersPlugin: {excerpt} ???ControllersPlugin???????????????

def doWithWebDescriptor = { webXml ->
   def controllers = [] as HashSet

plugin.watchedResources.each { def match = it.filename =~ /(w+)(Controller.groovy$)/ if(match) { def controllerName = match[0][1] controllerName = GCU.getPropertyName(controllerName) controllers << controllerName } } def mappingElement = webXml.'servlet-mapping' controllers.each { c -> mappingElement + { 'servlet-mapping' { 'servlet-name'("grails") 'url-pattern'("/${c}/*") } } } … }

{excerpt:hidden=true} Here the plugin goes through all its watched resources, finding all the controller names and then creates <servlet-mapping> elements appended after the last <servlet-mapping> element found in the web.xml {excerpt} ??????????????????????????????web.xml???<servlet-mapping>????????????<servlet-mapping>????????????

{excerpt:hidden=true} Note that at the point of web.xml generation there is no GrailsApplication instance, so you need to use the conventions in the file names instead. {excerpt} web.xml??????GrailsApplication????????????????????????????????????????????????????

??????????? {excerpt:hidden=true} Doing Post Initialisation Configuration {excerpt}

{excerpt:hidden=true} Sometimes it is useful to be able do some runtime configuration after the Spring ApplicationContext has been built. For this you can define a {{doWithApplicationContext}} closure property. {excerpt} Spring????????????????????????????????????????????????? {{doWithApplicationContext}} ??????????????????

class SimplePlugin {
     def name="simple"
     def version = 1.1

def doWithApplicationContext = { appCtx -> SessionFactory sf = appCtx.getBean("sessionFactory") // do something here with session factory } }