自动将 JaversSpringDataAuditable 添加到 Spring Boot 中 JPARepository 的所有实例中

问题描述 投票:0回答:1
@Repository
@JaversSpringDataAuditable
public interface UserRepository extends JpaRepository<User, Integer> {

这工作正常,但我有很多存储库需要注释。有没有办法配置 Spring Boot,以便

@Repository
的所有实例都会自动使用
@JaversSpringDataAuditable

spring-boot jpa spring-data-jpa javers
1个回答
0
投票

我不知道 @JaversSpringDataAuditable,但是您可以通过利用Spring Boot如何工作注释来解决您的问题。

  • 解决方案:创建并使用具有 @Repository 和 @JaversSpringDataAuditable 的自定义注释。

这是我的示例代码:

  • 首先,查看@Repository注解内部,如下所示。
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Repository {
    @AliasFor(
        annotation = Component.class
    )
    String value() default "";
}
  • 创建与注释相同但名称不同的自定义注释。
  • 现在添加我们想要的@JaversSpringDataAuditable。
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
@JaversSpringDataAuditable //<- Add
public @interface CustomRepository {
    @AliasFor(
            annotation = Component.class
    )
    String value() default "";
}
  • 最后使用如下
@CustomRepository
public interface UserRepository extends JpaRepository<User, Integer> {}

您还可以使注释名称更美观(适合其功能)。

希望对您有所帮助。谢谢你

© www.soinside.com 2019 - 2024. All rights reserved.