Thursday 18 May 2023

Micronaut: Define immutable configuration using @ConfigurationProperties

In this post, let’s learn how to define an immutable configuration using @ConfigurationProperties annotation.

 

How to define an immutable configuration?

Just define an interface and annotate it with @ConfigurationProperties annotation.

@ConfigurationProperties("my.app")
public interface AppConfig {

  @Bindable(defaultValue = "my chat server")
  @Nonnull
  String getName();

  @Bindable(defaultValue = "1.0.0")
  String getVersion();

  @Bindable(defaultValue = "chat server send and receive messages")
  String getDescription();

}

Find the below working application.

 

Step 1: Create new maven project ‘micronaut-immutable-config’.

 

Step 2: Update pom.xml with maven dependencies.

 

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.sample.app</groupId>
  <artifactId>micronaut-immutable-config</artifactId>
  <version>0.1</version>
  <packaging>jar</packaging>

  <parent>
    <groupId>io.micronaut</groupId>
    <artifactId>micronaut-parent</artifactId>
    <version>3.7.3</version>
  </parent>

  <properties>
    <packaging>jar</packaging>
    <jdk.version>11</jdk.version>
    <release.version>11</release.version>
    <micronaut.version>3.7.3</micronaut.version>
    <micronaut.runtime>netty</micronaut.runtime>
    <exec.mainClass>com.sample.app.App</exec.mainClass>
  </properties>

  <repositories>
    <repository>
      <id>central</id>
      <url>https://repo.maven.apache.org/maven2</url>
    </repository>
  </repositories>

  <dependencies>
    <dependency>
      <groupId>io.micronaut</groupId>
      <artifactId>micronaut-inject</artifactId>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>io.micronaut</groupId>
      <artifactId>micronaut-validation</artifactId>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>io.micronaut</groupId>
      <artifactId>micronaut-http-client</artifactId>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>io.micronaut</groupId>
      <artifactId>micronaut-http-server-netty</artifactId>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>io.micronaut</groupId>
      <artifactId>micronaut-jackson-databind</artifactId>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>jakarta.annotation</groupId>
      <artifactId>jakarta.annotation-api</artifactId>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>io.micronaut.test</groupId>
      <artifactId>micronaut-test-junit5</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-api</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-engine</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>io.micronaut.build</groupId>
        <artifactId>micronaut-maven-plugin</artifactId>
      </plugin>
      
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <!-- Uncomment to enable incremental compilation -->
          <!-- <useIncrementalCompilation>false</useIncrementalCompilation> -->

          <annotationProcessorPaths combine.children="append">
            <path>
              <groupId>io.micronaut</groupId>
              <artifactId>micronaut-http-validation</artifactId>
              <version>${micronaut.version}</version>
            </path>
          </annotationProcessorPaths>
         
        </configuration>
      </plugin>
    </plugins>
  </build>

</project>

Step 3: Create application.yml file under src/main/resources folder.

 

application.yml

logger:
  levels:
    io.netty: ERROR
    io.micronaut: ERROR

my:
  app:
    name: chat server
    version: 1.23.45

Step 4: Define AppConfig class.

AppConfig.java

package com.sample.app.configuration;

import io.micronaut.context.annotation.ConfigurationProperties;
import io.micronaut.core.bind.annotation.Bindable;

import jakarta.annotation.Nonnull;

@ConfigurationProperties("my.app")
public interface AppConfig {

  @Bindable(defaultValue = "my chat server")
  @Nonnull
  String getName();

  @Bindable(defaultValue = "1.0.0")
  String getVersion();

  @Bindable(defaultValue = "chat server send and receive messages")
  String getDescription();

}

Step 5: Define main application class.

 

App.java

package com.sample.app;

import com.sample.app.configuration.AppConfig;

import io.micronaut.context.ApplicationContext;

public class App {

  public static void main(String[] args) {

    try (ApplicationContext applicationContext = ApplicationContext.run()) {

      AppConfig appConfig = applicationContext.getBean(AppConfig.class);
      System.out.println("name : " + appConfig.getName());
      System.out.println("version : " + appConfig.getVersion());
      System.out.println("description : " + appConfig.getDescription());
    }

  }
}

Total project structure looks like below.




Build the project using mvn package command.

Navigate to the folder where pom.xml is located and execute the command ‘mvn package’.

 

Upon command successful execution, you can see the jar file ‘micronaut-immutable-config-0.1.jar’ in project target folder.


$ls ./target/
classes
generated-sources
generated-test-sources
maven-archiver
maven-status
micronaut-immutable-config-0.1.jar
original-micronaut-immutable-config-0.1.jar
test-classes

Execute below command to run the application.

java -jar ./target/micronaut-immutable-config-0.1.jar

You will see following messages in the console.

name : chat server
version : 1.23.45
description : chat server send and receive messages

You can download this application from this link.



Previous                                                    Next                                                    Home

No comments:

Post a Comment