我有一个经过验证的配置属性类。
@Data
@Configuration
@ConfigurationProperties(prefix = "notifications")
@Validated
public class NotificationConfig {
@NotNull
@Valid
private NotificationMessageConfig message = new NotificationMessageConfig();
@NotNull
@Valid
private SmsConfig sms = new SmsConfig();
@NotNull
@Valid
private EmailConfig email = new EmailConfig();
@NotNull
@Valid
private NotificationTimerConfig timer = new NotificationTimerConfig();
}
在集成测试中,我尝试自动连接类的bean。在运行时,我有一个autowiredbean,但是这个bean有空值而不是绑定配置。注意!如果我删除@validated annotation,我将拥有一个有效的bean,但是配置将不会被验证。
@Slf4j
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@RequiredArgsConstructor
public class NotificationsIT extends BaseIntegrationTest {
@Autowired
private Notifications notification;
@Autowired
private Banks banks;
@Autowired
private NotificationConfig notificationConfig;
@BeforeEach
public void init() {
super.init();
// notificationConfig autowired but fields have null values
defaultNotificationConfig = clone(notificationConfig);
}
}
暂无答案!
目前还没有任何答案,快来回答吧!