0

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

danny
  • 427
  • 1
  • 15
  • 25

1 Answers1

2

Use @Value annotation to read the values from application.properties

@Value("${myProperty}")
private String myProperty;
Bishal Jaiswal
  • 1,684
  • 13
  • 15