批注接口 TargetAnnotation


@Target(TYPE) public @interface TargetAnnotation
Jimmer supports compile-time transaction management AOP implementation. If a class or its methods are annotated with Tx, Jimmer's APT/KSP will automatically generate a derived class for it.

This annotation includes a parameter of another annotation type, and that annotation type represented by this parameter should support parameterless usage. This method allows specifying an annotation for the automatically generated derived class, rather than the current class. For example:

User writes code like this:


 @TargetAnnotation(Component.class)
 @Tx(Propgation.REQUIRED)
 public class BookRepository {

     final JSqlClient sqlClient;

     public BookRepository(JSqlClient sqlClient) {
         this.sqlClient = sqlClient;
     }

     public List<Book> findBooks() {
         ... ...
     }

     @Tx(Propgation.MANDATORY)
     public void saveBook(BookInput input) {
         ... ...
     }
 }
 

After compilation, a derived class will be generated automatically:


 @Component
 public class BookRepositoryTx extends BookRepository {

     public BookRepository(JSqlClient sqlClient) {
         super(sqlClient);
     }

     @Override
     public List<Book> findBooks() {
         return sqlClient.transaction(Propagation.REQUIRED, () -> {
             return super.findBooks();
         });
     }

     @Override
     public void saveBook(BookInput input) {
         sqlClient.transaction(Propagation.MANDATORY, () -> {
             super.saveBook(input);
             return null;
         });
     }
 }
 

As we can see, the automatically generated derived class will be annotated by @Component, but the current class remains unannotated.

  • 必需元素概要

    所需元素
    修饰符和类型
    必需的元素
    说明
    Class<? extends Annotation>