In this post I shall give an overview of sequence of events that happens during bootstrapping of Grails application
Bootstrap can be divided into following 4 steps from conceptual point of view
- Loading Plugins and init grailsApplication(Implementation of GrailsApplication Interface )
- Create Parent Application Context
- Create Grails Root Application Context
- Create WebAppliationContext for GrailsDispatcherServlet
Below is diagrammatic representation of same
Lets look at each of above steps in details
Loading Plugins and GrailsApplication
Before talking about workflow of loading plugins, I want briefly discuss Gant Scripts
Gant Scripts
- These are essentially Groovy wrapper around ant scripts.They support concept of targets and dependencies
- Grails searches for these scripts in below directories
- USER_HOME/.grails/scripts
- PROJECT_HOME/scripts
- PROJECT_HOME/plugins/*/scripts
- GRAILS_HOME/scripts
- when you run any grails command example.- grails run-app, it will search for RunApp.groovy in above directories and execute default target in that script
- Scripts which do not have ‘_’ ( underscore) in their name, have a default target and can be run using grails command
- examples – RunApp, CreateScript,CreateService etc
- Any script whose name begins with ‘_’ (underscore) are internal scripts, i.e they are used by other scripts, and do not have default target
- example - _PluginDependencies.groovy, _PackagePlugins.groovy etc
- include keyword specifies other scripts to be included in particular script
- depends keyword executes target from other script.
More about Gant scripts can be found here
Now lets look at the workflow. It can be represented by below diagram
Workflow Key Points
- There are 4 main Gant scripts shown in workflow – RunApp.groovy, _GrailsPackage.groovy, _PluginDependencies.groovy, _GrailsRun.groovy
- Each box in diagram represents target which is being executed
- Sequence of action is represented by arrows . Dashed line indicates return to caller script
- RunApp.groovy is called when you run command , grails run-app ($GRAILS_HOME/scripts/RunApp.groovy)
- RunApp has dependency named packageApp( from _GrailsPackage.groovy) . Two important targets called by packageApp are
- loadPlugins
- it initializes Java Objects – DefaultGrailsApplication, DefaultGrailsPluginManager (if you want profile time taken to load individual plugins, you could set enableProfile to true and it will load ProfilingGrailsPluginManager)
- loads individual plugins and stores them as list in DefaultPluginManager
- perform artefactConfigurations for plugins . (Artefacts are resources that fulfil some convention eg Controller, Services.)
- initializes grailsApplication
- populatingAllClasses – essentially store all the classes which are loaded in a variable
- ConfigureLoadedClasses
- initialize Artifact Handlers – eg. ControllerArtefactHandler, ServiceArtefactHandler etc…
- inspect loaded classes and determine which ones are artefacts
- generateWebXML
- calls doWebDescriptor for each plugin
- Generates web.xml and stores it @ /USER_HOME/.grails/2.1.1/projects/PROJECT_NAME/resources
- loadPlugins
- Finally runApp is called which starts Embeded TomcatServer
Root Application Context
When GrailsContextLoader loads and below are 3 steps performed by it
- Create Parent Application Context
- Create Root Application Context
- Execute Grails Bootstrap classes
Lets look at second step above in more details .
Below are key Points
- create WebRunTimeSpringConfig
- Generation of Spring Config at runtime
- registerProvidedArtifacts – register Precompiled Artifacts
- pluginManager.doRunTimeSpringConfig
- Hook into Grails runtime configuration, configure beans.
- run closure doWithSpring associated with each plugin
- InitApplicationContext
- Create GrailsApplicationContext
- RegisterBeans
- Register BeanConfigs,BeanNames, Aliases with BeanRegistry.
- Refresh the context created
- pluginManager.doDynamicMethods
- register dynamic methods with any classes based on some criteria (inspect properties, add new methods for benchmarking etc)
- pluginManager.doPostProcessing
- execute doWithApplicationContext associated with each plugin
WebApplicationContext and GrailsDispatcherServlet
This is the last step before application launches. Key points regards this
- an instance of GrailsDispatcherServlet is created
- Initialize and configure WebApplicationContext . Below are strategies initialized for default settings
- MultiPartResolver – ContentLengthAwareCommonsMultipartResolver
- LocalResolver – SessionLocaleResolver
- ThemeResolver – FixedThemeResolver
- HandlerMapping
- GrailsControllerHandlerMapping
- RequestMappingHandlerMapping
- HandlerAdapters
- RequestMappingHandlerAdapter
- SimpleControllerHandlerAdapter
- HandlerExceptionResolver – GrailsExceptionResolver
- ViewNameTranslator – DefaultRequestToViewNameTranslator
- ViewResolvers – ScaffoldingViewResolver
- FlashMapManager – SessionFlashMapManager.
You can read about DispatcherServlet in one of my other article. Once above step is complete , application is ready to run.
A good presentation about architecture of Grails can be found here . Presentation is old, but core-concepts are explained very elegantly.



Very useful, I am impressed by your in depth coverage. I want to develop some short tutorials in line with Rails Casts. Would you like to help/ partner with me. thanks. (vamsi v)
Vamsi – I am glad you liked the post. Regards your idea with Screen casts, could you send me email about your thoughts/ideas and we can discuss
Sure,
Thanks for responding, my email is viz.ply@gmail.com, I live in Dallas, TX.
Please send in the email with your contact info and I shall put together some details.
Good description and explanation
but i am lacking inline comparison of Grails with Spring MVC
i believe these two are conceptually same for some initial only
and your posted URL – http://www.slideshare.net/hominhchuc/graemerocher-grailsplug-inarchitecture-presentation
is not working; kindly rectify and/or correct t this
Thanks