Spring - DI

yeong ·2023년 2월 14일
0

spring

목록 보기
7/8
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">

	<!-- Spring Bean으로 등록된 클래스의 기본 생성자를 이용하여 객체 생성 -->
	<!-- => 객체의 필드에는 기본값(숫자형 : 0, 논리형 : false, 참조형 : null) 저장 -->
	<bean class="xyz.itwill05.di.Student" id="student1"/>
	
	<!-- Spring Injection : 스프링 컨테이너에 의해 Spring Bean Configuration File에 등록된
	클래스로 객체(Spring Bean) 생성시 필드에 원하는 값 또는 객체를 저장되도록 설정 -->
	<!-- => 생성자(Constructor Injection) 또는 Setter 메소드(Setter Injection)를 이용하여 값 
	또는 객체를 필드에 저장 -->
	
	<!-- Spring Bean으로 등록된 클래스의 매개변수가 선언된 생성자를 이용하여 객체 생성 -->
	<!-- => bean 엘리먼트의 하위 엘리먼트를 사용하여 생성자 매개변수에 값을 전달하여 필드값으로 저장 -->
	<!-- => Constructor Injection : 생성자를 이용하여 객체 필드 초기화 작업 실행 -->
	<bean class="xyz.itwill05.di.Student" id="student2">
		<!-- constructor-arg : Spring Bean으로 등록된 클래스의 생성자 매개변수에 값(객체)을 
		전달하기 위한 엘리먼트 -->
		<!-- => constructor-arg 엘리먼트의 갯수만큼 매개변수가 선언된 생성자를 반드시 작성 -->
		<!-- value 속성 : 매개변수에 전달하기 위한 값을 속성값으로 설정 -->
		<!-- => Spring Bean으로 등록된 클래스가 객체로 생성될 때 필드에 전달값 저장 -->
		<!-- => Value Injection : 객체의 필드에 값이 저장되도록 초기화 작업 실행 - 값 주입 -->
		<!-- => 전달값은 기본적으로 문자열(String 객체)로 전달 - 매개변수의 자료형에 의해 자동 형변환 -->
		<!-- => 매개변수의 자료형에 의해 자동 형변환될 경우 NumberFormatException 발생 가능 -->
		<constructor-arg value="1000"/>
	</bean>
	
	<!-- constructor-arg 엘리먼트의 작성순서에 의해 매개변수에 값(객체)이 전달되어 객체 초기화 -->
	<!-- 
	<bean class="xyz.itwill05.di.Student" id="student3">
		<constructor-arg value="2000"/>
		<constructor-arg value="홍길동"/>
		<constructor-arg value="abc@itwill.xyz"/>
	</bean>
	-->
	
	<bean class="xyz.itwill05.di.Student" id="student3">
		<!-- index 속성 : 생성자 매개변수에 값(객체)을 전달하기 위한 순서를 속성값으로 설정 -->
		<!-- => index 속성값은 0부터 1씩 증가되는 정수값 사용 -->		
		<constructor-arg value="홍길동" index="1"/>
		<constructor-arg value="abc@itwill.xyz" index="2"/>
		<constructor-arg value="2000" index="0"/>
	</bean>
	
	<!-- 클래스의 기본 생성자를 이용하여 객체 생성 - 객체 필드에는 기본값 저장 -->
	<!-- => 하위 엘리먼트 사용하여 Setter 메소드를 호출해 필드값 변경 - Setter Injection -->
	<bean class="xyz.itwill05.di.Student" id="student4">
		<!-- property : 객체의 Setter 메소드를 호출하여 필드값을 변경하는 엘리먼트 -->
		<!-- name 속성 : 필드값을 변경하기 위한 필드명을 속성값으로 설정 - 자동 완성 기능 사용 가능 -->
		<!-- => name 속성값으로 설정된 필드에 대한 Setter 메소드를 호출하여 필드값 변경 -->
		<!-- => 필드에 대한 Setter 메소드가 없거나 잘못 선언된 경우 예외 발생 -->
		<!-- value 속성 : 필드에 저장된 값을 속성값으로 설정 - 값 주입 -->
		<property name="num" value="3000"/>
		<property name="name" value="임꺽정"/>
		<property name="email" value="xyz@itwill.xyz"/>
	</bean>
	
	<!-- 생성자(Constructor Injection)와 Setter 메소드(Setter Injection)를 같이 사용하여 
	객체 초기화 작업 가능 -->
	<bean class="xyz.itwill05.di.Student" id="student5">
		<constructor-arg value="4000"/>
		<constructor-arg value="전우치"/>
		<property name="email" value="opq@itwill.xyz"/>
	</bean>
	
	<!-- PropertyPlaceholderConfigurer 클래스 : Properties 파일을 제공받아 파일에 설정된
	값을 Spring Bean Configuration File에서 사용할 수 있도록 제공하는 클래스 -->
	<!-- => locations 필드에 Properties 파일의 경로를 전달하여 저장 -->
	<!-- => Properties 파일에 의해 제공되는 값은 Spring Bean Configuration File에서 ${이름}으로 사용 -->
	<!--  
	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations" value="xyz/itwill05/di/student.properties"/>
	</bean>
	-->
	
	<!-- Spring 5.2 이상에서는 PropertySourcesPlaceholderConfigurer 클래스를 사용하여
	Properties 파일을 제공받아 Spring Bean Configuration File에서 사용할 수 있도록 변경 -->
	<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
		<property name="locations" value="xyz/itwill05/di/student.properties"/>
	</bean>
	
	<!-- Properties 파일에 의해 제공되는 값을 사용하여 객체 필드 초기화 작업 -->
	<bean class="xyz.itwill05.di.Student" id="student6">
		<property name="num" value="${num}"/>
		<property name="name" value="${name}"/>
		<property name="email" value="${email}"/>
	</bean>
	
	<!-- StudentDAO 인터페이스를 상속받아 자식클래스를 Spring Bean으로 등록 -->
	<bean class="xyz.itwill05.di.StudentJdbcDAO" id="studentJdbcDAO"/>
	<bean class="xyz.itwill05.di.StudentMybatisDAO" id="studentMybatisDAO"/>

	<!-- StudentService 인터페이스를 상속받아 자식클래스를 Spring Bean으로 등록 -->
	<!-- => 클래스의 기본 생성자를 이용하여 객체 생성 - 객체 필드에는 기본값 저장 -->
	<!-- 문제점)StudentServiceImpl 클래스로 생성된 객체의 필드에는 [null]이 저장되어 StudentServiceImpl
	클래스의 메소드에서 StudentDAO 클래스의 메소드를 호출하면 NullPointerExcetion 발생 - 의존관계 미성립 -->
	<!-- 해결법)StudentServiceImpl 클래스의 객체 필드에 StudentDAO 인터페이스를 상속받은
	자식클래스의 객체가 저장되도록 설정 - 의존관계 성립 -->
	<!-- <bean class="xyz.itwill05.di.StudentServiceImpl" id="studentServiceImpl"/> -->

	<!-- StudentServiceImpl 클래스의 매개변수가 선언된 생성자를 이용하여 객체 생성 -->
	<!-- => 생성자 매개변수에 StudentDAO 인터페이스를 상속받은 자식클래스의 객체를 전달하여
	필드에 저장 - Constructor Injection -->
	<!-- constructor-arg 엘리먼트를 사용하여 StudentServiceImpl 클래스의 객체 필드에  
	StudentDAO 인터페이스를 상속받은 자식클래스의 객체 저장 - 의존관계 성립 -->
	<!-- ref 속성 : 스프링 컨테이너로 관리되는 Spring Bean의 식별자를 속성값으로 설정 -->
	<!-- => 스프링 컨테이너로 관리되는 Spring Bean을 객체 필드에 저장 - 의존성 주입(DI : Dependency Injection) -->
	<!--  
	<bean class="xyz.itwill05.di.StudentServiceImpl" id="studentServiceImpl">
		<constructor-arg ref="studentJdbcDAO"/>
	</bean>
	-->
	
	<!-- StudentServiceImpl 클래스의 기본 생성자를 이용하여 객체를 생성 -->
	<!-- => Setter 메소드를 호출하여 StudentDAO 인터페이스를 상속받은 자식클래스의 객체를
	필드에 저장 => Setter Injection -->
	<!-- property 엘리먼트를 사용하여 StudentServiceImpl 클래스의 객체 필드에 StudentDAO 
	인터페이스를 상속받은 자식클래스의 객체 저장 - 의존관계 성립 -->
	<!--  
	<bean class="xyz.itwill05.di.StudentServiceImpl" id="studentServiceImpl">
		<property name="studentDAO" ref="studentJdbcDAO"/>
	</bean>
	-->
	
	<!-- 기존에 사용하던 StudentJdbcDAO 클래스 대신 새롭게 작성한 StudentMybatisDAO 클래스로
	의존관계를 변경하고자 할 경우 ref 속성값만 변경 -->
	<!-- => 기존 클래스 대신 새로운 클래스로 바꿔도 관계가 설정된 클래스를 변경하지 않고
	Spring Bean Configuration File만 수정해도 의존관계 변경 - 유지보수의 효율성 증가 -->
	<bean class="xyz.itwill05.di.StudentServiceImpl" id="studentServiceImpl">
		<property name="studentDAO" ref="studentMybatisDAO"/>
	</bean>
