Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Micronaut: read list of values from application properties file

You can specify the lists and arrays in application property file and read them as a list.

 

Step 1: Define the collection of values in application.yml file.

my:
  app:
    primes:
      - 2
      - 3
      - 5
      - 7
      - 11

 

Step 2: Read the primes using ConfigurationProperties annotation.

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

	private List primes;

	public ListgetPrimes() {
		return primes;
	}

	public void setPrimes(List primes) {
		this.primes = primes;
	}

}

 

Find the below working application.

 

Step 1: Create new maven project ‘micronaut-convert-to-list-or-array’.

 

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.0modelVersion>
	groupId>com.sample.appgroupId>
	artifactId>micronaut-convert-to-list-or-arrayartifactId>
	version>1version>

	parent>
		groupId>io.micronautgroupId>
		artifactId>micronaut-parentartifactId>
		version>3.7.3version>
	parent>


	properties>
		slf4j.version>2.0.3slf4j.version>
		maven-compiler-plugin.version>3.8.1maven-compiler-plugin.version>

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

	dependencies>

		dependency>
			groupId>io.micronautgroupId>
			artifactId>micronaut-injectartifactId>
			scope>compilescope>
		dependency>


		dependency>
			groupId>io.micronautgroupId>
			artifactId>micronaut-inject-javaartifactId>
		dependency>

		dependency>
			groupId>io.micronautgroupId>
			artifactId>micronaut-runtimeartifactId>
		dependency>


		dependency>
			groupId>org.slf4jgroupId>
			artifactId>slf4j-apiartifactId>
			version>${slf4j.version}version>
		dependency>
		dependency>
			groupId>org.slf4jgroupId>
			artifactId>slf4j-simpleartifactId>
			version>${slf4j.version}version>
		dependency>

	dependencies>

	build>
		plugins>
			plugin>
				groupId>org.apache.maven.pluginsgroupId>
				artifactId>maven-compiler-pluginartifactId>
				version>${maven-compiler-plugin.version}version>
				configuration>
					annotationProcessorPaths>
						path>
							groupId>io.micronautgroupId>
							artifactId>micronaut-inject-javaartifactId>
						path>
					annotationProcessorPaths>
				configuration>
			plugin>

			plugin>
				artifactId>maven-assembly-pluginartifactId>
				configuration>
					archive>
						manifest>
							mainClass>com.sample.app.AppmainClass>
						manifest>
					archive>
					descriptorRefs>
						descriptorRef>jar-with-dependenciesdescriptorRef>
					descriptorRefs>
				configuration>

				executions>
					execution>
						id>make-assemblyid>
						phase>packagephase>
						goals>
							goal>singlegoal>
						goals>
					execution>
				executions>
			plugin>


		plugins>
	build>
project>

 

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

 

application.yml

my:
  app:
    primes:
      - 2
      - 3
      - 5
      - 7
      - 11

 

Step 4: Define AppConfig class.

 

AppConfig.java

package com.sample.app.configuration;

import java.util.List;

import io.micronaut.context.annotation.ConfigurationProperties;

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

	private List primes;

	public ListgetPrimes() {
		return primes;
	}

	public void setPrimes(List primes) {
		this.primes = primes;
	}

}

 

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(appConfig.getPrimes());

		}

	}
}

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-convert-to-list-or-array-1-jar-with-dependencies.jar’ in project target folder.

 

$ls ./target/
archive-tmp
classes
generated-sources
generated-test-sources
maven-archiver
maven-status
micronaut-convert-to-list-or-array-1-jar-with-dependencies.jar
micronaut-convert-to-list-or-array-1.jar
test-classes

Execute below command to run the application.


java -jar ./target/micronaut-convert-to-list-or-array-1-jar-with-dependencies.jar

$java -jar ./target/micronaut-convert-to-list-or-array-1-jar-with-dependencies.jar
[2, 3, 5, 7, 11]

 

You can download this application from this link.


 

How to specify the list of values in java properties file?

Specify list of values as comma separated.

 

primes=2,3,5,7,11

 


Previous                                                    Next                                                    Home


This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

Share the post

Micronaut: read list of values from application properties file

×

Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×