Friday 17 March 2023

Micronaut: Define and read application properties

How to define application properties?

Application configurations can be provided in Java properties, YAML, JSON, or Groovy files. By convention, following file names are used to define application configuration.

1.   application.yml,

2.   application.properties,

3.   application.json

4.   application.groovy

 

How to read application properties?

Get the instance of Environment.

ApplicationContext applicationContext = ApplicationContext.run()
Environment environment = applicationContext.getEnvironment();

 

Use getter methods of Environment, to read environment properties.

String appName = environment.getProperty("appName", String.class, "demo chat server");

 

'appName' is the property to read from application properties file.

'String.class' specifies the property value type.

'demo chat server' is the default value to return when this property is not exist.

 

Find the below working application.

 

Step 1: Create new maven project ‘micronaut-environment-props-helloworld’.

 

Step 2: Update pom.xml with maven dependencies.

 

pom.xml

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.sample.app</groupId>
	<artifactId>micronaut-environment-props-helloworld</artifactId>
	<version>1</version>

	<properties>
		<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
		<micronaut.version>3.7.3</micronaut.version>
		<slf4j.version>2.0.3</slf4j.version>
		<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>

		<maven.compiler.target>15</maven.compiler.target>
		<maven.compiler.source>15</maven.compiler.source>
	</properties>

	<dependencies>

		<dependency>
			<groupId>io.micronaut</groupId>
			<artifactId>micronaut-inject-java</artifactId>
			<version>${micronaut.version}</version>
		</dependency>
		<dependency>
			<groupId>io.micronaut</groupId>
			<artifactId>micronaut-runtime</artifactId>
			<version>${micronaut.version}</version>
		</dependency>

		<dependency>
			<groupId>io.micronaut</groupId>
			<artifactId>micronaut-validation</artifactId>
			<version>${micronaut.version}</version>
		</dependency>


		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>${slf4j.version}</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-simple</artifactId>
			<version>${slf4j.version}</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>${maven-compiler-plugin.version}</version>
				<configuration>
					<annotationProcessorPaths>
						<path>
							<groupId>io.micronaut</groupId>
							<artifactId>micronaut-validation</artifactId>
							<version>${micronaut.version}</version>
						</path>
					</annotationProcessorPaths>
				</configuration>
			</plugin>

			<plugin>
				<artifactId>maven-assembly-plugin</artifactId>
				<configuration>
					<archive>
						<manifest>
							<mainClass>com.sample.app.App</mainClass>
						</manifest>
					</archive>
					<descriptorRefs>
						<descriptorRef>jar-with-dependencies</descriptorRef>
					</descriptorRefs>
				</configuration>

				<executions>
					<execution>
						<id>make-assembly</id>
						<phase>package</phase>
						<goals>
							<goal>single</goal>
						</goals>
					</execution>
				</executions>
			</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
    
appName: chat server
version: 1.2.34

 

Step 4: Define application class.

 

App.java

 

package com.sample.app;

import io.micronaut.context.ApplicationContext;
import io.micronaut.context.env.Environment;

public class App {

	public static void main(String[] args) {
		try (ApplicationContext applicationContext = ApplicationContext.run()) {

			Environment environment = applicationContext.getEnvironment();
			
			String appName = environment.getProperty("appName", String.class, "demo chat server");
			String version = environment.getProperty("version", String.class, "1.0");
			String orgName = environment.getProperty("organization", String.class, "ABC Demo Corp");
			
			System.out.println("appName : " + appName);
			System.out.println("version : " + version);
			System.out.println("orgName : " + orgName);
		}

	}
}

 

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-environment-props-helloworld-1-jar-with-dependencies.jar’ in project target folder.

$ls ./target/
archive-tmp
classes
generated-sources
generated-test-sources
maven-archiver
maven-status
micronaut-environment-props-helloworld-1-jar-with-dependencies.jar
micronaut-environment-props-helloworld-1.jar
test-classes

Execute below command to run the application.

java -jar ./target/micronaut-environment-props-helloworld-1-jar-with-dependencies.jar

$java -jar ./target/micronaut-environment-props-helloworld-1-jar-with-dependencies.jar
appName : chat server
version : 1.2.34
orgName : ABC Demo Corp

You can download this application from this link.



Previous                                                    Next                                                    Home

No comments:

Post a Comment