In the vein of some Java Interview questions, I’ve started thinking of some Grails specific questions I could ask or be asked in job interviews. Here are some of my questions with some answers. Please feel free to submit your own questions or challenge my answers.
I have a parameter in my Config.groovy file named layer1.prop1. How can I access this in a controller? How can I access this in a service?
In a controller, I can inject grailsApplication via def grailsApplication and access this variable via grailsApplication.config.layer1.prop.
Anywhere else, I can use the ConfigurationHolder and access it via ConfigurationHolder.config.layer1.prop1
How do I edit the web.xml file of my grails application?
First, I need to generate the template files via grails install-templates. The web.xml template will be under src/templates/war directory.
Alternatively, I could also create a plugin and use the doWithWebDescriptor method to add or remove nodes from my web.xml file.
Explain the concept of externalized configuration. What is this good for?
Externalized configurations let me define other config and .properties files that don’t live within the grails-app/conf directory.
To enable this, I need to define locations for the application to look for configuration files via the grails.config.locations in Config.groovy in relation to the classpath or filesystem.
The most popular uses of externalized configurations is to enable configuration changes without having to re-build a war file. By planning ahead, configurations can even be reloaded without restarting the application container via this mechanism.
Please explain the difference between the Config.groovy and BuildConfig.groovy files.
The Config.groovy file contains configuration properties and information needed when running your application. It contains properties used by your services, controllers, etc.
The BuildConfig.groovy file contains information relevant to building and deploying your application, such as repositories, jar file dependencies and war file name.
Please explain what Bootstrap.groovy does.
Bootstrap.groovy allows you to define code that is ran every time your grails application starts up. Continue reading