Java 最终字段的配置属性不为';行不通
我需要将Java 最终字段的配置属性不为';行不通,java,spring,spring-boot,Java,Spring,Spring Boot,我需要将application.yml文件中的数据设置到我的配置类中,但当我尝试这样做时,我得到一个错误: TestConfig is annotated with @ConstructorBinding but it is defined as a regular bean which caused dependency injection to fail. 我的application.yml文件如下所示: test: app: id: app_id 我的TestConfig
application.yml
文件中的数据设置到我的配置类中,但当我尝试这样做时,我得到一个错误:
TestConfig is annotated with @ConstructorBinding but it is defined as a regular bean which caused dependency injection to fail.
我的application.yml
文件如下所示:
test:
app:
id: app_id
我的TestConfig
类如下所示:
@Configuration
@ConfigurationProperties(prefix = "test.app")
@ConstructorBinding
public class TestConfig {
private final String id;
public TestConfig(String id) {
this.id = id;
}
}
我试着做喜欢的事,但对我来说不起作用。我错在哪里?根据: 您需要从TestConfig.class中删除@Configuration 此外,需要强调的是,要使用构造函数绑定,我们需要使用@EnableConfigurationProperties或@ConfigurationPropertiesScan显式启用配置类 ---------编辑-----
你能给我举个例子吗?我读了这篇文章,但结果是一样的。我没有从文件中获取数据。谢谢你的帮助@约翰很高兴这对你有用!
@ConfigurationProperties(prefix = "test.app")
@ConstructorBinding
public class TestConfig {
private final int id;
public TestConfig (int id)
this.id = id
}
public String getId() {
return id;
}
}
@SpringBootApplication
@ConfigurationPropertiesScan
public class YourApp{
public static void main(String[] args) {
SpringApplication.run(YourApp.class, args);
}
}