TIL 2021.09.29(수) Spring DI 의존성 주입하기

개발중·2021년 9월 30일
0

Spring

목록 보기
2/11

프러퍼티는 set 메서드가 없으면 에러가 난다.

scope를 안 써 주면 기본인 싱글톤으로 생성된다.

destory 메서드는 출력되지 않는다.
init 메서드는
메서드가 종료된 후에 실행되기 때문이다.

![](https://images.velog.io/images/moodnightsummer/post/b9fba1b5-0899-46ca-a5bb-43f58204900f/%EC%8A%A4%ED%81%AC%EB%A6%B0%EC%83%B7%202021-09-29%20%EC%98%A4%ED%9B%84%2012.40.14.png

<?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:p="http://www.springframework.org/schema/p"
	xmlns:util="http://www.springframework.org/schema/util"
	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-4.3.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
<bean id="book1" class="sist.com.di.basic2.Book" scope="singleton">
	<property name="bookName" value="spring"></property>
	<property name="author" value="가가가"></property>
	<property name="price" value="7000"></property>
	<property name="publihser" value="가출판"></property>
</bean>
<bean id="book2" class="sist.com.di.basic2.Book" scope="singleton" p:bookName="java" p:author="나나나" p:price="8000" p:publihser="나출판"></bean>

<bean id="book3" class="sist.com.di.basic2.Book" scope="singleton">
	<property name="bookName" value="Oracle"></property>
	<property name="author" value="마마마"></property>
	<property name="price" value="9000"></property>
	<property name="publihser" value="마출판"></property>
</bean>
<!-- 의존성 주입 -->
<bean id="store1" class="sist.com.di.basic2.Store" scope="singleton">
	<property name="list">
		<list>
			<ref bean="book1"/>
			<ref bean="book2"/>
			<ref bean="book3"/>
		</list>
	</property>
	<property name="set">
		<set>
			<ref bean="book1"/>
			<ref bean="book2"/>
			<ref bean="book3"/>
		</set>
	</property>
	<property name="map">
		<map>
			<entry>
				<key>
					<value type="java.lang.Integer">1</value> <!-- 키의 값 -->
				</key>
				<ref bean="book1"></ref> <!-- 1번의 값 -->
			</entry> <!--키 없이 밸류를 가져올 수 있는 것이다 -->
		</map>
	</property>
</bean>


<bean id="store2" class="sist.com.di.basic2.Store" scope="singleton">
<property name="list">
		<list>
			<ref bean="book1"/>
			<ref bean="book1"/>
			<ref bean="book1"/>
		</list>
	</property>
		<property name="set">
		<set>
			<ref bean="book1"/>
			<ref bean="book1"/>
			<ref bean="book1"/>
		</set>
	</property>
</bean>

</beans>

의존성을 주입하여 준다.

bean에 class에 넣어 준 경로에 있는 클래스에 값을 넣어 준다.

package sist.com.di.basic2;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class Store {  
	
	private List<Book> list; // 스토어는 리스트를 의존하고 있다.
	private Set<Book> set; // 스토어는 셋을 의존하고 있다.
	private Map<Integer, Book>map; // 스토어는 맵을 의존하고 있다.
	private Properties properties;// 스토어는 프로퍼티스를 의존하고 있다.
	public List<Book> getList() {
		return list;
	}
	public void setList(List<Book> list) {
		this.list = list;
	}
	public Set<Book> getSet() {
		return set;
	}
	public void setSet(Set<Book> set) {
		this.set = set;
	}
	public Map<Integer, Book> getMap() {
		return map;
	}
	public void setMap(Map<Integer, Book> map) {
		this.map = map;
	}
	public Properties getProperties() {
		return properties;
	}
	public void setProperties(Properties properties) {
		this.properties = properties;
	}
}

Store 클래스를 통해 bookdi에서 의존성 주입으로 생성한 값들을 list, set, map으로 또 의존성을 주입한 것이다.

  • 나중에 추가로 볼 것.

의존성 주입에 대한 잘 정리돼 있는 블로그 글
https://battlewithmyself.tistory.com/112

Spring의 구조-Spring Framework의 동작원리
https://hello-walnuty.tistory.com/16

profile
공부한 것 정리하는 개발 입문자

0개의 댓글