Thursday 27 April 2023

Micronaut: inject configuration property using @Value annotation

@Value annotation is used to allows configuration injection from the environment on a per property, field, method/constructor parameter basis.

 

Example 1

@Value("${appName}")
private String name;

Above snippet inject the value associated with environment property ‘appName’ to the variable name.

 

Example 2

@Value("${appLicence:apache}")
private String licence;

Above snippet inject the value associated with environment property ‘appLicence’ to the variable licence. If the environment property is not exist, then it inject the default value ‘licence’.

 

Example 3

Use backticks, if the default value has : in it.

@Value("${appHome:`https://my-chat-server.com`}")
private String home;

Example 4

@Value("Hi there!!!!!")
private String message;

Above snippet inject the static value "Hi there!!!!!" to the variable message.

 

Example 5

@Value("${appName} can send and receive messages. Version is ${appVersion}")
private String description;

 

Above snippet compose the injected values by combining static content and placeholders.

 

Find the below working application.

 

Step 1: Create new maven project ‘micronaut-value-annotation-demo’.

 

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-value-annotation-demo</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>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-inject-java</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
appName: chat server
appVersion: 1.23.45

 

Step 4: Define AppConfig class.

 

AppConfig.java

 

package com.sample.app.configs;

import io.micronaut.context.annotation.Value;
import jakarta.inject.Singleton;

@Singleton
public class AppConfig {

	@Value("${appName}")
	private String name;

	@Value("${appVersion}")
	private String version;

	@Value("${appLicence:apache}")
	private String licence;

	@Value("${appHome:`https://my-chat-server.com`}")
	private String home;

	@Value("${appName} can send and receive messages. Version is ${appVersion}")
	private String description;

	@Value("Hi there!!!!!")
	private String message;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	public String getLicence() {
		return licence;
	}

	public void setLicence(String licence) {
		this.licence = licence;
	}

	public String getHome() {
		return home;
	}

	public void setHome(String home) {
		this.home = home;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

}

 

Step 5: Define main application class.

 

App.java

 

package com.sample.app;

import com.sample.app.configs.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);

			String appName = appConfig.getName();
			String version = appConfig.getVersion();
			String licence = appConfig.getLicence();
			String home = appConfig.getHome();
			String description = appConfig.getDescription();
			String message = appConfig.getMessage();

			System.out.println("appName : " + appName);
			System.out.println("version : " + version);
			System.out.println("licence : " + licence);
			System.out.println("home : " + home);
			System.out.println("description : " + description);
			System.out.println("message : " + message);
		}

	}
}

 

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-value-annotation-demo-1-jar-with-dependencies.jar’ in project target folder.

$ ls ./target/
archive-tmp
classes
generated-sources
generated-test-sources
maven-archiver
maven-status
micronaut-value-annotation-demo-1-jar-with-dependencies.jar
micronaut-value-annotation-demo-1.jar
test-classes

Execute below command to run the application.

$ java -jar ./target/micronaut-value-annotation-demo-1-jar-with-dependencies.jar
appName : chat server
version : 1.23.45
licence : apache
home : https://my-chat-server.com
description : chat server can send and receive messages. Version is 1.23.45
message : Hi there!!!!!

You can download this application from this link.


  

Previous                                                    Next                                                    Home

No comments:

Post a Comment