spring boot version 2.2.7.RELEASE

I try to add a configuration file outside application.properties to configure the project. I found that in the configuration file, if it is a YAML file, I cannot get the value in this way

@Value ("$ {system.group3.name}")
public String group3;

That's right

@Value (value = "system.group3.name}")
public String group3;

But in the properties file, both methods are fine. What is the reason? Is YAML unable to support the first method?

bad :

package com.example.demo1;

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

@Component
@PropertySource(value = "classpath:demo.yml")
public class Config01 {

    // this function can't get value , But you can get the value in the *.properities file
    /** @see com.example.demo1.Config02 */
//    @Value("${system.group1.name}") // this bad, spring boot not start
//    public String group1;

    @Value(value = "system.group2.name")  // right
    public String group2;
}

right:

package com.example.demo1;

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

@Component
@PropertySource(value = "classpath:demo.properties")
public class Config02 {

    /** @see com.example.demo1.Config01 */  // right
    @Value("${system.group3.name}")
    public String group3;

    @Value(value = "system.group4.name") // right
    public String group4;
}

Sample program

maven-demo.zip

Thanks ~

Comment From: wilkinsona

As noted in the documentation, @PropertySource does not support YAML.

In the future, please ask this sort of question on Stack Overflow or Gitter. As mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements.

Comment From: wxw310415

谢谢您的回答!作为初学者很难找到文档中关于此类问题的说明,感谢您为我回答。