Show Navigation

Micronaut @ConfigurationProperties in Grails App

Grails 4 apps can access many Micronaut features. 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_HOME configured 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.

or

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.

Make sure you have the micronaut-inject-groovy dependency in build.gradle.

build.gradle
link:../../snippets/build.gradle[role=include]

Create a file named AddressConfiguration.groovy.

src/main/groovy/example/grails/AddressConfiguration.groovy
link:../../snippets/src/main/groovy/example/grails/AddressConfiguration.groovy[role=include]
1 @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

grails-app/conf/application.yml
link:../../snippets/grails-app/conf/application.yml[role=include]

3.2 Tag Lib

Create a Tag Library to render the address:

grails-app/taglib/example/grails/AddressTagLib.groovy
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 To obtain a reference to a Micronaut bean, you have to use the Autowired annotation
3 Inject a Micronaut Bean (AddressConfiguration) into your TagLib using its type.
4 Create a valid adr microformat HTML snippet.

3.3 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:

grails-app/views/index.gsp
...
 <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:

src/integration-test/groovy/example/grails/AddressSpec.groovy
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.

For Grails plugins, see the matching project on the apache org or the plugin’s own GitHub repository.