In a springboot application, how can I read values form application.yml, if I need to use them in my code

Following is my application.yml file –

spring:
  mail:
    host: smtp.gmail.com
    port: 587

In one of my Java classes, I want to use the port number, which annotation I can use to achieve the same.

>Solution :

You can use the @Value annotation:

@Value("${spring.mail.port}")
private Integer port;

Leave a Reply