对于最新的稳定版本,请使用 Spring Modulith 1.2.1

对于最新的稳定版本,请使用 Spring Modulith 1.2.1

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

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

  • Maven

  • Gradle

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

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

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

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

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

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

  • Java

  • Kotlin

@Configuration
class MyConfiguration {

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

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

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

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

spring.modulith.moments.enable-time-machine

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

spring.modulith.moments.granularity

小时

要触发的事件的最小粒度。避免每小时事件的替代值。days

spring.modulith.moments.locale

Locale.getDefault()

确定周边界时使用的 to。Locale

spring.modulith.moments.quarter-start-month

Months.JANUARY

季度开始的月份。

spring.modulith.moments.zone-id

ZoneOffset#UTC

用于确定附加到已发布事件的时间。ZoneId