对于最新的稳定版本,请使用 Spring Modulith 1.3.0spring-doc.cadn.net.cn

Moments — 时间流逝事件 API

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

要使用抽象,请在项目中包括以下依赖项:spring-doc.cadn.net.cn

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

添加到项目 Classpath 的依赖项会导致应用程序中出现以下情况:spring-doc.cadn.net.cn

  • 应用程序代码可以引用HourHasPassed,DayHasPassed,WeekHasPassed,MonthHasPassed,QuarterHasPassed,YearHasPassed键入 Spring 事件侦听器,以便在经过一定时间时收到通知。spring-doc.cadn.net.cn

  • 类型org.springframework.modulith.MomentsApplicationContext,其中包含触发这些事件的逻辑。spring-doc.cadn.net.cn

  • 如果spring.modulith.moments.enable-time-machine设置为true,则该实例将为org.springframework.modulith.TimeMachine它允许 “shift” 时间并触发所有中间事件,这对于由事件触发的集成测试功能很有用。spring-doc.cadn.net.cn

默认情况下,Moments 使用Clock.systemUTC()实例。要自定义此 Bean,请声明一个Clock.spring-doc.cadn.net.cn

@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-doc.cadn.net.cn

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

spring.modulith.moments.enable-time-machinespring-doc.cadn.net.cn

spring-doc.cadn.net.cn

如果设置为trueMomentsinstance 将是一个TimeMachine,这会公开 API 以向前移动时间。对于需要由 Passed of Time Events 触发的功能的集成测试非常有用。spring-doc.cadn.net.cn

spring.modulith.moments.granularityspring-doc.cadn.net.cn

小时spring-doc.cadn.net.cn

要触发的事件的最小粒度。替代值days以避免每小时事件。spring-doc.cadn.net.cn

spring.modulith.moments.localespring-doc.cadn.net.cn

Locale.getDefault()spring-doc.cadn.net.cn

Locale在确定周边界时使用。spring-doc.cadn.net.cn

spring.modulith.moments.quarter-start-monthspring-doc.cadn.net.cn

Months.JANUARYspring-doc.cadn.net.cn

季度开始的月份。spring-doc.cadn.net.cn

spring.modulith.moments.zone-idspring-doc.cadn.net.cn

ZoneOffset#UTCspring-doc.cadn.net.cn

ZoneId以确定附加到事件发布时间。spring-doc.cadn.net.cn