๐Ÿ‘“[Spring 6-2] Annotation์„ ์‚ฌ์šฉํ•œ Dependency Injecttion(์˜์กด๊ด€๊ณ„ ์ฃผ์ž…) ์„ค์ •

์ž„์Šนํ˜„ยท2023๋…„ 2์›” 15์ผ

Spring

๋ชฉ๋ก ๋ณด๊ธฐ
15/46

๐ŸŒˆDAO ํด๋ž˜์Šค ์ƒ์„ฑ

๐Ÿ“ƒAnnotationStudentJdbcDAO.java

โ€ป xyz.itwill05.di ํŒจํ‚ค์ง€์— AnnotationStudentJdbcDAO.java ํด๋ž˜์Šค ์ƒ์„ฑ

package xyz.itwill05.di;
//
import java.util.List;
//
import org.springframework.stereotype.Repository;
//
//@Component : ํด๋ž˜์Šค๋ฅผ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” Spring Bean์œผ๋กœ ๋“ฑ๋กํ•˜๊ธฐ ์œ„ํ•œ ์–ด๋…ธํ…Œ์ด์…˜
// โ†’ ํด๋ž˜์Šค์˜ ์ด๋ฆ„์„ beanName์œผ๋กœ ์ž๋™ ์„ค์ • - ํด๋ž˜์Šค์˜ ์ด๋ฆ„์—์„œ ์ฒซ๋ฌธ์ž๋Š” ์†Œ๋ฌธ์ž๋กœ ๋ณ€ํ™˜
// โ†’ @Component ์–ด๋…ธํ…Œ์ด์…˜์˜ value ์†์„ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ beanName ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ - ๋‹ค๋ฅธ ์†์„ฑ์ด ์—†๋Š” ๊ฒฝ์šฐ ์†์„ฑ๊ฐ’๋งŒ ์„ค์ • ๊ฐ€๋Šฅ
//@Component
//
//@Repository : DAO ํด๋ž˜์Šค๋ฅผ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” Spring Bean์œผ๋กœ ๋“ฑ๋กํ•˜๊ธฐ ์œ„ํ•œ ์–ด๋…ธํ…Œ์ด์…˜
// โ†’ ํด๋ž˜์Šค์˜ ์ด๋ฆ„์„ beanName์œผ๋กœ ์ž๋™ ์„ค์ •ํ•˜์ง€๋งŒ value ์†์„ฑ์œผ๋กœ beanName ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ
//@Repository("studentDAO")
@Repository
//@Primary : ์˜์กด์„ฑ์„ ์ฃผ์ž…ํ•˜๊ธฐ ์œ„ํ•œ ์šฐ์„ ๊ถŒ์„ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•œ ์–ด๋…ธํ…Œ์ด์…˜
// โ†’ ๋™์ผ ์ž๋ฃŒํ˜•์˜ ํด๋ž˜์Šค์— @Primary ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•œ ๊ฒฝ์šฐ ์˜์กด์„ฑ ์ฃผ์ž… ์‹คํŒจ
//@Primary
public class AnnotationStudentJdbcDAO implements StudentDAO {
	public AnnotationStudentJdbcDAO() {
		System.out.println("### AnnotationStudentJdbcDAO ํด๋ž˜์Šค์˜ ๊ธฐ๋ณธ ์ƒ์„ฑ์ž ํ˜ธ์ถœ ###");
	}
	@Override
	public int insertStudent(Student student) {
		System.out.println("*** AnnotationStudentJdbcDAO ํด๋ž˜์Šค์˜ insertStudent(Student student) ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ ***");
		return 0;
	}
	@Override
	public int updateStudent(Student student) {
		System.out.println("*** AnnotationStudentJdbcDAO ํด๋ž˜์Šค์˜ updateStudent(Student student) ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ ***");
		return 0;
	}
	@Override
	public int deleteStudent(int num) {
		System.out.println("*** AnnotationStudentJdbcDAO ํด๋ž˜์Šค์˜ deleteStudent(int num) ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ ***");
		return 0;
	}
	@Override
	public Student selectStudent(int num) {
		System.out.println("*** AnnotationStudentJdbcDAO ํด๋ž˜์Šค์˜ selectStudent(int num) ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ ***");
		return null;
	}
	@Override
	public List<Student> selectStudentList() {
		System.out.println("*** AnnotationStudentJdbcDAO ํด๋ž˜์Šค์˜ selectStudentList() ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ ***");
		return null;
	}
}

๐ŸŒˆService ํด๋ž˜์Šค ์ƒ์„ฑ

