We can read properties file in spring boot using 3 way
1. Read value from application.properties Using @Value
map key as
public class EmailService {
@Value("${email.username}")
private String username;
}
2. Read value from application.properties Using @ConfigurationProperties
In this we will map prefix of key using ConfigurationProperties and key name is same as field of class
@Component
@ConfigurationProperties("email")
public class EmailConfig {
private String username;
}
3. Read application.properties Using using Environment object
public class EmailController {
@Autowired
private Environment env;
@GetMapping("/sendmail")
public void sendMail(){
System.out.println("reading value from application properties file using Environment ");
System.out.println("username ="+ env.getProperty("email.username"));
System.out.println("pwd ="+ env.getProperty("email.pwd"));
}
Reference : how to read value from application.properties in spring boot