6

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"
Aditya T
  • 1,110
  • 10
  • 21

2 Answers2

10

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

debuglevel
  • 144
  • 7
  • 1
    The syntax you mentioned is for Java, not for Groovy. I tried it first thing as it was in [send mail in micronaut] documentation, but code does not even compile if you use above syntax. – Aditya T Nov 23 '18 at 11:05
  • 1
    The syntax I posted is for Groovy and does compile indeed. Notice that you must use single quotes to avoid Groovy to attempt interpolation. – Álvaro Sánchez-Mariscal Nov 23 '18 at 11:10
  • 1
    Indeed you are right, I made the mistake of using double quotes instead of single quotes. Although due to this silly mistake at my end, I ended up upgrading Micronaut version to 1.0.1 and used @Property(name = "aws.secretkeyid") :D – Aditya T Nov 23 '18 at 18:06
  • I was going to answer it myself but your answer is correct as per the question. :) – Aditya T Nov 23 '18 at 18:07
6

If anyone else stumbles upon this problem, you also have alternative to use @Property annotation in Micronaut ( starting from version 1.0.1 )

Syntax is as follows

@Property(name = "your.application.property")
String propertyName

PS : This is what was mentioned in Micronaut Documentation but was not working in my case as I was on Micronaut Version 1.0.0

Aditya T
  • 1,110
  • 10
  • 21
  • you need to add property in `application.properties` file. based on this link http://mrhaki.blogspot.com/2018/10/micronaut-mastery-configuration.html – sfgroups Jan 10 '19 at 03:42