๐Ÿ“ƒAnnotationStudentServiceImpl.java

โ€ป xyz.itwill05.di ํŒจํ‚ค์ง€์— AnnotationStudentServiceImpl.java ํด๋ž˜์Šค ์ƒ์„ฑ

package xyz.itwill05.di;
//
import java.util.List;
//
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
//
//@Component("studentService")
//
//@Service : Service ํด๋ž˜์Šค๋ฅผ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” Spring Bean์œผ๋กœ ๋“ฑ๋กํ•˜๊ธฐ ์œ„ํ•œ ์–ด๋…ธํ…Œ์ด์…˜
// => ํด๋ž˜์Šค์˜ ์ด๋ฆ„์„ beanName์œผ๋กœ ์ž๋™ ์„ค์ •ํ•˜์ง€๋งŒ value ์†์„ฑ์œผ๋กœ beanName ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ
@Service("studentService")
public class AnnotationStudentServiceImpl implements StudentService {
	//@Autowired : ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๋กœ๋ถ€ํ„ฐ Spring Bean๋ฅผ ์ œ๊ณต๋ฐ›์•„ ํ•„๋“œ์— ์ €์žฅ๋˜๋„๋ก ์˜์กด๊ด€๊ณ„๋ฅผ 
	//์ž๋™์œผ๋กœ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•œ ์–ด๋…ธํ…Œ์ด์…˜
	//โ†’ ์˜์กด์„ฑ ์ฃผ์ž…์„ ์œ„ํ•ด ํ•„๋“œ์— ์‚ฌ์šฉํ•˜๋Š” ์–ด๋…ธํ…Œ์ด์…˜ - ์„ ์–ธ๋œ ํ•„๋“œ๋งˆ๋‹ค ์–ด๋…ธํ…Œ์ด์…˜ ์„ค์ • 
	//โ†’ bean ์—˜๋ฆฌ๋จผํŠธ์—์„œ autowire ์†์„ฑ๊ฐ’์„ [byType]์œผ๋กœ ์„ค์ •ํ•œ ๊ฒƒ๊ณผ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ์˜์กด์„ฑ ์ฃผ์ž… - Setter Injection
	//โ†’ Setter ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ์˜์กด๊ด€๊ณ„๋ฅผ ์„ค์ •ํ•˜์ง€๋งŒ Setter ๋ฉ”์†Œ๋“œ๋ฅผ ์ž‘์„ฑํ•˜์ง€ ์•Š์•„๋„ ์˜์กด์„ฑ ์ฃผ์ž…
	//๋ฌธ์ œ์ )ํ•„๋“œ์˜ ์ž๋ฃŒํ˜•๊ณผ ๊ฐ™์€ ์ž๋ฃŒํ˜•์˜ Spring Bean์ด 2๊ฐœ ์ด์ƒ ์กด์žฌํ•  ๊ฒฝ์šฐ ์˜์กด์„ฑ ์ฃผ์ž… ์‹คํŒจ - NoUniqueBeanDefinitionException ๋ฐœ์ƒ
	//ํ•ด๊ฒฐ๋ฒ•-1)ํ•„๋“œ์˜ ์ž๋ฃŒํ˜•๊ณผ ๊ฐ™์€ ์ž๋ฃŒํ˜•์˜ Spring Bean์ด 2๊ฐœ ์ด์ƒ ์กด์žฌํ•  ๊ฒฝ์šฐ ํ•„๋“œ์— ์ €์žฅ๋  Spring Bean์˜ ์‹๋ณ„์ž(beanName)๋ฅผ ํ•„๋“œ๋ช…๊ณผ ๊ฐ™์€ ์ด๋ฆ„์œผ๋กœ ๋ณ€๊ฒฝ
	//โ†’ @Autowired ์–ด๋…ธํ…Œ์ด์…˜์€ ํ•„๋“œ์˜ ์ž๋ฃŒํ˜•๊ณผ ๊ฐ™์€ ์ž๋ฃŒํ˜•์˜ Spring Bean์ด 2๊ฐœ ์ด์ƒ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ
	//	bean ์—˜๋ฆฌ๋จผํŠธ์—์„œ autowire ์†์„ฑ๊ฐ’์„ [byName]์œผ๋กœ ์„ค์ •ํ•œ ๊ฒƒ๊ณผ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ์˜์กด์„ฑ ์ฃผ์ž…
	@Autowired
	//ํ•ด๊ฒฐ๋ฒ•-2)ํ•„๋“œ์˜ ์ž๋ฃŒํ˜•๊ณผ ๊ฐ™์€ ์ž๋ฃŒํ˜•์˜ Spring Bean์ด 2๊ฐœ ์ด์ƒ ์กด์žฌํ•  ๊ฒฝ์šฐ ํ•„๋“œ์— ์ €์žฅ๋  Spring Bean์— ๋Œ€ํ•œ ํด๋ž˜์Šค์— @Primary ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ์ž‘์„ฑ
	//ํ•ด๊ฒฐ๋ฒ•-3)ํ•„๋“œ์— @Qualifier ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ์˜์กด์„ฑ ์ฃผ์ž…์„ ์œ„ํ•œ Spring Bean ์ง€์ •
	//โ†’ @Primary ์–ด๋…ธํ…Œ์ด์…˜๊ณผ @Qualifier ์–ด๋…ธํ…Œ์ด์…˜์ด ๊ฐ™์ด ์„ค์ •๋œ ๊ฒฝ์šฐ @Qualifier ์–ด๋…ธํ…Œ์ด์…˜์œผ๋กœ ์˜์กด์„ฑ ์ฃผ์ž…
	//@Qualifier : ํ•„๋“œ์™€ ์˜์กด๊ด€๊ณ„๊ฐ€ ์„ค์ •๋  Spring Bean๋ฅผ ์ง์ ‘ ์ง€์ •ํ•˜๊ธฐ ์œ„ํ•œ ์–ด๋…ธํ…Œ์ด์…˜
	//โ†’ @Autowired ์–ด๋…ธํ…Œ์ด์…˜์— ์ข…์†๋œ ์–ด๋…ธํ…Œ์ด์…˜
	//value ์†์„ฑ : ์˜์กด์„ฑ ์ฃผ์ž…์„ ์œ„ํ•œ Spring Bean์˜ ์‹๋ณ„์ž(beanName)๋ฅผ ์†์„ฑ๊ฐ’์œผ๋กœ ์„ค์ •
	//โ†’ ๋‹ค๋ฅธ ์†์„ฑ์ด ์—†๋Š” ๊ฒฝ์šฐ ์†์„ฑ๊ฐ’๋งŒ ์„ค์ • ๊ฐ€๋Šฅ
	//@Qualifier("annotationStudentJdbcDAO")
	@Qualifier("annotationStudentMybatisDAO")
	private StudentDAO studentDAO;
	//
	//@Autowired ์–ด๋…ธํ…Œ์ด์…˜ ๋Œ€์‹  @Resource ์–ด๋…ธํ…Œ์ด์…˜ ๋˜๋Š” @Inject ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ์˜์กด์„ฑ ์ฃผ์ž… ๊ฐ€๋Šฅ
	//โ†’ @Autowired ์–ด๋…ธํ…Œ์ด์…˜์€ Spring Framework์˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ์ œ๊ณตํ•˜๋Š” ์–ด๋…ธํ…Œ์ด์…˜์ด์ง€๋งŒ  
	//@Resource ์–ด๋…ธํ…Œ์ด์…˜ ๋˜๋Š” @Inject ์–ด๋…ธํ…Œ์ด์…˜์€ JDK ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ์ œ๊ณตํ•˜๋Š” ์–ด๋…ธํ…Œ์ด์…˜
	//โ†’ @Resource ์–ด๋…ธํ…Œ์ด์…˜ ๋˜๋Š” @Inject ์–ด๋…ธํ…Œ์ด์…˜์€ ๋‹ค๋ฅธ Framework์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
	//@Resource : bean ์—˜๋ฆฌ๋จผํŠธ์—์„œ autowire ์†์„ฑ๊ฐ’์„ [byName]์œผ๋กœ ์„ค์ •ํ•œ ๊ฒƒ๊ณผ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ์˜์กด์„ฑ ์ฃผ์ž…
	//@Inject : bean ์—˜๋ฆฌ๋จผํŠธ์—์„œ autowire ์†์„ฑ๊ฐ’์„ [byType]์œผ๋กœ ์„ค์ •ํ•œ ๊ฒƒ๊ณผ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ์˜์กด์„ฑ ์ฃผ์ž…
	//
	public AnnotationStudentServiceImpl() {
		System.out.println("### AnnotationStudentServiceImpl ํด๋ž˜์Šค์˜ ๊ธฐ๋ณธ ์ƒ์„ฑ์ž ํ˜ธ์ถœ ###");
	}
	@Override
	public void addStudent(Student student) {
		System.out.println("*** AnnotationStudentServiceImpl ํด๋ž˜์Šค addStudent(Student student) ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ ***");
		studentDAO.insertStudent(student);
	}
	@Override
	public void modifyStudent(Student student) {
		System.out.println("*** AnnotationStudentServiceImpl ํด๋ž˜์Šค modifyStudent(Student student) ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ ***");
		studentDAO.updateStudent(student);
	}
	@Override
	public void removeStudent(int num) {
		System.out.println("*** AnnotationStudentServiceImpl ํด๋ž˜์Šค removeStudent(int num) ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ ***");
		studentDAO.deleteStudent(num);
	}
	@Override
	public Student getStudent(int num) {
		System.out.println("*** AnnotationStudentServiceImpl ํด๋ž˜์Šค getStudent(int num) ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ ***");
		return studentDAO.selectStudent(num);
	}
	@Override
	public List<Student> getStudentList() {
		System.out.println("*** AnnotationStudentServiceImpl ํด๋ž˜์Šค getStudentList() ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ ***");
		return studentDAO.selectStudentList();
	}
}

