ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [JAVA] 커스텀 어노테이션 @interface
    자바 2024. 11. 14. 12:56
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.FIELD)
    public @interface DataField {}

    @interface 어노테이션이 뭘까 찾아봤습니다

     

     

    어노테이션의 종류

    Java의 어노테이션은 크게 built-in 어노테이션과 Meta 어노테이션이 존재합니다

    1. built-in 어노테이션 - Java 코드에 적용되는 어노테이션
    @Overrie, @Deprecated, @SuppressWarnings 등
    2. meta 어노테이션 - 다른 어노테이션에 적용되기 위한 어노테이션 

    @Retention, @Documneted, @Target, @Inherited, @Repeatable 등

     

     

    커스텀 어노테이션

    커스텀 어노테이션은 자바의 리플렉션을 활용하여 특정 목적으로 사용할 수 있습니다
    ( 그전까지는 그저 문자열 정보를 가지고 있을 뿐 )
    - 반복되는 코드 개선 ex. 로그인 세션 정보 유지하는 객체
    - AOP 적용을 위해서 별도로 커스텀 어노테이션을 만들어야할 일

     

     

    커스텀 어노테이션 @interface
    @Target(ElementType.[적용할 대상])
    @Retention(RetentionPolicy.[정보유지 대상]
    public @interface CustomAnotation{
    
    };

    1. @interface
    Java에서 제공하는 어노테이션 외에 사용자가 커스텀하여 사용할 수 있는 어노테이션
    이 @interface는 클래스 또는 변수를 특정 값으로 지정하여 관리할 수 있다는 장점이 있음

    2. @Retention

    어느 시점까지 어노테이션의 메모리를 가져갈 지 설정
    - Indicates how long annotations with the annotated type are to be retained
    - the retention policy defaults to {@code RetentionPolicy.CLASS}
    - SOURCE(소스 코드(.java)), CLASS(클래스 파일(.class)), RUNTIME 

    3. @Target

    해당 사용자가 만든 어노테이션이 부착될 수 있는 타입을 지정하는 것
    필드, 메소드, 클래스, 파라미터 등 선언할 수 있는 타입을 설정

     

     

    커스텀 어노테이션 EX
    @Target({ElementType.FIELD})
    @Retention(value = RetentionPolicy.RUNTIME)
    public @interface ExCustomSpecification {
        String name();
        int order();
    }
    
    public class ExSpecification {
        @ExCustomSpecification(name = "----", order = 1)
        private String ExField;
    }
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.FIELD)
    public @interface DataFieldEx {
        int length();
    }
    
    private static List<FieldExInfo> getAnnotatedFields(Class<?> dtoExClass) {
    	...
    	DataFieldEx annotation = field.getAnnotation(DataFieldEx.class);
    	...
    }

     

     

     

     

     


    (참고)

    https://mangkyu.tistory.com/130
    https://hbase.tistory.com/169
    https://pamyferret.tistory.com/47
    https://curiousjinan.tistory.com/entry/spring-custom-annotation
    https://blog.naver.com/javaking75/220727816394

    728x90

    '자바' 카테고리의 다른 글

    [JAVA] 자바 basic 5 - 8 강 요약  (1) 2024.11.19
    [JAVA] 자바 basic 1 - 4 강 요약  (0) 2024.11.19
    [JAVA] Class 클래스  (1) 2024.11.14
    [JAVA] Int & Integer 차이점 그리고 wrapper 클래스  (0) 2024.08.22
    [JAVA] Static 키워드  (0) 2024.08.20
Designed by Tistory.