Thomas Schmidt
Thomas Schmidt

Reputation: 1378

Spring Boot: Load @Value from YAML file

I need to load a property from a .yml file, which contains the path to a folder where the application can read files from.

I'm using the following code to inject the property:

@Value("${files.upload.baseDir}")
private String pathToFileFolder;

The .yml file for development is located under src/main/resources/config/application.yml, im running the application with the following command in production, to override the development settings:

java -jar app.jar --spring.config.location=/path/to/application-production.yml

The Spring Boot documentation says:

SpringApplication will load properties from application.properties files in the following locations and add them to the Spring Environment:

  1. A /config subdirectory of the current directory.

  2. The current directory

  3. A classpath /config package

  4. The classpath root

As well as:

You can also use YAML ('.yml') files as an alternative to '.properties'.

The .yml file contains:

{...}
files:
      upload:
        baseDir: /Users/Thomas/Code/IdeaProjects/project1/files
{...}

And my Application class is annotated with:

@SpringBootApplication
@EnableCaching

When I run the application, i get an exception:

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'files.upload.baseDir' in string value "${files.upload.baseDir}"

Do I have to use the YamlPropertySourceLoader class or add a special annotation to enable the support for .yml in Spring Boot?

Edit: The .yml file contains some other properties, which get successfully loaded by Spring Boot like dataSource.XXXor hibernate.XXX.

Upvotes: 46

Views: 200762

Answers (8)

havryliuk
havryliuk

Reputation: 311

My properties file was mistakenly named applcation.properties as it was auto-generated by the Spring initializer. But I added the properties there in the .yml format and they were not retrieved with the same error.

When I renamed the file to application.yml, it started working.

Upvotes: 0

Marcos Ale
Marcos Ale

Reputation: 1

For those who have problems with a @RestController, I do it as follows:

@Autowired
@Value("${google.recaptcha}") 
private String keyRecaptcha;

Upvotes: 0

harun ugur
harun ugur

Reputation: 1852

In yml properties file :

xxxx:
     page:
        rowSize: 1000

Create your Yaml properties config class :

@Configuration
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "xxxx")
public class YmlPropertiesConfig {

    private Page page;

    public Page getPage() {
        return page;
    }
    public void setPage(Page page) {
        this.page = page;
    }

    public class Page {
        private Integer rowSize;

        public Integer getRowSize() {
            return rowSize;
        }

        public void setRowSize(Integer rowSize) {
            this.rowSize = rowSize;
        }
    }    
}

Finally get it and use it :

public class XXXXController {

     @Autowired
     private YmlPropertiesConfig ymlProperties;

     public String getIt(){

        Integer pageRowSize = ymlProperties.getPage().getRowSize();
     }
}

Upvotes: 3

dev_fondue
dev_fondue

Reputation: 121

I've got that issue Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder cause I've set test spring boot profile in properties.yaml. Spring can't find properties for test profile when run app with no profile.

So remove spring boot profile from properties or yaml or run app with enabled profile.

Configuration file example is below:

@Configuration
public class AppConfig {
  @Value("${prop.foo}")
  private String foo;
  @Value("${prop.bar}")
  private String bar;

  @Bean
  BeanExample beanExample() {
    return new BeanExample(foo, bar);
  }
}

Upvotes: 0

Kjeld
Kjeld

Reputation: 470

I found the above wasn't working for me, because I tried to access the variable in a constructor. But at construction, the value is not injected yet. Eventually I got it to work using this workaround: https://mrhaki.blogspot.com/2015/04/spring-sweets-using-value-for.html

Maybe this is helpful to others.

Upvotes: 7

Nick Borges
Nick Borges

Reputation: 481

For example: application.yml

key:
 name: description here

Your Class:

@Value("${key.name}")
private String abc;

Upvotes: 19

SatyaRajC
SatyaRajC

Reputation: 51

For me a duplicate key in the property file caused this...

I used same key unknowingly in large yml file.

key:   
 key1: value
 key2: value

key:  
 key3: value

Upvotes: 2

Thomas Schmidt
Thomas Schmidt

Reputation: 1378

M. Deinum is right, the setup i've provided is working - the yml file was indented wrong, so the property couldn't be found.

Upvotes: 14

Related Questions