๐ŸŒˆํ”„๋กœ๊ทธ๋žจ ์ƒ์„ฑ

๐Ÿ“ƒAnnotationStudentApp.java(ํด๋ž˜์Šค)

โ€ป xyz.itwill05.di ํŒจํ‚ค์ง€์— AnnotationStudentApp.java ํด๋ž˜์Šค ์ƒ์„ฑ

package xyz.itwill05.di;
//
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
//
public class AnnotationStudentApp {
	public static void main(String[] args) {
		System.out.println("================== Spring Container ์ดˆ๊ธฐํ™” ์ „ ==================");
		ApplicationContext context=new ClassPathXmlApplicationContext("05-4_diAnnotation.xml");
		System.out.println("================== Spring Container ์ดˆ๊ธฐํ™” ํ›„ ==================");
		StudentService service=context.getBean("studentService",StudentService.class);
		service.addStudent(null);
		System.out.println("================================================================");
		((ClassPathXmlApplicationContext)context).close();		
	}
}

๐ŸŒˆํ™˜๊ฒฝ์„ค์ •ํŒŒ์ผ ์ž‘์„ฑ

๐Ÿ“ขAnnotation์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” context File๋„ ์žˆ์–ด์•ผ ์‚ฌ์šฉ๊ฐ€๋Šฅ

