2

I am trying out a few things about YAML configuration, seems like I am missing something.

Below is my controller class and Yaml file.

HomeController.java

@Controller
@PropertySource("file:${user.dir}/config/webappdemo.yml")
public class HomeController {
    
    @Value("${app.message}")
    private String appMessage;

    @GetMapping("/home")
    @ResponseBody
    public String homePage() {
        return this.appMessage;
    }
}

webappdemo.yml

app:
   message: hello

My application failing to start, with the below exception:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'homeController': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'app.message' in value "${app.message}"
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:405) ~[spring-beans-5.3.9.jar:5.3.9]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1413) ~[spring-beans-5.3.9.jar:5.3.9]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:601) ~[spring-beans-5.3.9.jar:5.3.9]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.9.jar:5.3.9]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.9.jar:5.3.9]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.9.jar:5.3.9]

From the error message I can feel, because of some reason, It's not able to find that property If I modify the property file as below, Its working, Please help me identify the issue:

webappdemo.yml

app.message=hello

1 Answers1

4

PropertySource doesn't support .yml, you need to use .properties file in this case.

"YAML files cannot be loaded by using the @PropertySource or @TestPropertySource annotations. So, in the case that you need to load values that way, you need to use a properties file"

It From the official docs at 2.5.1. Mapping YAML to Properties

You might find solutions/workarounds in Spring @PropertySource using YAML

Manuel Jordan
  • 15,253
  • 21
  • 95
  • 158
Joker
  • 2,304
  • 25
  • 36
  • Thanks, I was going through one online tutorial where the guy uses application.yml, instead of application.properties and it works the same way, I thought its the same outcome for external properties as well – SRI HARSHA S V S Aug 22 '21 at 14:05
  • 1
    Yeah, sadly for PropertySource not. I usually use only application.yml and then use `@ConfigurationProperties` with a prefix to load specific configuration within that file. See https://www.baeldung.com/configuration-properties-in-spring-boot for a guide – Joker Aug 22 '21 at 14:10
  • Yes, @ConfigurationProperties is actually a very good utility to group a set of properties together, will make use of that, Thanks for your valuable insights. – SRI HARSHA S V S Aug 22 '21 at 14:18