๐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; } }
๐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>
๐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; } }