component-scan
빈으로 등록될 준비를 마친 클래스*들을 scan하여, 빈으로 등록해준다.
*@Controller, @Service, @Componet, @Respository 어노테이션을 붙인 클래스
사용법
1. xml 파일 설정
나는 기존에 진행한 플젝 파일에서 mybatis 주입된 root-servlet.xml 파일에 추가함
1) 한 개의 base-package 사용
base-package 를 기준으로 클래스를 스캔하여 빈으로 등록한다.
| <context:component-scan base-package="com.spring.board"/> |
2) 여러 개의 base-package를 사용할 경우
| <context:component-scan base-package="com.spring.board, com.spring.member"/> |
3) 설정한 package를 전부 스캔하지 않고 특정한 객체만 빈으로 등록하고 싶을 때
include-filter / exclude-filter
● include-filter
| <context:component-scan base-package="com.spring.board" use-dafault="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> |
● exclude-filter
| <context:component-scan base-package="com.spring.board" use-dafault="false"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> |
use-default="false" > 기본 어노테이션을 스캔하지 않겠다.
*-filter > 특정 어노테이션 스캔
2. .java 파일 안에서 설정
| @Configuration @ComponentScan(basePackages="com.spring.board") public class ApplicationConfig{ } |
@Configuration > 이 클래스가 xml을 대체하는 설정 파일임을 선언
@ComponentScan을 통해 basePackages를 설정해준다.
'Backend > Spring' 카테고리의 다른 글
| 게시판 CRUD 진행 중 javax.annotation.resource를 import하지 못해서 발생하는 에러 (0) | 2024.01.04 |
|---|---|
| Java VM Arguments 설정? (0) | 2024.01.04 |
| [Spring]Project 동작 순서 (0) | 2023.06.03 |
| Spring framework 동작원리 (0) | 2023.06.03 |
| [Spring]InternalResourceViewResolver를 이용한 View Page 지정(prefix, suffix) (1) | 2023.06.03 |