SpringBoot @ConfigurationProperties in Grails App
Grails 5 apps are Spring Boot apps. Learn how property values can be bound to structured objects through @ConfigurationProperties.
Authors: Sergio del Amo
Grails Version: 4
1 Training
Apache Grails Training
Apache Grails is now part of the Apache Software Foundation. The community-maintained training catalog is being migrated; in the meantime see the Learning page for current resources, recorded talks, and links to other community-supplied training material.
2 Getting Started
In this guide, we are going to demonstrate Grails file transfer capabilities by creating an app which downloads an excel file with a list of books.
2.1 What you will need
To complete this guide, you will need the following:
-
Some time on your hands
-
A decent text editor or IDE
-
JDK 11 or greater installed with
JAVA_HOMEconfigured appropriately
2.2 Solution
We recommend you to follow the instructions in the next sections and create the app step by step. However, you can go right to the completed example.
-
Download and unzip the source
or
-
Clone the Git repository:
git clone https://github.com/grails-guides/grails-configuration-properties.git
Then, cd into the complete folder which you will find in the root project of the downloaded/cloned project.
3 Writing the App
grails create-app example.grails.complete
3.1 Configuration Properties
In this section we are going to explore how property values can be bound to structured objects through @ConfigurationProperties.
First, we need to annotate Application.groovy with @ComponentScan.
link:../../snippets/grails-app/init/example/grails/Application.groovy[role=include]
| 1 | @ComponentScan tells Spring to look for other components, configurations, and services in the specified package. Spring is able to auto scan, detect and register your beans or components from pre-defined project package. If no package is specified current class package is taken as the root package. |
Create a file named AddressConfiguration.groovy.
link:../../snippets/src/main/groovy/example/grails/AddressConfiguration.groovy[role=include]
| 1 | We have added the @Configuration annotation for Spring to be able to find this bean and make it a candidate for injection. |
| 2 | The @ConfigurationProperties annotation takes the configuration prefix. |
Any properties defined in the property file that has the prefix address and the same name as one of the properties are automatically assigned to this object.
Add some properties to application.yml
link:../../snippets/grails-app/conf/application.yml[role=include]
3.2 Tag Lib
Create a Tag Library to render the address:
link:../../snippets/grails-app/taglib/example/grails/AddressTagLib.groovy[role=include]
| 1 | By default, tags are added to the default Grails namespace and are used with the g: prefix in GSP pages. However, you can specify a different namespace by adding a static property to your TagLib class. |
| 2 | You can inject AddressConfiguration as any other bean into your TagLib. |
| 3 | Create a valid adr microformat HTML snippet. |
3.3 Unit Tests
Create a unit test for the TagLib:
link:../../snippets/src/test/groovy/example/grails/AddressTagLibSpec.groovy[role=include]
| 1 | Tag libraries and GSP pages can be tested with the grails.testing.web.taglib.TagLibUnitTest trait. |
| 2 | To provide or replace beans in the context, you can override the doWithSpring method in your test. |
| 3 | Adding the TagLibUnitTest trait to a test causes a new tagLib field to be automatically created for the TagLib class under test. The tagLib property can be used to test calling tags as function calls. |
3.4 Acceptance Tests
Edit grails-app/views/index.gsp, the GSP that is currently rendered when you visit the home page / and add the next snippet:
...
<div id="content" role="main">
....
...
<app:address/>
</div>
...
Now we can create an acceptance test with Geb to verify the address is rendered in the home page:
link:../../snippets/src/integration-test/groovy/example/grails/AddressSpec.groovy[role=include]
4 Test the app
To run the tests:
./grailsw
grails> test-app
grails> open test-report
or
./gradlew check
open build/reports/tests/index.html
5 Help with Grails
Help with Apache Grails
Apache Grails is supported by an active community of contributors and the Apache Software Foundation. If you need help working through a guide, want to discuss the framework, or have run into something that looks like a bug, the channels below are the right place to start.
-
Slack - real-time conversation with the Apache Grails community.
-
dev@grails.apache.org">Developer mailing list - design discussions and contributor coordination.
-
users@grails.apache.org">Users mailing list - end-user questions and answers.
-
Issue tracker on GitHub - file a bug or feature request against the framework.
For Grails plugins, see the matching project on the apache org or the plugin’s own GitHub repository.