본문 바로가기
카테고리 없음

(Spring) 오토 스캔 (component-scan / @Repository 등)

by kakk789 2022. 6. 8.

컨트롤러 객체를 xml에 자동으로 설정 (오토 스캔)

  • component-scan : Bean으로 등록 될 준비를 마친 클래스를 스캔하여 Bean으로 등록!
  • @Controller, @Service, @Component, @Repository 어노테이션을 붙힌 클래스를 자동으로 Bean으로 등록한다
  • @Autowired : 특정 객체에 대해 해당 어노테이션을 붙히면 자동으로 Bean으로 등록 한다.
1. context에 대한 네임스페이스 설정 필요 ( servlet.xml에 필요 )
xmlns:context="http://www.springframework.org/schema/context"

2. xsi:schemaLocation에 2개의 URL 추가
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"

3. 오토스캔 (해당 패키지 명 안에 존재하는 컨트롤러 클래스를 자동으로 xml에 등록)
<context:component-scan base-package="패키지 명" />

xxx-servlet.xml

       xmlns:context="http://www.springframework.org/schema/context"
	<context:component-scan base-package="com.sist.controller"></context:component-scan>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="
    ................
    xmlns:context="
    http://www.springframework.org/schema/context"
    ...............
    
    xsi:schemaLocation="
    .............. 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.sist.controller"></context:component-scan>
    <context:component-scan base-package="com.sist.dao"></context:component-scan>

@Repository

@Repository
public class BookDAO {

	public List<BookVO> listBook(){
		
		return DBManager.listBook();
	}
	
	public int insertBook(BookVO vo) {
		return DBManager.insertBook(vo);
	}
}

 

@Autowired 예시
@Autowired : 특정 객체에 대해 해당 어노테이션을 붙히면 자동으로 Bean으로 등록 한다.
반응형

댓글