1

我有一个经过验证的配置属性类。

@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。在运行时,我有一个自动装配的 bean,但这个 bean 有空值而不是绑定配置。 笔记!如果我删除 @Validated 注释,我将拥有一个有效的 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);
    }
}
4

0 回答 0