Last updated by mauk81 1 month ago

Runtime Configuration

Grails automatically configures itself at runtime using the conventions within the classes and Spring. The class that performs the runtime configuration is called org.codehaus.groovy.grails.commons.spring.GrailsRuntimeConfigurator which takes a GrailsApplication and produces a bunch of bean references which can be used to create a Spring application context.

The GrailsRuntimeConfigurator class does many things including, but not limited to:

  • Configuring the data source
  • Setting up the Hibernate session factory
  • Creating the handler mappings for the Spring MVC servlet
  • Setting up any Quartz scheduled tasks
  • Configuring Grails service classes for transaction demarcation
An example of its usage can be seen in the grails.util.GrailsUtil class which is used when running unit tests and scaffolding. It essentially bootstraps the Grails environment using the GrailsRuntimeConfigurator class and an applicationContext.xml on the classpath:

ApplicationContext parent = new ClassPathXmlApplicationContext("applicationContext.xml");
    DefaultGrailsApplication application = (DefaultGrailsApplication)parent.getBean("grailsApplication", DefaultGrailsApplication.class);
    GrailsRuntimeConfigurator config = new GrailsRuntimeConfigurator(application,parent);

ApplicationContext appCtx = config.configure(new MockServletContext());

When a Grails web application is executed this configuration takes place in the org.codehaus.groovy.grails.web.servlet.GrailsDispatcherServlet class which is a class that extends Spring MVC's DispatcherServlet to bootstrap the Grails environment