ฉันยังใหม่กับ Spring Boot ขณะนี้ ฉันกำลังพยายามสร้างคลาส POJO (SystemProperties.class) เพื่ออ่านค่าในไฟล์คุณสมบัติ (parameter.properties แยกจาก application.properties แต่ยังอยู่ภายใต้ ไดเรกทอรีเดียวกัน /src/main/resources ปัญหาเกิดขึ้นเมื่อฉันใช้ @ConstructorBinding ในคลาสเพื่อที่จะไม่เปลี่ยนรูป
- @ConstructorBinding จำเป็นต้องใช้กับ @EnableConfigurationProperties หรือ @ConfigurationPropertiesScan
- @ConfigurationPropertiesScan จะเพิกเฉยต่อคำอธิบายประกอบ @Configuration ซึ่งจำเป็นเมื่อใช้ @PropertySource เพื่อระบุไฟล์
*.properties ภายนอก
A) SystemProperties.class
@Configuration
@PropertySource("classpath:parameter.properties")
@ConstructorBinding
@ConfigurationProperties(prefix = "abc")
public class SystemProperties {
private final String test;
public SystemProperties (
String test) {
this.test = test;
}
public String getTest() {
return test;
}
B) parameter.properties
abc.test=text1
ฉันได้ลองลบคำอธิบายประกอบ @PropertySource แล้ว แต่ไม่สามารถดึงค่าออกมาได้ เว้นแต่ว่ามาจาก application.properties ความช่วยเหลือใด ๆ ที่ชื่นชมอย่างมาก!