@ComponentScan
어노테이션은 빈으로 등록 될 준비를 마친 클래스들을 스캔하여, 빈으로 등록해 준다.
@Controller, @Service, @Component, @Repository 등 어노테이션을 붙인 클래스들이 빈으로 등록 될 준비를 한 것이다.
// XML
<context:component-scan base-package="com.example.twice" use-default="false">
<context:include-filter type="annotation"
expression="org.springframework.Controller"/>
</context:component-scan>
// JAVA
@Configuration
@ComponentScan(
basePackages = "com",
useDefaultFilters = false,
includeFilters = {
@Filter(
type = FilterType.ANNOTATION,
classes = {Component.class, Repository.class, Service.class, Controller.class}
)
}
)
use default는 @Controller, @Component, @Service, @Repository등을 스캔하지 않는다. 는 것이다.
기본적으로 스캔되는 어노테이션을 스캔하지 않고, include filter를 통해서 특정 어노테이션만 스캔할 수 있다.
@EnableJpaRepositories
어노테이션은 JPA Repository를 빈으로 등록하는 역할을 한다.
@EnableJpaRepositories 어노테이션은 @SpringBootApplication 어노테이션 안에 이미 등록되어 있어 스프링 부트를 사용할 경우에는 따로 어노테이션을 추가할 필요가 없다.
@EntityScan
어노테이션은 애플리케이션이 실행될 때 basePackages로 지정된 패키지 하위에서 JPA 엔티티 (@Entity 어노테이션이 설정된 도메인 클래스)를 검색하여 Entity Class를 찾을 수 있도록 해주는 어노테이션이다.
외부 프로젝트에 package에 있는 Entity class를 찾는다.
'개발 > Spring' 카테고리의 다른 글
[Spring] JPA 기본 키 생성 전략 (0) | 2022.03.02 |
---|---|
[Spring] @transactional 동작 원리 (0) | 2022.02.23 |
[Spring] JPA 복합 키 생성 (@Embeddable, @IdClass) (0) | 2022.02.22 |
[Spring] JPA 잠금(Lock) 이해하기 (0) | 2022.02.15 |