📃AutoWireApp.java(클래스)
※ xyz.itwill05.di 패키지에 AutoWireApp.java 클래스 생성
package xyz.itwill05.di; // import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; // public class AutoWireApp { public static void main(String[] args) { System.out.println("================== Spring Container 초기화 전 =================="); ApplicationContext context=new ClassPathXmlApplicationContext("05-3_autowire.xml"); System.out.println("================== Spring Container 초기화 후 =================="); StudentService service=context.getBean("studentService", StudentService.class); service.addStudent(null); System.out.println("================================================================"); ((ClassPathXmlApplicationContext)context).close(); } }
📢의존성 주입을 수동으로 하는 방법(권장)
📌StudentService 인터페이스를 상속받은 자식클래스를 Spring Bean으로 등록
→ StudentServiceImpl 클래스의 studentDAO 필드에 StudentDAO 인터페이스를 상속받은 자식클래스의 객체(Spring Bean)가 저장되도록 의존성 주입 - 의존관계 성립
→ 의존성 주입을 하지 않으면 StudentServiceImpl 클래스의 메소드에서 StudentDAO 인터페이스를 상속받은 자식클래스의 메소드를 호출할 경우 NullPointException 발생
📌property 엘리먼트를 사용하여 studentDAO 필드에 StudentDAO 인터페이스를 상속받은 자식 클래스의 객체(Spring Bean)가 저장되도록 설정 - Setter Injection📢의존성 주입을 자동으로 하는 방법
📌autowire 속성 : no(기본), byName, byType, constructor 중 하나를 속성값으로 설정
→ 스프링 컨테이너가 Spring Bean의 의존관계를 자동으로 구현되도록 설정하는 속성
📍no 속성값 : 자동으로 의존관계를 구현하는 기능 미사용<!-- <bean class="xyz.itwill05.di.StudentServiceImpl" id="studentService" autowire="no"> <property name="studentDAO" ref="studentJdbcDAO"/> </bean> -->📍byName 속성값 : Spring Bean으로 등록된 클래스의 필드명과 같은 식별자(beanName)로 선언된 Spring Bean을 제공받아 필드에 저장되도록 의존성 주입 - Setter Injection
→ 필드명과 같은 이름의 식별자(beanName)로 선언된 Spring Bean이 없는 경우 의존성 미주입 - NullPointException 발생<!-- <bean class="xyz.itwill05.di.StudentServiceImpl" id="studentService" autowire="byName"/> -->📍byType 속성값 : Spring Bean으로 등록된 클래스의 필드와 같은 자료형의 Spring Bean을 제공받아 필드에 저장되도록 의존성 주입 - Setter Injection
→ 필드의 자료형이 인터페이스인 경우 인터페이스를 상속받은 자식클래스로 등록된 Spring Bean을 제공받아 의존성 주입
→ 필드와 같은 자료형의 Spring Bean이 2개 이상 등록된 경우 의존성 주입 실패 - NoUniqueBeanDefinitionException 발생<!-- <bean class="xyz.itwill05.di.StudentServiceImpl" id="studentService" autowire="byType"/> -->📍constructor 속성값 : Spring Bean으로 등록된 클래스의 필드명과 같은 식별자(beanName)로 선언된 Spring Bean을 제공받아 필드에 저장되도록 의존성 주입 - Constructor Injection
→ 필드의 자료형이 인터페이스인 경우 인터페이스를 상속받은 자식클래스로 등록된 Spring Bean을 제공받아 의존성 주입
→ 필드와 같은 자료형의 Spring Bean이 2개 이상 등록된 경우 기본 생성자로 객체 생성<bean class="xyz.itwill05.di.StudentServiceImpl" id="studentService" autowire="constructor"/>📃05-3_autowire.xml
※ src/main/resources 폴더에 05-3_autowire.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" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- ================================================================================ --> <!-- StudentDAO 인터페이스를 상속받은 자식클래스를 Spring Bean으로 등록 --> <bean class="xyz.itwill05.di.StudentJdbcDAO" id="studentJdbcDAO"/> <!-- <bean class="xyz.itwill05.di.StudentJdbcDAO" id="studentDAO"/> --> <bean class="xyz.itwill05.di.StudentMybatisDAO" id="studentMybatisDAO"/> <!-- ================================================================================ --> <!-- StudentService 인터페이스를 상속받은 자식클래스를 Spring Bean으로 등록 --> <!-- → StudentServiceImpl 클래스의 studentDAO 필드에 StudentDAO 인터페이스를 상속받은 자식클래스의 객체(Spring Bean)가 저장되도록 의존성 주입 - 의존관계 성립 --> <!-- → 의존성 주입을 하지 않으면 StudentServiceImpl 클래스의 메소드에서 StudentDAO 인터페이스를 상속받은 자식클래스의 메소드를 호출할 경우 NullPointException 발생 --> <!-- property 엘리먼트를 사용하여 studentDAO 필드에 StudentDAO 인터페이스를 상속받은 자식 클래스의 객체(Spring Bean)가 저장되도록 설정 - Setter Injection --> <!-- ◈의존성 주입을 수동으로 하는 방법(권장) --> <!-- <bean class="xyz.itwill05.di.StudentServiceImpl" id="studentService"> <property name="studentDAO" ref="studentJdbcDAO"/> </bean> --> <!-- ================================================================================ --> <!-- ◈의존성 주입을 자동으로 하는 방법 --> <!-- autowire 속성 : no(기본), byName, byType, constructor 중 하나를 속성값으로 설정 --> <!-- → 스프링 컨테이너가 Spring Bean의 의존관계를 자동으로 구현되도록 설정하는 속성 --> <!-- no 속성값 : 자동으로 의존관계를 구현하는 기능 미사용 --> <!-- <bean class="xyz.itwill05.di.StudentServiceImpl" id="studentService" autowire="no"> <property name="studentDAO" ref="studentJdbcDAO"/> </bean> --> <!-- byName 속성값 : Spring Bean으로 등록된 클래스의 필드명과 같은 식별자(beanName)로 선언된 Spring Bean을 제공받아 필드에 저장되도록 의존성 주입 - Setter Injection --> <!-- → 필드명과 같은 이름의 식별자(beanName)로 선언된 Spring Bean이 없는 경우 의존성 미주입 - NullPointException 발생 --> <!-- <bean class="xyz.itwill05.di.StudentServiceImpl" id="studentService" autowire="byName"/> --> <!-- --> <!-- byType 속성값 : Spring Bean으로 등록된 클래스의 필드와 같은 자료형의 Spring Bean을 제공받아 필드에 저장되도록 의존성 주입 - Setter Injection --> <!-- → 필드의 자료형이 인터페이스인 경우 인터페이스를 상속받은 자식클래스로 등록된 Spring Bean을 제공받아 의존성 주입 --> <!-- → 필드와 같은 자료형의 Spring Bean이 2개 이상 등록된 경우 의존성 주입 실패 - NoUniqueBeanDefinitionException 발생 --> <!-- <bean class="xyz.itwill05.di.StudentServiceImpl" id="studentService" autowire="byType"/> --> <!-- --> <!-- constructor 속성값 : Spring Bean으로 등록된 클래스의 필드명과 같은 식별자(beanName)로 선언된 Spring Bean을 제공받아 필드에 저장되도록 의존성 주입 - Constructor Injection --> <!-- → 필드의 자료형이 인터페이스인 경우 인터페이스를 상속받은 자식클래스로 등록된 Spring Bean을 제공받아 의존성 주입 --> <!-- → 필드와 같은 자료형의 Spring Bean이 2개 이상 등록된 경우 기본 생성자로 객체 생성 --> <bean class="xyz.itwill05.di.StudentServiceImpl" id="studentService" autowire="constructor"/> </beans>