티스토리 뷰

3-4 (Dependency Injection) - Annotation 기반 설정

Annotation 설정

우선 XML 설정 파일에 Namespace 부터 설정함.

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemalLocation=

"http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/contxt/springcontext-3.0.xsd">

</beans>

Component Scan

<beans …… />

<context:component-scan base-package="com.multicampus.biz" />

</beans>

<bean />과 관련된 Annotation

<bean /> 으로 명시적 등록하는 대신 @Component 같은 Annotation 사용하여 등록함.

component scan에 의해 Bean이 자동 등록됨.

@Component("userService")

public class UserServiceImpl implements UserService {

}

Dependency Injection 설정

@Autowired

Spring에서 지원하는 Annotation(Spring 2.5 이상)

객체 이름 = 클래스명의 첫글자만 소문자로 바꾼 것

@Service("userService")

public class UserServiceImpl implements UserService {

@Autowired

private UserDAO userDAO;

}

Setter 메서드나 생성자를 작성할 필요가 없음

@Qualifier

주입하려는 객체명과 클래스명이 다른 경우나 모호한 경우 사용

@Service("userService")

public class UserServiceImpl implements UserService {

@Autowired

@Qualifier("userDAOJDBC")

private UserDAO userDAO;

}

@Resource

Bean Name을 지정하여 Dependency Injection 하는 경우 사용함.

JSR-250 표준 Annotation

특정 프레임워크에 종속적이지 않음.

@Service("userService")

public class UserServiceImpl implements UserService {

@Resource(name="userDAO")

private UserDAO userDAO;

}

'It' 카테고리의 다른 글

명령어 세트  (0) 2022.08.16
명령어 파이프라이닝  (0) 2022.08.16
명령어 실행  (0) 2022.08.16
Spring AOP 4-1 AOP  (0) 2022.08.15
Dependency Injection 3-3 Dependency Injection  (0) 2022.08.15