Spring

230130 Spring DI 설정 방법-생성자, setter를 통한 의존성 주입

주영재 2023. 1. 30. 19:39


DI설정 방법

2가지 의존성 주입 방법
xml파일에서...


1.생성자를 통한 의존성 주입
ref는 객체가 들어간다

<constructor-arg ref="빈id"/>



2.setter를 통한 의존성 주입
변수명은 멤버변수와 동일

<property name="변수명" value="값/>  <!--값은 내가 넣고 싶은 값-->
<property name="변수명" ref="객체"/>  <!--객체는 빈id가 들어갈 수 있다.-->

 


application-context.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 http://www.springframework.org/schema/beans/spring-beans.xsd">

	<!-- 생성자 통한 주입 -->
	<bean id="chef" class="ex02.Chef"/>
	<bean id="hotel" class="ex02.Hotel">
		<constructor-arg ref="chef"/>
	</bean>
	
	<!-- 세터를 통한 주입 -->
	<bean id="dev" class="ex02.DatabaseDev">
		<property name="url" value="데이터베이스주소"/>
		<property name="uid" value="데이터베이스계정명"/>
		<property name="upw" value="데이터베이스비밀번호"/>
	</bean>
	
	<bean id="dao" class="ex02.MemberDAO">
		<property name="databaseDev" ref="dev"/>
	</bean>

</beans>

-세터를통한주입 : 주입이 두번 들어간 것


생성자 통한 주입

Chef.java

package ex02;

public class Chef {
	
	public void cooking() {
		System.out.println("요리하기 기능");
	}
}

 

Hotel.java

package ex02;

public class Hotel {
	
	private Chef chef;
	
	//constructor
	public Hotel(Chef chef) {
		this.chef = chef;
	}

	//get
	public Chef getChef() {
		return chef;
	}
	
	
	
}

 

 


세터 통한 주입

DatabaseDev.java

package ex02;

public class DatabaseDev {
	private String url;
	private String Uid;
	private String upw;
	
	
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getUid() {
		return Uid;
	}
	public void setUid(String uid) {
		Uid = uid;
	}
	public String getUpw() {
		return upw;
	}
	public void setUpw(String upw) {
		this.upw = upw;
	}

}

 

 

MemberDAO.java

package ex02;

public class MemberDAO {
	
	private DatabaseDev databaseDev;
	
	//get
	public DatabaseDev getDatabaseDev() {
		return databaseDev;
	}

	//set
	public void setDatabaseDev(DatabaseDev databaseDev) {
		this.databaseDev = databaseDev;
	}
}

 

 


MainClass

package ex02;

import org.springframework.context.support.GenericXmlApplicationContext;

public class MainClass {

	public static void main(String[] args) {

		GenericXmlApplicationContext ctx=new GenericXmlApplicationContext("application-context.xml");

		//생성자를 통한 주입확인
		Hotel hotel=ctx.getBean(Hotel.class);
		hotel.getChef().cooking();

		//세터를 통한 주입확인
		MemberDAO dao=ctx.getBean("dao", MemberDAO.class);

		DatabaseDev dev=dao.getDatabaseDev();
		System.out.println(dev.getUrl());
		System.out.println(dev.getUid());
		System.out.println(dev.getUpw());
		

	}
}

 

콘솔결과창

1월 30, 2023 7:38:05 오후 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [application-context.xml]
1월 30, 2023 7:38:05 오후 org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.GenericXmlApplicationContext@402a079c: startup date [Mon Jan 30 19:38:05 KST 2023]; root of context hierarchy
요리하기 기능
데이터베이스주소
데이터베이스계정명
데이터베이스비밀번호