spring-data-commons icon indicating copy to clipboard operation
spring-data-commons copied to clipboard

Allow overriding DateTimeProvider via Bean Definition

Open ZIRAKrezovic opened this issue 1 year ago • 1 comments

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)

ZIRAKrezovic avatar Oct 15 '24 10:10 ZIRAKrezovic

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.

mp911de avatar Oct 16 '24 09:10 mp911de

hello ! Would it be okay if I try?

ghgh415263 avatar Sep 16 '25 04:09 ghgh415263

Sure. If you're using AI, please use AI to augment yourself and critically consider whether that what AI has produced would be of good quality that you as developer are proud of.

mp911de avatar Sep 16 '25 08:09 mp911de