spring-projects / spring-data-commons

Spring Data Commons. Interfaces and code shared between the various datastore specific implementations.
https://spring.io/projects/spring-data
Apache License 2.0
773 stars 672 forks source link

Allow overriding DateTimeProvider via Bean Definition #3177

Open ZIRAKrezovic opened 5 days ago

ZIRAKrezovic commented 5 days ago

The default DateTimeProvider is set to CurrentDateTimeProvider.INSTANCE which returns LocalDateTime.now().

We have to override it when using OffsetDateTime as JPA will throw an error.

According to https://docs.spring.io/spring-data/jpa/reference/auditing.html#jpa.auditing.configuration it is already possible to override AuditorAware by simply defining a bean.

However, the same is not true for DateTimeProvider. I have tried defining a bean of type DateTimeProvider, but it was not picked up. I was only able to specify it via @EnableJpaAuditing(dateTimeProviderRef = "...")

(note, I used EnableJpaAuditing, but AuditingHandlerSupport is part of spring-data-commons)

mp911de commented 4 days ago

AuditingBeanDefinitionRegistrarSupport enables autowire by type if auditorAwareRef isn't set. I think we should be able to generally set autowrite mode to AUTOWIRE_BY_TYPE and back off from setting dateTimeProvider to CurrentDateTimeProvider.INSTANCE if no reference is configured as AuditingHandlerSupport initializes itself with CurrentDateTimeProvider.INSTANCE.