Show Navigation

Micronaut @ConfigurationProperties in Grails App

Grails apps can access many Micronaut features. Learn how property values can be bound to structured objects through @ConfigurationProperties.

Authors: Sergio del Amo, Puneet Behl

Grails Version: 6.0.0-RC1

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 How to complete the guide

To get started do the following:

or

The Grails guides repositories contain two folders:

  • initial Initial project. Often a simple Grails app with some additional code to give you a head-start.

  • complete A completed example. It is the result of working through the steps presented by the guide and applying those changes to the initial folder.

To complete the guide, go to the initial folder

  • cd into grails-guides/grails-configuration-properties-micronaut/initial

and follow the instructions in the next sections.

You can go right to the completed example if you cd into grails-guides/grails-configuration-properties-micronaut/complete

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

Object Computing, Inc. (OCI) sponsored the creation of this Guide. A variety of consulting and support services are available.

OCI is Home to Grails

Meet the Team