Java 如何从spring启动项目的application.properties文件中获取属性值?

Java 如何从spring启动项目的application.properties文件中获取属性值?,java,spring-boot,spring-mvc,Java,Spring Boot,Spring Mvc,下面是我的代码和配置,但我无法获取属性的值,始终为null 应用程序属性 @Value("${myProperty}") private String myProperty; app.myProperty=1234 类AppProperties: @Component @Configuration @ConfigurationProperties(prefix = "app") public class AppProperties { private String myProper

下面是我的代码和配置,但我无法获取属性的值,始终为null


应用程序属性

@Value("${myProperty}")
private String myProperty;
app.myProperty=1234

类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;
    }
}
控制器:

@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));
        }
    }

}
日志输出:

属性值:null


使用
@Value
注释从application.properties读取值

@Value("${myProperty}")
private String myProperty;

使用
@Value
注释从application.properties读取值

@Value("${myProperty}")
private String myProperty;

AppProperties
上删除
@组件
,您不需要它,也可以显示包的副本structure@Value上的(${app.myProperty})int myProperty用户@Configuration@EnableConfigurationProperties({AppProperties.class})在
AppProperties
上删除
@组件
,您不需要它,也可以显示包的副本structure@Value上的(${app.myProperty})int myProperty用户@Configuration@EnableConfigurationProperties({AppProperties.class})我认为它应该包含{…}个字符:@Value(${myProperty})我认为它应该有{…}个字符:@Value(${myProperty})