简体   繁体   中英

Spring not picking up value defined in properties file

I have a class with inject attributes like so:

@RestController
public class MyController {

    private final String myValue;

    @Autowired
    public MyController(
        @Value("${config.myValue}") String myValue) {
        this.myValue = myValue;
    }


    @PostMapping("/dostuff")
    public String dostuff(@RequestBody String message) {
      // stuff
    }
}

And then I have a config file defined inside src/main/resources/application.yml like so:

config:
  myValue: "some value"

But my IntelliJ (and cmd) tell me the value is not found:

 Could not resolve placeholder 'config.myValue' in value "${config.myValue}"

Why is my defined value not being recognized? (I tried renaming file to application-default.yml and application.properties but that didn't help either)

Could you please try renaming file as application.properties and define property like below and check if it works? config.myValue = some value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM