We can define an immutable configuration by defining a class and use the @ConfigurationInject annotation on a constructor of a @ConfigurationProperties or @EachProperty bean.
Example
@ConfigurationProperties("my.app")
public class AppConfig {
private final String name;
private final String version;
private final String description;
@ConfigurationInject
public AppConfig(@Nonnull String name, String version,
@Bindable(defaultValue = "chat server send and receive messages") String description) {
this.name = name;
this.version = version;
this.description = description;
}
.......
.......
}
Find the below working application.
Step 1: Create new maven project ‘micronaut-immutable-configuration-inject’.
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-configuration-inject</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.ConfigurationInject;
import io.micronaut.context.annotation.ConfigurationProperties;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.bind.annotation.Bindable;
@ConfigurationProperties("my.app")
public class AppConfig {
private final String name;
private final String version;
private final String description;
@ConfigurationInject
public AppConfig(@NonNull String name, String version,
@Bindable(defaultValue = "chat server send and receive messages") String description) {
this.name = name;
this.version = version;
this.description = description;
}
public String getName() {
return name;
}
public String getVersion() {
return version;
}
public String getDescription() {
return description;
}
}
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-configuration-inject-0.1.jar’ in project target folder.
$ ls ./target/
classes
generated-sources
generated-test-sources
maven-archiver
maven-status
micronaut-immutable-configuration-inject-0.1.jar
original-micronaut-immutable-configuration-inject-0.1.jar
test-classes
Execute below command to run the application.
java -jar ./target/micronaut-immutable-configuration-inject-0.1.jar
You will see below 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.
No comments:
Post a Comment