18.5 Hooking into Runtime Configuration

Grails provides a number of hooks to leverage the different parts of the system and perform runtime configuration by convention.

Hooking into the Grails Spring configuration

First, you can hook in Grails runtime configuration overriding the doWithSpring method from the Plugin class and returning a closure that defines additional beans. For example the following snippet is from one of the core Grails plugins that provides i18n support:

  1. import org.springframework.web.servlet.i18n.CookieLocaleResolver
  2. import org.springframework.web.servlet.i18n.LocaleChangeInterceptor
  3. import org.springframework.context.support.ReloadableResourceBundleMessageSource
  4. import grails.plugins.*
  5. class I18nGrailsPlugin extends Plugin {
  6. def version = "0.1"
  7. Closure doWithSpring() {{->
  8. messageSource(ReloadableResourceBundleMessageSource) {
  9. basename = "WEB-INF/grails-app/i18n/messages"
  10. }
  11. localeChangeInterceptor(LocaleChangeInterceptor) {
  12. paramName = "lang"
  13. }
  14. localeResolver(CookieLocaleResolver)
  15. }}
  16. }

This plugin configures 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.

Customizing the Servlet Environment

In previous versions of Grails it was possible to dynamically modify the generated web.xml. In Grails 3.x there is no web.xml file and it is not possible to programmatically modify the web.xml file anymore.

However, it is possible to perform the most commons tasks of modifying the Servlet environment in Grails 3.x.

Adding New Servlets

If you want to add a new Servlet instance the simplest way is simply to define a new Spring bean in the doWithSpring method:

  1. Closure doWithSpring() {{->
  2. myServlet(MyServlet)
  3. }}

If you need to customize the servlet you can use Spring Boot’s ServletRegistrationBean:

  1. Closure doWithSpring() {{->
  2. myServlet(ServletRegistrationBean, new MyServlet(), "/myServlet/*") {
  3. loadOnStartup = 2
  4. }
  5. }}

Adding New Servlet Filters

Just like Servlets, the simplest way to configure a new filter is to simply define a Spring bean:

  1. Closure doWithSpring() {{->
  2. myFilter(MyFilter)
  3. }}

However, if you want to control the order of filter registrations you will need to use Spring Boot’s FilterRegistrationBean:

  1. myFilter(FilterRegistrationBean) {
  2. filter = bean(MyFilter)
  3. urlPatterns = ['/*']
  4. order = Ordered.HIGHEST_PRECEDENCE
  5. }
Grails' internal registered filters (GrailsWebRequestFilter, HiddenHttpMethodFilter etc.) are defined by incrementing HIGHEST_PRECEDENCE by 10 thus allowing several filters to be inserted before or between Grails' filters.

Doing Post Initialisation Configuration

Sometimes it is useful to be able do some runtime configuration after the Spring ApplicationContext has been built. In this case you can define a doWithApplicationContext closure property.

  1. class SimplePlugin extends Plugin{
  2. def name = "simple"
  3. def version = "1.1"
  4. @Override
  5. void doWithApplicationContext() {
  6. def sessionFactory = applicationContext.sessionFactory
  7. // do something here with session factory
  8. }
  9. }