Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I read application properties in Micronaut?

Tags:

micronaut

I integrated AWS SES API to my Micronaut Groovy application using guide send mail in micronaut and I am able send mails if I directly assign values to properties.

I want to make it config driven hence have been trying to find ways to achieve that.

I tried @Value annotation as mentioned in guide but was not able to make it work.

@Value("aws.secretkeyid")
String keyId

Further digging into documentation revealed that Micronaut has its own annotation for injecting properties in variables.

@Property(name="aws.secretkeyid")
String keyId

But nothing seems to work, my variables are still null.

What could be possibly wrong here ?

For reference, following is in my application.yml file

aws:
  keyid: "2weadasdwda"
  secretkeyid: "abcdesdasdsddddd"
  region: "us-east-1"
like image 573
Aditya T Avatar asked Nov 22 '18 17:11

Aditya T


People also ask

How do I get spring boot properties?

Step 1 − After creating an executable JAR file, run it by using the command java –jar <JARFILE>. Step 2 − Use the command given in the screenshot given below to change the port number for Spring Boot application by using command line properties.

How does a Micronaut work?

How does Micronaut work? Micronaut is designed to function as both a client and server framework. The framework features an annotation-based programming model that is very similar to the Java Spring framework. Unlike the Spring framework, however, Micronaut does not use Java Reflection APIs.


1 Answers

You are using it incorrectly, you are injecting the literal value aws.secretkeyid, not the value of a variable.

The correct syntax is (Groovy):

@Value('${aws.secretkeyid}')
String keyId

Notice that you must use single quotes to avoid Groovy to attempt interpolation

Java:

@Value("${aws.secretkeyid}")
String keyId;

Kotlin:

@Value("\${aws.secretkeyid}")
keyId: String

Notice that you must use a backslash to escape the dollar sign to avoid Kotlin string templates

like image 147
Álvaro Sánchez-Mariscal Avatar answered Sep 27 '22 19:09

Álvaro Sánchez-Mariscal