spring-bootjpaspring-data-jpajavers

Automatically add JaversSpringDataAuditable to all instances of JPARepository in Spring Boot


@Repository
@JaversSpringDataAuditable
public interface UserRepository extends JpaRepository<User, Integer> {

This works fine but I have a lot of repositories I need to annotate. Is there a way to configure Spring Boot so that all instances of @Repository will automatically use @JaversSpringDataAuditable?


Solution

  • I don't know about @JaversSpringDataAuditable, but you can solve your problem by taking advantage of how Spring Boot works and annotations.

    Here is my example code:

    @Target({ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Component
    public @interface Repository {
        @AliasFor(
            annotation = Component.class
        )
        String value() default "";
    }
    
    @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> {}
    

    You can also make the annotation name more beautiful (suitable for its functionality).

    I hope it will be of help. thank you