在每个字段的类级别使用Java中的注释

时间:2017-07-20 15:19:08

标签: java annotations

我有一个自定义注释,我目前在每个字段都有注释。这很好,但不是超级干净。我希望能够将这个注释放在类上,然后自动注释每个字段。

@Target(value = {ElementType.TYPE, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface TestAnnotation {

    boolean searchable() default true;

    String attribute() default "";
}

// Current Class
@Entity
@Table(name = "TESTER")
public class Tester () {

    @Id
    @GeneratedValue
    @TestAnnotation 
    @Column(name = "ID")
    private Long id;

    @TestAnnotation 
    @Column(name = "COL_1", nullable = false)
    private String col1;

    @TestAnnotation 
    @Column(name = "COL_2", nullable = false)
    private String col2;

    @TestAnnotation 
    @Column(name = "COL_3", nullable = false, unique = true)
    private String col3;

    @TestAnnotation 
    @Column(name = "COL_4", nullable = false, unique = true)
    private String col4;
}
// Expectation
@Entity
@TestAnnotation 
@Table(name = "TESTER")
public class Tester () {

    @Id
    @GeneratedValue
    @Column(name = "ID")
    private Long id;

    @Column(name = "COL_1", nullable = false)
    private String col1;

    @Column(name = "COL_2", nullable = false)
    private String col2;

    @Column(name = "COL_3", nullable = false, unique = true)
    private String col3;

    @Column(name = "COL_4", nullable = false, unique = true)
    private String col4;
}

0 个答案:

没有答案