Home > AI > Backend > SpringBoot >

@Value

Method 1, use Autowired to get the value

@Data
@Service
@PropertySource("classpath:foo.properties")
class PropertyComponent {

    // inject a string
    @Value("you can define string here directly")
    public String stringValue;

    // get the value from the file
    @Value("${value.from.file}")
    public String valueFromFile;
    
    // default for undefined
    @Value("${unknown.param:some default}")
    public String someDefault;

    // list
    @Value("${listOfValues}")
    public String[] valuesArray;

}


@RestController
class PropertyController {
    @Autowired
    PropertyComponent propertyComponent;

    @GetMapping("/ok")
    public PropertyComponent ok() {
        return propertyComponent;
    }
}

Leave a Reply