</beans>
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">

	<!-- Controller 인터페이스를 상속받은 자식클래스를 Spring Bean으로 등록 -->
	<bean class="xyz.itwill05.di.LoginController" id="loginController"/>
	<bean class="xyz.itwill05.di.LogoutController" id="logoutController"/>
	<bean class="xyz.itwill05.di.ListController" id="listController"/>

	<bean class="xyz.itwill05.di.CollectionBean" id="collectionBean">
		<property name="nameSet">
			<!-- set : Set 객체를 생성하여 필드에 저장하기 위한 엘리먼트 -->
			<set>
				<!-- value : Collection 객체에 요소값을 추가하는 엘리먼트 -->
				<value>홍길동</value>
				<value>임꺽정</value>
				<value>전우치</value>
				<value>홍길동</value>
			</set>
		</property>
		
		<property name="nameList">
			<!-- list : List 객체를 생성하여 필드에 저장하기 위한 엘리먼트 -->
			<list>
				<value>홍길동</value>
				<value>임꺽정</value>
				<value>전우치</value>
				<value>홍길동</value>
			</list>
		</property>
		
		<property name="controllerSet">
			<set>
				<!-- Collection 객체의 요소로 Spring Bean으로 등록된 클래스의 객체를 추가하기 위한 엘리먼트 -->
				<!-- bean 속성 : 요소로 추가될 Spring Bean의 식별자를 속성값으로 설정 - 자동 완성 기능 사용 가능 -->
				<ref bean="loginController"/>
				<ref bean="logoutController"/>
				<ref bean="listController"/>
			</set>
		</property>
		
		<property name="controllerList">
			<list>
				<ref bean="loginController"/>
				<ref bean="logoutController"/>
				<ref bean="listController"/>
			</list>
		</property>
		
		<property name="controllerMap">
			<!-- map : Map 객체를 생성하여 필드에 저장하기 위한 엘리먼트 -->
			<map>
				<!-- entry : Map 객체에 엔트리(Entry - Key & Value)를 추가하기 위한 엘리먼트 -->
				<entry>
					<!-- key : 엔트리의 맵키(String)를 설정하기 위한 엘리먼트 -->
					<key>
						<value>login</value>
					</key> 
					<!-- ref : 엔트리의 맵값(Controller 객체)를 설정하기 위한 엘리먼트 -->
					<ref bean="loginController"/>
				</entry>
				<entry>
					<key>
						<value>logout</value>
					</key> 
					<ref bean="logoutController"/>
				</entry>
				<entry>
					<key>
						<value>list</value>
					</key> 
					<ref bean="listController"/>
				</entry>
			</map>
		</property>
		
		<property name="controllerProperties">
			<!-- props : Properties 객체를 생성하여 필드에 저장하기 위한 엘리먼트 -->
			<!-- => 필드의 자료형 Map<String,String>인 경우 props 엘리먼트로 객체 필드에 
			Map 객체를 생성하여 저장 가능 -->		
			<props>
				<!-- prop : Properties 객체에 엔트리를 추가하는 메소드 -->
				<!-- => 엘리먼트의 내용으로 엔트리에 저장될 값(문자열)을 설정 -->
				<!-- key 속성 : 엔트리를 구분하기 위한 식별자(문자열)를 속성값으로 설정 -->
				<prop key="login">xyz.itwill05.di.LoginController</prop>
				<prop key="logout">xyz.itwill05.di.LogoutController</prop>
				<prop key="list">xyz.itwill05.di.ListController</prop>
			</props>
		</property>
	</bean>
</beans>

0개의 댓글