我有以下两个类和 yaml-config:
第一类:
@ConfigurationProperties
@Validated ֎ 1 ֎
public class SplitterProps {
@Valid
public final ConnectionParams connectionParams;
// @Valid
// @NotNull
public final Subscriber subscriber;
public final List<@NotNull @Valid Converter> converters;
@ConstructorBinding
public SplitterProps(ConnectionParams connectionParams,
Map<String, Subscriber> subscribersList,
List<Converter> converters) {
this.connectionParams = connectionParams;
this.subscriber = subscribersList.isEmpty() ?
null :
subscribersList.values().iterator().next();
this.converters = converters;
}
...
}
YAML 配置:
...
subscribersList:
sub1:
kafkaBroker: xxx:9092
topicName: saveTopic
healthCheckTimeout: 10
producerTimeout: 10
...
第 2 类:
@Configuration(proxyBeanMethods = false)
public class KafkaConfig {
private final Subscriber subscriberProps;
private final Map<String, Object> producerProps;
public KafkaConfig(KafkaProperties kafkaProps,
SplitterProps appProps) {
this.subscriberProps = appProps.subscriber; ֎ 2 ֎
this.producerProps = getProducerProperties(kafkaProps, subscriberProps);
}
...
}
如果行中有@Validated注解 ֎ 1 ֎
然后appProps.subscriber == null in row ֎ 2 ֎. (NPE 出现)
如果行中没有@Validated注解 ֎ 1 ֎
然后appProps.subscriber != null in row ֎ 2 ֎. (按预期工作)
为什么会这样以及如何避免?