티스토리 뷰
stereotype annotation이 무엇일까?
전체 아키텍처에서 type 혹은 method의 역할을 나타내는 어노테이션을 뜻합니다.
종류로는 Component, Controller, Indexed, Repository, Service가 있습니다.
1. Indexed
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Indexed {
}
어노테이션이 달린 요소가 index에 대한 stereotype임을 나타냅니다.
CandidateComponentsIndex는 컴파일 시 생성된 메타데이터 파일을 사용하는 classpath scanning 대상입니다.
index를 사용하면 stereotype에 따라 candidate components(정규화된 이름)를 검색할 수 있습니다.
Indexed 어노테이션은 색인화하도록 제너레이터에 지시합니다.
- 어노테이션이 달린 요소(element)
- 주석이 달린 요소를 implements 혹은 extends하는 요소(element)
stereotype은 어노테이션이 달린 요소의 정규화된 이름입니다.
meta-annotated를 사용하는 디폴트 컴포넌트 어노테이션을 Indexed 어노테이션으로 여깁니다.
meta-annotated는 어노테이션을 선언할 때 사용하는 어노테이션이라고 합니다.
예시로, @Target, @Retention, @Inherited 등이 있습니다.
Component 어노테이션이 있는 component인 경우, org.springframework.stereotype.Component sterotype을 사용하여 해당 component에 대한 항목이 Index에 추가됩니다.
Indexed 어노테이션은 meta-annotation에도 적용됩니다.
package com.example;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Indexed
@Service
public @interface PrivilegedService { ... }
위에 어노테이션이 type에 있으면 두 가지 stereotype로 index됩니다.
- org.springframework.stereotype.Component
- com.example.PrivilegedService
Service 어노테이션은 인덱싱된 상태로 직접 annotated되지는 않지만 Component를 사용하여 meta-annotated됩니다.
@Indexed를 추가하여 특정 인터페이스의 모든 구현체 혹은 특정 클래스의 모든 하위 클래스를 인덱싱할 수도 있습니다.
다음 기본 인터페이스를 살펴봅시다.
package com.example;
@Indexed
public interface AdminService { ... }
이제 이 AdminService를 다음과 같이 구현할 수 있습니다.
package com.example.foo;
import com.example.AdminService;
public class ConfigurationAdminService implements AdminService { ... }
이 클래스는 인덱싱된 인터페이스를 구현하므로 com.example.AdminService stereotype에 자동으로 포함됩니다.
계층에 @Indexed 인터페이스 및 슈퍼클래스가 더 많으면 클래스는 모든 stereotypes에 매핑됩니다.
2. Component
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Indexed
public @interface Component {
String value() default "";
}
Component 어노테이션이 달린 클래스가 "Component"임을 나타냅니다.
이러한 클래스는 annotation-based configuration 및 classpath scanning을 사용할 때 자동 탐지의 후보로 간주됩니다.
다른 클래스 레벨 어노테이션(ex @Repository, @Aspect)도 같은 component를 식별하는 것으로 간주될 수 있습니다.
3. Controller
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller {
/**
* The value may indicate a suggestion for a logical component name,
* to be turned into a Spring bean in case of an autodetected component.
* @return the suggested component name, if any (or empty String otherwise)
*/
@AliasFor(annotation = Component.class)
String value() default "";
}
Controller 어노테이션이 달린 클래스가 "Controller"임을 나타냅니다.
Controller 어노테이션은 @Component의 specialization 역할을 하며, classpath scanning을 통해 구현 클래스를 자동으로 탐지할 수 있습니다.
일반적으로 RequestMapping 어노테이션을 기반으로 어노테이션이 달린 핸들러 메서드와 함께 사용됩니다.
예시로, InboundApi class를 가져왔습니다.
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/inbounds")
public class InboundApi {
// ...
}
4. Service
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Service {
/**
* The value may indicate a suggestion for a logical component name,
* to be turned into a Spring bean in case of an autodetected component.
* @return the suggested component name, if any (or empty String otherwise)
*/
@AliasFor(annotation = Component.class)
String value() default "";
}
Service 어노테이션이 달린 클래스가 "Service"임을 나타냅니다.
원래 도메인 중심 설계(Evans, 2003)에 의해 "캡슐화된 상태 없이 모델에서 독립적으로 제공되는 인터페이스로 제공되는 작업"으로 정의됩니다.
클래스가 "Business Service Facade" 또는 이와 유사한 것임을 나타낼 수도 있습니다.
Service 어노테이션은 일반적인 목적의 stereotype이며 팀마다 의미를 좁히고 적절하게 사용할 수 있습니다.
Service 어노테이션은 @Component의 specialization 역할을 하며, classpath scanning을 통해 구현 클래스를 자동으로 탐지할 수 있습니다.
5. Repository
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Repository {
/**
* The value may indicate a suggestion for a logical component name,
* to be turned into a Spring bean in case of an autodetected component.
* @return the suggested component name, if any (or empty String otherwise)
*/
@AliasFor(annotation = Component.class)
String value() default "";
}
Repository 어노테이션이 달린 클래스가 "Repository"임을 나타냅니다.
원래 도메인 중심 설계(Evans, 2003)에 의해 "객체 모음을 에뮬레이트하는 저장, 검색 및 검색 동작을 캡슐화하는 메커니즘"으로 정의됩니다.
"Data Access Object(DAO)"와 같은 전통적인 자바 EE 패턴을 구현하는 팀들은 DAO 클래스에 이러한 stereotype를 적용할 수 있지만, 그렇게 하기 전에 DAO와 DDD 스타일 저장소 간의 차이를 이해하도록 주의를 기울여야 합니다.
Repository 어노테이션은 일반적인 목적의 stereotype이며 팀마다 의미를 좁히고 적절하게 사용할 수 있습니다.
따라서 @Repository가 달린 클래스는 PersistenceExceptionTranslationPostProcessor와 함께 사용될 때 Spring Data Access Exception 변환에 적합합니다.
@Repository가 달린 클래스는 tooling, aspects 등을 목적으로 하는 전체 애플리케이션 아키텍처에서의 역할에 대해서도 명확히 설명됩니다.
스프링 2.5에서 Repository 어노테이션은 @Component의 specialization 역할을 하며, classpath scanning을 통해 구현 클래스를 자동으로 탐지할 수 있습니다.
'Sping Framework' 카테고리의 다른 글
자바빈(JavaBean) (0) | 2022.07.10 |
---|---|
stereotype annotation들을 어떻게 찾아서 bean으로 등록할까? (0) | 2022.06.21 |
Spring MVC lifecycle (0) | 2022.05.15 |
XML In Java [2]. XML String to VO (0) | 2022.04.23 |
XML In Java [1]. VO to XML String (0) | 2022.04.23 |
- Total
- Today
- Yesterday
- Spring
- kotest
- programmers
- 클린 아키텍처
- 테라폼
- 객체지향
- Algorithm
- 이펙티브 자바
- 프로그래머스
- 디자인패턴
- 디자인 패턴
- Java
- JPA
- 정규표현식
- BAEKJOON
- 백준
- kkoon9
- AWS
- C++
- 클린 코드
- node.js
- MSA
- Kotlin
- 이팩티브 자바
- Olympiad
- 코테
- Spring Boot
- BOJ
- Effective Java
- 알고리즘
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |