Main Tutorials

How to access values from application.properties in Spring Boot

spring boot access example

In Spring Boot, we can use @Value annotation to access the values defined in the application.properties from Spring-managed beans.

application.properties

#Logging
logging.level.org.springframework.web=ERROR
logging.level.com.mkyong=DEBUG

email.smtp.server=smtp.gmail.com

  import org.springframework.beans.factory.annotation.Value;

  @Value("${email.smtp.server}")
  private String server;

Table of contents:

P.S. Tested with Spring Boot 3.1.2

1. application.properties

Spring default loads the application.properties file from the root classpath. Later, we will show a few ways to access the values defined in the below application.properties.

application.properties

#Logging
logging.level.org.springframework.web=ERROR
logging.level.com.mkyong=DEBUG

#email server
email.smtp.server=smtp.gmail.com
email.smtp.port=467
[email protected]

Here’s the project structure for reference.

project structure

1. @Value

In Spring managed beans, we can use the @Value annotation to access the values defined in the application.properties.

EmailServer.java

package com.mkyong.global;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class EmailServer {

    // injects smtp.gmail.com
    @Value("${email.smtp.server}")
    private String server;

    // injects 467
    @Value("${email.smtp.port}")
    private Integer port;

    // injects [email protected]
    @Value("${email.smtp.username}")
    private String username;

    //setters, getters
}

Below is a REST controller to return the above EmailServer object in JSON format.

MainController.java

package com.mkyong;

import com.mkyong.global.EmailServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MainController {

  @Autowired
  private EmailServer email;

  @GetMapping("/email")
  public EmailServer getEmail() {
      return email;
  }


}

2. Environment

Alternatively, we can also auto-wire the org.springframework.core.env.Environment bean and access the values defined in the application.properties.

EnvironmentController.java

package com.mkyong;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class EnvironmentController {

    @Autowired
    private Environment env;

    @GetMapping("/env")
    public Map<String, String> getEmailFromEnvironment() {

        String server = env.getProperty("email.smtp.server");
        String username = env.getProperty("email.smtp.username");
        String port = env.getProperty("email.smtp.port");

        Map<String, String> map = new HashMap<>();
        map.put("server", server);
        map.put("username", username);
        map.put("port", port);

        return map;
    }

}

3. @ConfigurationProperties

The @ConfigurationProperties bind values from application.properties to a Java object.

EmailServer2.java

package com.mkyong.global;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties("email.smtp") // prefix
public class EmailServer2 {

  private String server;    // injects smtp.gmail.com

  private Integer port;     // injects 467

  private String username;  // injects [email protected]

  //getters, setters
}

4. Download Source Code

$ git clone https://github.com/mkyong/spring-boot.git

$ cd spring-boot-externalize-config-2

$ mvn test

$ mvn spring-boot:run

$ curl localhost:8080/email

$ curl localhost:8080/email2

$ curl localhost:8080/env

5. References

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
1 Comment
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Chuck
1 month ago

Can you please share if there is a way to iterate through all properties defined in application.properties, thank you