In this article, we will find something out about reading properties in file with Spring framework. Let’s get started.

Table of contents

How to use @ConfigurationProperties annotation

We have the content of properties file.

    org.springframework.web: ERROR
    com.mkyong: DEBUG
thread-pool: 10
    - title: Home
      name: Home
      path: /
    - title: Login
      name: Login
      path: /login
    timeout: 5
    output-folder: /temp/
  error: /error/
@ConfigurationProperties("app") // prefix app, find app.* values
public class AppProperties {
    private String error;
    private List<Menu> menus = new ArrayList<>();
    private Compiler compiler = new Compiler();

    public static class Menu {
        private String name;
        private String path;
        private String title;

        //getters and setters

        public String toString() {
            return "Menu{" +
                    "name='" + name + '\'' +
                    ", path='" + path + '\'' +
                    ", title='" + title + '\'' +

    public static class Compiler {
        private String timeout;
        private String outputFolder;

        //getters and setters

        public String toString() {
            return "Compiler{" +
                    "timeout='" + timeout + '\'' +
                    ", outputFolder='" + outputFolder + '\'' +

    //getters and setters

How to use @PropertySource with @Value

@PropertySource(value = "classpath:/", ignoreResourceNotFound = true)
public class AuthenticationController {
	private String expirationTime;

    // ...

Combine settings into object

  1. Use @ConfigurationProperties annotation

    By default, file is read from Spring Boot. If we want to specify another properties file, we can use @PropertySource annotation.

     @PropertySource(value = "", ignoreResourceNotFound = true)
     public class AppConfig {
         private String username;
         private String password;
  2. Use @Value annotation

     @PropertySource(value = "", ignoreResourceNotFound = true)
     public class AppConfig {
         private String username;
         private String password;

Use Properties class

If we do not want to use annotation to read properties file, we can use Properties class to read file directly. But it’s not recommeded way to deal with it.

public static void main(String args[]) throws IOException {
    Properties prop = readPropertiesFile("");
    System.out.println("username: "+ prop.getProperty("username"));
    System.out.println("password: "+ prop.getProperty("password"));

public static Properties readPropertiesFile(String fileName) throws IOException {
    FileInputStream fis = null;
    Properties prop = null;

    try {
        fis = new FileInputStream(fileName);
        prop = new Properties();
    } catch(FileNotFoundException fnfe) {
    } catch(IOException ioe) {
    } finally {

    return prop;

The order that Spring boot setup for properties

When using Spring Boot the properties are loaded in the following order (see Externalized Configuration in the Spring Boot reference guide).

  1. Command line arguments.

  2. Java System properties (System.getProperties()).

  3. OS environment variables.

  4. JNDI attributes from java:comp/env

  5. A RandomValuePropertySource that only has properties in random.*.

  6. Application properties outside of your packaged jar (application. properties including YAML and profile variants).

  7. Application properties packaged inside your jar ( including YAML and profile variants).

  8. @PropertySource annotations on your @Configuration classes.

  9. Default properties (specified using SpringApplication.setDefaultProperties).

When resolving properties (i.e. @Value(“${myprop}”) resolving is done in the reverse order (so starting with 9).

To add different files you can use the spring.config.location properties which takes a comma separated list of property files or file location (directories).


The one above will add a directory which will be consulted for files.,

This will add the 2 properties file to the files that are loaded.

For example:

java -jar target/myapp.jar --spring.config.location=classpath:file:///C:/Apps/springtest/,classpath:file:///C:/Apps/springtest/

The default configuration files and locations are loaded before the additonally specified spring.config.location ones meaning that the latter will always override properties set in the earlier ones. (See also this section of the Spring Boot Reference Guide).

If spring.config.location contains directories (as opposed to files) they should end in / (and will be appended with the names generated from before being loaded). The default search path classpath:,classpath:/config,file:,file:config/ is always used, irrespective of the value of spring.config.location. In that way you can set up default values for your application in (or whatever other basename you choose with and override it at runtime with a different file, keeping the defaults.

The other way to add multiple properties files, we can use PropertyPlaceHolderConfigurer class

public class PropertiesConfiguration {
    public PropertyPlaceholderConfigurer properties() {
        final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
        // ppc.setIgnoreUnresolvablePlaceholders(true);

        final List<Resource> resourceLst = new ArrayList<Resource>();

        resourceLst.add(new ClassPathResource(""));
        resourceLst.add(new FileSystemResource("/etc/myapp/overriding.propertie"));
        resourceLst.add(new ClassPathResource(""));
        resourceLst.add(new ClassPathResource("")); // for Developer debugging.

        ppc.setLocations(resourceLst.toArray(new Resource[]{}));

        return ppc;

Wrapping up

  • Understanding about how to use @ConfigurationProperties, @PropertySource, and @Value.

  • @ConfigurationProperties supports both .properties and .yml file.