์ด์ ํฌ์คํ ์ ํตํด ์ด๋ ธํ ์ด์ ์ ํตํด DI๋ฅผ ํ๋ ๊ณผ์ ์ ๊ณต๋ถํด ๋ณด์๋ค. (unounou.tistory.com/44?category=1183877)
์ด๋ ธํ ์ด์ ์ ์ด์ฉํ DI (@Autowired, @Qualifier)
์ด์ ํฌ์คํ ์์ ๋น์ ์์ฑ๊ณผ ์ฃผ์ ์ ๊ด๋ จํ ๋ถ๋ถ์ ์์ค ์ฝ๋ ์ธ๋ถ์ธ xml์ผ๋ก ๋นผ๋์ ๊ณผ์ ์ ์ดํด๋ณด์๋ค. ์์ค์ ์์ ์ฌํญ์ด ์์ ๋ ์์ค ์์ฒด๋ฅผ ์์ ํ์ง ์์๋ ๋๊ณ xml์ ์ค์ ๋ถ๋ถ๋ง ์์
unounou.tistory.com
๋น์ ์์ฑํ๋ ๋ถ๋ถ์ xml์ ์๊ณ ์๋ฐ ์์ค์ @Autowired๋ฅผ ๋ถ์ฌ ์์ฑ๋ ๋น์ ์ธํ ํ๋ ๊ณผ์ ์ด๋ค. ๊ทธ๋ ๋ค๋ฉด ์ด๋ฒ์๋ ๋น ์์ฑ ๊ณผ์ ์ญ์ ์๋ฐ ์์ค์์ ์ด๋ ธํ ์ด์ ์ผ๋ก ์ฒ๋ฆฌํด ๋ณด๋๋ก ํ๊ฒ ๋ค.
GridExamConsole.java
@Component
public class GridExamConsole implements ExamConsole {
@Autowired(required=false)
@Qualifier("exam1")
private Exam exam;
}
NewlecExam.java
@Component
public class NewlecExam implements Exam {
//์ดํ ์๋ต
}
ํด๋์ค์ @Component ์ด๋ ธํ ์ด์ ์ ๋ถ์์ผ๋ก์จ xml ์์ด ๋น์ ์์ฑํ ์ ์๋ค.
์์ฑ๋ ๋น์ @Autowired๋ฅผ ํตํด ์ฃผ์ ํ๊ธฐ ์ํด์ xml์ <context:annotation-config/> ํ๊ทธ๋ฅผ ๋ถ์๋ค. ์ด์ฒ๋ผ @Component ์ด๋ ธํ ์ด์ ์ ํตํด ๋น์ ์์ฑํ๊ธฐ ์ํด์๋ xml์ ํ๊ทธ๊ฐ ํ์ํ๋ค. ์๋์ฒ๋ผ ํ๊ทธ๋ฅผ ์ถ๊ฐํด ์ค๋ค.
<context:component-scan> ํ๊ทธ๋ฅผ ํตํด @Component ์ด๋ ธํ ์ด์ ์ด ๋ถ์ ํด๋์ค๋ฅผ ์ฝ๊ฒ ๋๋ฉด, ์๋์ผ๋ก ๊ทธ ์์์๋ @Autowired ํ๊ทธ๋ ์ฝ๊ธฐ ๋๋ฌธ์ <context:annotation-config/> ํ๊ทธ๋ ๋์ด์ ํ์์น ์์ ์ง์์ฃผ์๋ค.
setting.xml
<context:component-scan base-package="spring.di.ui, spring.di.entity"/>
๊ฐ์ฒด์ ์์ฑ, ์ฃผ์ ๋ชจ๋ ์ด๋ ธํ ์ด์ ์ด ๋์ ํ๊ธฐ ๋๋ฌธ์ xml์๋ ์ปดํฌ๋ํธ ์ค์บ ํ๊ทธ๋ง ๋จ์์๋ค. base-package์ ๋ช ์๋ ํจํค์ง์์ @Component ์ด๋ ธํ ์ด์ ์ด ๋ถ์ ํด๋์ค๋ฅผ ์ฐพ์ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ผ๋ ๋ป์ด๋ค. @Component ์ด๋ ธํ ์ด์ ์ด ๋ถ์ ๋ ํด๋์ค์ ํจํค์ง๊ฐ ๋ค๋ฅด๊ธฐ ๋๋ฌธ์ ์ผํ๋ก ์ฐ๊ฒฐํด ์ฐพ์์ผ ํ ํจํค์ง๋ฅผ ์ ๋ถ ์จ์ฃผ์๋ค.
Program.java
public class Program {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("spring/di/setting.xml");
Exam exam = context.getBean(Exam.class);
//ExamConsole console = (ExamConsole) context.getBean("console");
ExamConsole console = context.getBean(ExamConsole.class);
console.print();
}
}
๊ฐ์ฒด๋ฅผ ๊บผ๋ด๋ ๋ ๊ฐ์ง ๋ฐฉ๋ฒ์ธ ํด๋์ค๋ก ๊บผ๋ด๊ธฐ, ์ด๋ฆ์ผ๋ก ๊บผ๋ด๊ธฐ ์ค์์ ํด๋์ค๋ก ๊บผ๋ด๋ณด์๋ค. ๋ง์ฝ ์ด๋ฆ์ผ๋ก ๊บผ๋ด๊ณ ์ถ๋ค๋ฉด @Component("console") ์ด๋ฐ ์์ผ๋ก ์ด๋ฆ ์ต์ ์ ์จ์ฃผ๋ฉด ๋๋ค. ์ด๋ฆ์ผ๋ก getBean ํ ๋ ์ด๋ฆ ์ต์ ์์ด @Component ์ด๋ ธํ ์ด์ ์ ๊ทธ๋๋ก ์ฌ์ฉํ๋ค๋ฉด ์๋ฌ๊ฐ ๋ฐ์ํ๋ค.
* @Value ์ด๋ ธํ ์ด์
NewlecExam.java
@Component
public class NewlecExam implements Exam {
@Value("20")
private int kor;
@Value("50")
private int eng;
private int math;
private int com;
}
@Value ์ด๋ ธํ ์ด์ ์ ์ด์ฉํ๋ฉด ๊ฐ์ฒด ์์ฑ ์์ ๊ฐ์ ์ด๊ธฐํ ํ ์ ์๋ค.
* ํนํ๋ @Component ์ด๋ ธํ ์ด์ (@Controller / @Service / @Repository)
Spring MVC ํจํด์์ ์ฌ์ฉํ๋ Controller, Service, Dao์ ์ญํ ์ ๋ถ๋ช ํ๊ฒ ๋ช ์ํ๊ธฐ ์ํด์ ํนํ๋ ์ด๋ ธํ ์ด์ ์ ์ฌ์ฉํ๋ค. ์์ ์ฌ์ฉํ NewlecExam ๊ฐ์ฒด๋ Entity์ ์ํ๋ค.
๋ด๋ ์ฒ ์คํ๋ง ํ๋ ์์ํฌ ๊ฐ์๋ฅผ ๋ฃ๊ณ ์ ๋ฆฌํ ๊ฒ์๊ธ์ ๋๋ค.
'๐ปStudy > Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
AOP(Aspect Oriented Programming)์ ๊ฐ๋ (0) | 2021.02.13 |
---|---|
XML Configuration์ Java Configuration์ผ๋ก ๋ณ๊ฒฝํ๊ธฐ (0) | 2021.02.13 |
์ด๋ ธํ ์ด์ ์ ์ด์ฉํ DI (@Autowired, @Qualifier) (0) | 2021.02.12 |
xml์ ์ด์ฉํ ์คํ๋ง DI ์ง์์ ์์ฑ(Spring Bean Configuration), IoC ์ปจํ ์ด๋ ์ฌ์ฉํ๊ธฐ (0) | 2021.02.12 |
์คํ๋ง ์์ด DI ํด๋ณด๊ธฐ (0) | 2021.02.11 |