Spring

[스프링 #6] Custom Annotation

zamezzz 2018. 5. 30. 00:42

 스프링 #6. Custom Annotation



 Annotation


Custom Annotation을 생성하는 방법에 대해서 정리해보겠습니다.


먼저 Annotation이란 무엇인지 간단히 정의하면 사전적의미로는 '주석'이라는 뜻입니다.


즉, 코드에 주석처럼 달아주어 특정한 의미를 부여해준다고 보시면 됩니다.


보통 @Controller와 같이 미리 정의된 Annotation을 많이 사용하는데요.


사용자가 필요에 의해 직접 Annotation을 추가할 수도 있습니다.



 Custom Annotation


간단하게 보면 아래와 같은 형태를 가집니다.


@Target(ElementType.METHOD) 

@Retention(RetentionPolicy.Runtime) 


public @interface MyAnnotation { 

    int myCount() default 10 

}


Annotation명MyAnnotation입니다.


그리고 myCount라는 int형 값을 갖습니다. 입력이 없을 경우 기본 값은 10입니다.


사용시에는 @MyAnnotation(myCount=1) 이러한 방식으로 사용할 수 있겠습니다.


다음은 위에 선언된 @Target@Retention에 대해 알아보겠습니다.


@Target Annotation은 어디에 우리가 만든 custom annotation을 적용할지를 정하는


역할을 합니다. 위에서는 METHOD에 적용하기로 설정을 했습니다.


실제 @Target 소스를 까보면 아래와 같은 Element Type이 정의되어 있습니다.


 public enum ElementType {

    /** Class, interface (including annotation type), or enum declaration */
    TYPE,
    /** Field declaration (includes enum constants) */
    FIELD,
    /** Method declaration */
    METHOD,
    /** Formal parameter declaration */
    PARAMETER,
    /** Constructor declaration */
    CONSTRUCTOR,
    /** Local variable declaration */
    LOCAL_VARIABLE,
    /** Annotation type declaration */
    ANNOTATION_TYPE,
    /** Package declaration */
    PACKAGE,
    /**
     * Type parameter declaration
     *
     * @since 1.8
     */
    TYPE_PARAMETER,
    /**
     * Use of a type
     *
     * @since 1.8
     */
    TYPE_USE
}


@Retention Annotation은 언제까지 annotation을 남길 것인지를 설정합니다.


위 설정에서는 RetentionPolicy.RUNTIME로 설정하여 어노테이션을 런타임때까지 


남기겠다고 설정을 하였습니다. 이 값 역시 실제 코드를 보면 아래와 같이 있습니다.


public enum RetentionPolicy {
    /**
     * Annotations are to be discarded by the compiler.
     */
    SOURCE,
    /**
     * Annotations are to be recorded in the class file by the compiler
     * but need not be retained by the VM at run time.  This is the default
     * behavior.
     */
    CLASS,
    /**
     * Annotations are to be recorded in the class file by the compiler and
     * retained by the VM at run time, so they may be read reflectively.
     *
     * @see java.lang.reflect.AnnotatedElement
     */
    RUNTIME

} 


쉽게 말해 SOURCE는 컴파일 시 사라지며, CLASS는 컴파일러가 클래스를 참조할 때까지


유효합니다. 그리고 RUNTIME은 컴파일 이후에도 VM을 통해 참조 가능한 설정입니다.


다시 우리의 MyAnnotation으로 돌아가보겠습니다.


@Target(ElementType.METHOD) 

@Retention(RetentionPolicy.Runtime) 


public @interface MyAnnotation { 

    int myCount() default 10 

}


위에서 말한대로 @MyAnnotation(myCount=1) 이렇게 사용을 할 수 있습니다.

하지만 아직은 정말 '주석'정도의 역할만 할 수 있습니다.


실제 이 Annotation이 특정한 역할을 가지기 위해서는 동작 구현이 필요합니다.


보통 해당 Annotation이 붙어 있을 시 어떠한 역할을 하도록 인터셉터에서 그 동작을


구현하곤합니다. 다음 포스팅에서는 인터셉터에 이 Annotation을 붙여서 특정한 동작을


수행하도록 정리해보겠습니다.




감사합니다.


반응형

'Spring' 카테고리의 다른 글

STOMP에 대한 이해  (0) 2022.03.14
[스프링 #7] Test  (0) 2019.03.30
[스프링 #5] DispatcherServlet  (0) 2018.05.05
[스프링 #4] 의존관계 주입 (DI) - 2  (0) 2018.02.24
[스프링 #3] 의존관계 주입 (DI) - 1  (0) 2018.01.19