【发布时间】:2018-08-30 00:32:00
【问题描述】:
我正在使用 Spring Boot 编写 REST 服务。
我的 rest 服务中的方法调用了一个 util 类,这个 util 类需要引用 application.properties 中定义的某些属性
我使用@Value,但它在 util 类中不起作用,而它在我的 REST 服务类中起作用。
我的 REST 服务:ReportsController.java
@RestController
@RequestMapping("/api/v1")
public class ReportsController{
@Value("${report.path}")
private String reportPath;
@GetMapping
@RequestMapping("/welcome")
public String retrieveWelcomeMessage() {
return new ExcelFileUtil().test();
}
@GetMapping
@RequestMapping("/welcome1")
public String retrieveWelcomeMessage() {
return reportPath;
}
}
我的 Utils 类:MyUtil.java
public class MyUtil{
@Value("${report.path}")
private String reportPath;
public String test()
{
return reportPath;
}
}
我从 application.properties 中获取值是为 http://localhost:8080/api/v1/welcome1 打印的 但是http://localhost:8080/api/v1/welcome 的空白
如何使 MyUtil.java 中的 application.properties 可读?
【问题讨论】:
标签: spring rest spring-boot