spring-modulith-moments是 Passage of Time Events 的实现,深受 Matthias Verraes 博客文章的启发。 这是一种基于事件的时间方法,用于触发与已过去的特定时间段相关的操作。Spring中文文档

若要使用抽象,请在项目中包含以下依赖项:Spring中文文档

<dependency>
  <groupId>org.springframework.modulith</groupId>
  <artifactId>spring-modulith-moments</artifactId>
</dependency>
dependencies {
  implementation 'org.springframework.modulith:spring-modulith-moments'
}

添加到项目类路径的依赖项会导致应用程序中出现以下情况:Spring中文文档

  • 应用程序代码可以引用 Spring 事件侦听器中的 、 、 、 类型,以便在经过一定时间时收到通知。HourHasPassedDayHasPassedWeekHasPassedMonthHasPassedQuarterHasPassedYearHasPassedSpring中文文档

  • 在 中提供了 bean 类型,其中包含用于触发这些事件的逻辑。org.springframework.modulith.MomentsApplicationContextSpring中文文档

  • 如果设置为 ,则该实例将是一个允许“转移”时间并触发所有中间事件的实例,这对于由事件触发的集成测试功能非常有用。spring.modulith.moments.enable-time-machinetrueorg.springframework.modulith.TimeMachineSpring中文文档

默认情况下,Moments 使用实例。要自定义此项,请声明类型为 的 Bean 。Clock.systemUTC()ClockSpring中文文档

@Configuration
class MyConfiguration {

  @Bean
  Clock myCustomClock() {
    // Create a custom Clock here
  }
}
@Configuration
class MyConfiguration {

  @Bean
  fun myCustomClock(): Clock {
    // Create a custom Clock here
  }
}

Moments 公开以下应用程序属性以进行高级自定义:Spring中文文档

表 1.可用的应用程序属性
财产 默认值 描述

spring.modulith.moments.enable-time-machineSpring中文文档

Spring中文文档

如果设置为 ,则实例将为 ,该实例将公开 API 以将时间向前移动。对于期望由时间流逝事件触发的功能的集成测试非常有用。trueMomentsTimeMachineSpring中文文档

spring.modulith.moments.granularitySpring中文文档

小时Spring中文文档

要触发的事件的最小粒度。避免每小时事件的替代值。daysSpring中文文档

spring.modulith.moments.localeSpring中文文档

Locale.getDefault()Spring中文文档

确定周边界时使用的 to。LocaleSpring中文文档

spring.modulith.moments.quarter-start-monthSpring中文文档

Months.JANUARYSpring中文文档

季度开始的月份。Spring中文文档

spring.modulith.moments.zone-idSpring中文文档

ZoneOffset#UTCSpring中文文档

用于确定附加到已发布事件的时间。ZoneIdSpring中文文档