๐Ÿ“ƒ05-4_diAnnotation.xml

โ€ป src/main/resources ํด๋”์— 05-4_diAnnotation.xml ์ƒ์„ฑ

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
	<!-- ================================================================================ -->
	<context:component-scan base-package="xyz.itwill05.di"/>
</beans>

๐ŸŒˆMybatisDAO ํด๋ž˜์Šค ์ƒ์„ฑ

๐Ÿ“ƒAnnotationStudentMybatisDAO.java

โ€ป xyz.itwill05.di ํŒจํ‚ค์ง€์— AnnotationStudentMybatisDAO.java ํด๋ž˜์Šค ์ƒ์„ฑ

package xyz.itwill05.di;
//
import java.util.List;
//
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Repository;
//
//@Repository("studentDAO")
@Repository
@Primary
public class AnnotationStudentMybatisDAO implements StudentDAO {
	public AnnotationStudentMybatisDAO() {
		System.out.println("### AnnotationStudentMybatisDAO ํด๋ž˜์Šค์˜ ๊ธฐ๋ณธ ์ƒ์„ฑ์ž ํ˜ธ์ถœ ###");
	}
	@Override
	public int insertStudent(Student student) {
		System.out.println("*** AnnotationStudentMybatisDAO ํด๋ž˜์Šค์˜ insertStudent(Student student) ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ ***");
		return 0;
	}
	@Override
	public int updateStudent(Student student) {
		System.out.println("*** AnnotationStudentMybatisDAO ํด๋ž˜์Šค์˜ updateStudent(Student student) ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ ***");
		return 0;
	}
	@Override
	public int deleteStudent(int num) {
		System.out.println("*** AnnotationStudentMybatisDAO ํด๋ž˜์Šค์˜ deleteStudent(int num) ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ ***");
		return 0;
	}
	@Override
	public Student selectStudent(int num) {
		System.out.println("*** AnnotationStudentMybatisDAO ํด๋ž˜์Šค์˜ selectStudent(int num) ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ ***");
		return null;
	}
	@Override
	public List<Student> selectStudentList() {
		System.out.println("*** AnnotationStudentMybatisDAO ํด๋ž˜์Šค์˜ selectStudentList() ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ ***");
		return null;
	}
}

0๊ฐœ์˜ ๋Œ“๊ธ€