下面是我的代码和配置,但我无法获取属性的值,总是空。
application.properties
app.myProperty = 1234
AppProperty类:
@Component
@Configuration
@ConfigurationProperties(prefix = "app")
public class AppProperties {
private String myProperty;
public String getMyProperty() {
return myProperty;
}
public void setMyProperty(String myProperty) {
this.myProperty = myProperty;
}
}
控制器:
@RestController
@Slf4j
public class TestController {
@Autowired
AppProperties appProperties;
@PostMapping(RoutePath.TEST)
public ResultVO test() {
try {
log.info("property value:" + appProperties.getMyProperty());
return ResultVOUtil.success(null);
} catch (Exception ex) {
return ResultVOUtil.error(CommonUtil.logExceptionError("发生错误。", null, ex));
}
}
}
日志输出:
属性值:空
答案 0 :(得分:0)
使用@Value
批注从application.properties读取值
@Value("$(myProperty)")
private String myProperty;