我的 application.yml 文件中有以下属性:
payment-routing-config:
drivers:
- id: 'ANETC'
weight: 80
backups: ['EPAYC', 'NMIC', 'ANETD']
- id: 'EPAYC'
weight: 80
backups: ['ANETC', 'NMIC', 'ANETD']
- id: 'NMI'
weight: 80
backups: ['EPAYC', 'ANETC', 'ANETD']
以及以下 Java:
@ConfigurationProperties("paymentRoutingConfig")
public record PaymentRoutingConfig(
List<PaymentDriver> drivers
) { }
record PaymentDriver(
String id,
int weight,
List<String> backups
) { }
当我使用@Inject加载配置时,drivers属性是一个空列表。如果我将列表驱动程序更改为列表驱动程序,我将填充以下对象:
PaymentRoutingConfig[drivers=[{id=ANETC, weight=80, backups=[EPAYC, NMIC, ANETD]}, {id=EPAYC, weight=80, backups=[ANETC, NMIC, ANETD]}, {id=NMI, weight=80, backups=[EPAYC, ANETC, ANETD]}]]
这就是我将其注入控制器的方式:
@Inject
PaymentRoutingConfig paymentRoutingConfig;
复杂对象是否无法与@ConfigurationProperties一起使用?
我什至尝试将 List 简化为单个对象:
@ConfigurationProperties("paymentRoutingConfig")
public record PaymentRoutingConfig(
PaymentDriver driver
) { }
record PaymentDriver(
String id,
int weight
) { }
yaml:
payment-routing-config:
driver:
id: 'test'
weight: 10
我收到此错误:
Message: Failed to inject value for parameter [driver] of class: com.***.paymentservice.infrastructure.PaymentRoutingConfig
Message: Error resolving property value [payment-routing-config.driver]. Property doesn't exist
Path Taken: new PaymentController() --> PaymentController.paymentRoutingConfig --> new PaymentRoutingConfig([PaymentDriver driver])
Path Taken: new PaymentController() --> PaymentController.paymentRoutingConfig --> new PaymentRoutingConfig([PaymentDriver driver])
io.micronaut.context.exceptions.DependencyInjectionException: Failed to inject value for parameter [driver] of class: com.***.paymentservice.infrastructure.PaymentRoutingConfig
在我的 yaml 文件中使用以下内容,我能够正确填充配置对象:
payment-routing-config:
drivers: [
{id: 'test', weight: 10, backups: ['EPAYC', 'ANETC', 'ANETD']},
{id: 'test2', weight: 20, backups: ['ANETC', 'EPAYC', 'ANETD']}
]