[ 이전 글 ] XML 사용법
XML 사용법
[ 이전 글 ] Inversion of Control Container ( Ioc Container ) Inversion of Control Container ( Ioc Container ) ( 이전 글과 이어지는 내용입니다. https://cloakinghost.tistory.com/8 ) 작성한 XML 파일을 읽어오는 코드를 작성해보
cloakinghost.tistory.com
XML을 통해 설정한 내용을 자동으로 적용해주기 위해 어노테이션을 사용해보자
<context:annotation-config/>
객체 생성 후 스프링에게 특정 어노테이션이 있는지 확인을 요청
- @Autowired
- DI(객체 주입) 자동으로 해줌
- class타임을 멤버로 가질 때 사용
- @Autowired(required = false) : XML의 Bean이 없어도 실행 가능
- @Qualifier : XML Bean id 특정 시 사용 / 동일 클래스 Bean이 다수 일 경우
- @Qualifier("Bean'id")
@Autowired 사용 시
XML에서 setter나 생성자 인자를 지정하지 않았는데 무엇을 근거로 할까?
XML에 지시한 Bean의 개수에 따라 달라진다
Bean 개수 | id 필요성 ( XML ) | 참조 대상 ( Java ) | @Qualifier 사용 |
1개 | X | 참조형명 ( class명 ) | X |
2개 이상 | O | 변수명 | O ( id 지정 필수 ) |
어노테이션 위치 : 상황에 맞춰 사용하자 ( Setter DI / Constructor DI )
1. 필드 : 기본생성자만 있을 경우 / 오버로딩 생성자가 있다면 기본생성자를 따로 선언 ( Constructor DI )
@Autowired
@Qualifier("exam2")
private Exam exam;
2. 생성자 : @Qualifier 사용 시 파라미터에 적용 ( Constructor DI )
@Autowired
public GridExamConsole(@Qualifier("exam2") Exam exam) {
System.out.println("Overloaded Constructor");
this.exam = exam;
}
3. 메소드 ( Setter DI )
@Autowired
@Qualifier("exam2")
@Override
public void setExam(Exam exam) {
System.out.println("Setter");
this.exam = exam;
}
1. 기본 @Autowired 일 때
XML
<context:annotation-config/> <!-- annotation을 읽도록 설정 함 -->
<bean id="exam1" class="spring.di.entity.ScoreExam" p:kor="10" p:eng="20"/>
<bean id="exam2" class="spring.di.entity.ScoreExam" p:kor="30" p:eng="40"/>
<bean id="console" class="spring.di.ui.GridExamConsole"/>
Java
package spring.di.ui;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import spring.di.entity.Exam;
public class GridExamConsole implements ExamConsole {
// @Autowired // xml -> annotation 순으로 읽고 @Autowired 만나면 객체를 자동으로 DI 함
@Autowired
@Qualifier("exam2")
private Exam exam;
public GridExamConsole() {
System.out.println("Constructor");
}
public GridExamConsole(Exam exam) {
System.out.println("Overloaded Constructor");
this.exam = exam;
}
@Override
public void setExam(Exam exam) {
System.out.println("Setter");
this.exam = exam;
}
@Override
public void print() {
System.out.println("---------- ----------");
System.out.println("| total avg |");
System.out.println("|--------- ---------|");
System.out.printf("| %3d %3.2f |\n", exam.total(), exam.avg());
System.out.println("---------- ----------");
}
}
2. @Autowired(required = false) 일 때
XML
<context:annotation-config/>
<bean id="console" class="spring.di.ui.GridExamConsole"/>
Java
package spring.di.ui;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import spring.di.entity.Exam;
public class GridExamConsole implements ExamConsole {
// @Autowired // xml -> annotation 순으로 읽고 @Autowired 만나면 객체를 자동으로 DI 함
@Autowired(required = false)
@Qualifier("exam2")
private Exam exam;
public GridExamConsole() {
System.out.println("Constructor");
}
public GridExamConsole(Exam exam) {
System.out.println("Overloaded Constructor");
this.exam = exam;
}
@Override
public void setExam(Exam exam) {
System.out.println("Setter");
this.exam = exam;
}
@Override
public void print() {
int total = 0;
float avg = 0.0f;
if (exam != null) {
total = exam.total();
avg = exam.avg();
}
System.out.println("---------- ----------");
System.out.println("| total avg |");
System.out.println("|--------- ---------|");
System.out.printf("| %3d %3.2f |\n", total, avg);
System.out.println("---------- ----------");
}
}
728x90
'Spring' 카테고리의 다른 글
@Component 사용법 ( 2 ) (0) | 2023.08.07 |
---|---|
@Component 사용법 ( 1 ) (0) | 2023.08.05 |
XML 사용법 (0) | 2023.08.05 |
Inversion of Control Container ( Ioc Container ) (0) | 2023.06.14 |
Dependency Injection ( XML ) (0) | 2023.06.14 |