在Spring Boot中,可以使用@Value注解来读取配置文件中的值。具体步骤如下:
myapp.name=My Application
myapp.version=1.0
@Component
public class MyAppProperties {
@Value("${myapp.name}")
private String name;
@Value("${myapp.version}")
private String version;
// 省略getter和setter方法
}
@RestController
public class MyController {
@Autowired
private MyAppProperties myAppProperties;
@GetMapping("/info")
public String getAppInfo() {
String info = "Name: " + myAppProperties.getName() + ", Version: " + myAppProperties.getVersion();
return info;
}
}
通过以上步骤,就可以在Spring Boot中读取配置文件中的属性值了。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
相关推荐:springboot如何读取配置文件的值