Reputation: 457
Below is my code and configuration, but I can't get properties' value, always null.
application.properties
app.myProperty=1234
class AppProperties:
@Component
@Configuration
@ConfigurationProperties(prefix = "app")
public class AppProperties {
private String myProperty;
public String getMyProperty() {
return myProperty;
}
public void setMyProperty(String myProperty) {
this.myProperty = myProperty;
}
}
Controller:
@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));
}
}
}
log output:
property value:null
Upvotes: 0
Views: 9824
Reputation: 1863
Use @Value
annotation to read the values from application.properties
@Value("${myProperty}")
private String myProperty;
Upvotes: 2