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.0.1

1 Training

Grails Training - Developed and delivered by the folks who created and actively maintain the Grails framework!.

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 1.8 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
    compileOnly "io.micronaut:micronaut-inject-groovy"

Create a file named AddressConfiguration.groovy.

src/main/groovy/example/grails/AddressConfiguration.groovy
package example.grails

import io.micronaut.context.annotation.ConfigurationProperties

@ConfigurationProperties("address") (1)
class AddressConfiguration {
    String street
    String city
    String country
}
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
address:
    street: 221B Baker Street
    city: London
    country: United Kingdom

3.2 Tag Lib

Create a Tag Library to render the address:

grails-app/taglib/example/grails/AddressTagLib.groovy
package example.grails

import org.springframework.beans.factory.annotation.Autowired

class AddressTagLib {

    static namespace = "app" (1)

    @Autowired (2)
    AddressConfiguration addressConfiguration (3)

    def address = { attrs, body ->
        out << """\
<div class='adr'>
    <div class='street-address'>${addressConfiguration.street}</div>
    <span class='locality'>${addressConfiguration.city}</span>,
    <div class='country-name'>${addressConfiguration.country}</div>
</div>""" (4)
    }
}
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
package example.grails

import geb.spock.GebSpec
import grails.testing.mixin.integration.Integration

@Integration
class AddressSpec extends GebSpec {

    def "verify address is displayed in homepage"() {
        when:
        browser.go("/")

        then:
        browser.driver.pageSource.contains('221B Baker Street')
    }
}

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

OCI sponsored the creation of this Guide. OCI offers several Grails services:

Free consultation

The OCI Grails Team includes Grails co-founders, Jeff Scott Brown and Graeme Rocher. Check our Grails courses and learn from the engineers who developed, matured and maintain Grails.

Grails OCI Team