Joh*_*ohn 3 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.
Run Code Online (Sandbox Code Playgroud)
我的application.yml文件如下所示:
test:
app:
id: app_id
Run Code Online (Sandbox Code Playgroud)
我的TestConfig班级是这样的:
@Configuration
@ConfigurationProperties(prefix = "test.app")
@ConstructorBinding
public class TestConfig {
private final String id;
public TestConfig(String id) {
this.id = id;
}
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试这样做,但它对我不起作用。
我哪里错了?
根据 :
您需要从 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);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8654 次 |
| 最近记录: |