• Home
  • About
    • Junseok photo

      Junseok

      개발자 블로그

    • Learn More
    • Facebook
    • Instagram
    • Github
  • Posts
    • All Posts
    • All Tags
  • Java
    • java-basic
    • java-solid
    • java-pattern
    • java-logging
  • Javascript
  • Angular
  • spring
    • spring-framework
    • spring-boot
    • spring-test
  • server
    • jeus
    • webtob
    • tomcat
  • test
    • junit
    • assertj
    • hamcrest
    • dbunit
    • spring
  • docker
  • unix
  • maven
  • db
  • network
  • eclipse
  • intellij
  • microservices
  • etc

Meta-annotations

11 Mar 2018

Reading time ~1 minute

Spring에서 제공하는 많은 annotation은 여러분의 코드에서 meta-annotations 으로 사용할 수 있습니다.

meta-annotation은 단순히 다른 annotation에 적용 할 수있는 annotation입니다.
예를 들어 위에서 언급 한 @Service 어노테이션은 @Component 메타 주석으로 표시됩니다.

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component // Spring은 이것을보고 @Service를 @Component와 같은 방식으로 처리합니다.
public @interface Service {

    // ....
}

메타 어노테이션을 결합하여 합성 어노테이션을 만들 수도 있습니다.
예를 들어, Spring MVC의 @RestController 어노테이션은 @Controller와 @ResponseBody로 구성된다.

또한 작성된 어노테이션은 선택적으로 사용자 정의가 가능하도록 메타 어노테이션의 속성을 재 선언 할 수 있습니다.
이는 메타 어노테이션 속성의 하위 집합 만 노출하려는 경우 특히 유용 할 수 있습니다.
예를 들어 Spring의 @SessionScope 어노테이션은 scope name을 세션에 하드 코드하지만
proxyMode를 사용자 정의 할 수 있습니다.

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Scope(WebApplicationContext.SCOPE_SESSION)
public @interface SessionScope {

    /**
     * {@link Scope#proxyMode}의 Alias(별칭) 입니다.
     * 기본값은 {@link ScopedProxyMode#TARGET_CLASS}.
     */
    @AliasFor(annotation = Scope.class)
    ScopedProxyMode proxyMode() default ScopedProxyMode.TARGET_CLASS;

}

@SessionScope은 다음과 같이 proxyMode를 선언하지 않고 사용할 수 있습니다.

@Service
@SessionScope
public class SessionScopedService {
    // ...
}

또는 다음과 같이 proxyMode에 대한 재정의 된 값을 사용합니다.

@Service
@SessionScope(proxyMode = ScopedProxyMode.INTERFACES)
public class SessionScopedUserService implements UserService {
    // ...
}

자세한 내용은 Spring Annotation Programming Model위키 페이지 를 참조하십시오.



springframeworkannotation Share Tweet +1