애플리케이션 모듈 (Application Modules)
Module- 특정 역할(도메인)을 수행하는 패키지(디렉토리)
애플리케이션 모듈의 구성요소는 다음과 같아요.
-
Provided Interface: 내가 남에게 주는 기능- 다른 모듈에 노출되는 API : 외부에서 호출할 수 있는
public서비스 클래스나 인터페이스 (예:OrderService). - 애플리케이션 이벤트 : 외부에 던지는 알림
- 다른 모듈에 노출되는 API : 외부에서 호출할 수 있는
-
Internal Component: 모듈 내에서만 사용하는 요소들- 외부 모듈이 직접 호출하면 안 되는 비즈니스 로직, 데이터베이스 처리 클래스 등
-
Required Interface: 내가 남에게 받아야 하는 기능- Spring 빈 의존성: 다른 모듈의 서비스를 주입받아 쓰는 것 (예:
OrderModule이 결제를 위해PaymentService를 의존하는 것). - 리스닝하는 이벤트: 다른 모듈이 발행한 알림을 듣는 것 (예: “결제 완료” 이벤트를 구독)
- 노출된 설정 프로퍼티 : 외부(
application.properties등)에서 주입받는 모듈 설정값.
- Spring 빈 의존성: 다른 모듈의 서비스를 주입받아 쓰는 것 (예:
ApplicationModules
ApplicationModules는 어떤 모듈들이 있고, 각 애플리케이션 모듈 구성요소가 어떻게 짜여있는지” 확인하고 검사하는 핵심 클래스에요.@SpringBootApplication이 있는 곳을 기준으로 아래 디렉토리(패키지) 들을 분석해요- 이후 메모리 상에 패키지가 어떤 모듈을 나타내는지 소유하는 모듈 지도를 만들어요.
인스턴스 생성
var modules = ApplicationModules.of(Application.class);
var modules는 메모리에 애플리케이션 모듈 배치(일종의 지도)을 가지고 있어요. 이 중 어떤 부분이 모듈로 감지될 지는 패키지의 구조 혹은 설정에 따라 달라져요. 이 부분에 대해서는 2. 애플리케이션 모듈의 구성에서 다루고 있어요.- 모듈 배치는 다음과 같이 확인할 수 있어요
modules.forEach(System.out::println);
## example.inventory ##
> Logical name: inventory
> Base package: example.inventory
> Spring beans:
+ ….InventoryManagement
o ….SomeInternalComponent
## example.order ##
> Logical name: order
> Base package: example.order
> Spring beans:
+ ….OrderManagement
+ ….internal.SomeInternalComponent
아키텍처 검증
ApplicationModules 는 검증에서도 사용되요.
var modules = ApplicationModules.of(Application.class);
modules.verify();
애플리케이션 모듈 검사에서 특정 Java 클래스나 전체 패키지를 제외하고 싶다면 다음과 같이 할 수도 있어요.
ApplicationModules.of(
Application.class,
JavaClass.Predicates.resideInAPackage("com.example.db")
).verify();
ApplicationModules.of(
Application::class.java,
JavaClass.Predicates.resideInAPackage("com.example.db")
).verify()
제외 문자열은 다음과 같이 설정할 수 있어요.
com.example.db- 지정된 패키지
com.example.db에 있는 모든 파일 제외
- 지정된 패키지
com.example.db..- 지정된 패키지(
com.example.db) - 모든 하위 패키지(
com.example.db.a또는com.example.db.b.c)에 있는 모든 파일
- 지정된 패키지(
..example..a.example,a.example.b,a.b.example.c.d의 경우에는 일치 -> 제외 대상a.exam.b-> 제외 대상 아님.
나머지 matcher 에 대한 자세한 내용은 JavaDoc of ArchUnit PackageMatcher. 에서 찾을 수 있어요.
이 글에 대해 이야기해요
질문이나 다른 관점을 남겨 주세요.