Search
Duplicate
๐Ÿ˜€

05. DI(Dependency Injection)

ํƒœ๊ทธ

๋นˆ ์ƒ์„ฑ๋ฒ”์œ„

์‹ฑ๊ธ€ํ†ค ๋นˆ(Singleton Bean)

โ€ข
์Šคํ”„๋ง ๋นˆ์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์‹ฑ๊ธ€ํ†ค์œผ๋กœ ๋งŒ๋“ค์–ด์ง„๋‹ค.
โ€ข
๊ทธ๋Ÿฌ๋ฏ€๋กœ, ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์ œ๊ณตํ•˜๋Š” ๋ชจ๋“  ๋นˆ์˜ ์ธ์Šคํ„ด์Šค๋Š” ํ•ญ์ƒ ๋™์ผํ•˜๋‹ค.
โ€ข
์ปจํ…Œ์ด๋„ˆ๊ฐ€ ํ•ญ์ƒ ์ƒˆ๋กœ์šด ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋งŒ๋“ค๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ scope์„ prototype์œผ๋กœ ์„ค์ •ํ•ด์•ผ ํ•จ
โ—ฆ
prototype์œผ๋กœ ์„ค์ •ํ•˜๋Š” ๊ฒฝ์šฐ Bean์€ xml์„ ์ฝ๋Š” ์‹œ์ ์ด ์•„๋‹ˆ๋ผ, getBean ์„ ํ˜ธ์ถœํ•˜๋Š” ์‹œ์ ์— Bean์ด ์ƒ์„ฑ๋œ๋‹ค.

์Šคํ”„๋ง ๋นˆ ์„ค์ •

์Šคํ”„๋ง ๋นˆ ์„ค์ • : XML ๋ฌธ์„œ
โ€ข
XML ๋ฌธ์„œ ํ˜•ํƒœ๋กœ ๋นˆ์˜ ์„ค์ • ๋ฉ”ํƒ€์ •๋ณด๋ฅผ ๊ธฐ์ˆ 
โ€ข
๋‹จ์ˆœํ•˜๋ฉฐ ์‚ฌ์šฉํ•˜๊ธฐ ์‰ฌ์›€(์ง๊ด€์ ), ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹
โ€ข
<bean> ํƒœ๊ทธ๋ฅผ ํ†ตํ•ด ์„ธ๋ฐ€ํ•œ ์ œ์–ด๊ฐ€ ๊ฐ€๋Šฅ
<bean id ="kor" class="com.ssafy.hello.di4.HelloMessageKor" scope = "prototype"></bean> <bean id ="eng" class="com.ssafy.hello.di4.HelloMessageEng"></bean>
XML
๋ณต์‚ฌ
์Šคํ”„๋ง ๋นˆ ์„ค์ • : Annotaion
โ€ข
์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ทœ๋ชจ๊ฐ€ ์ปค์ง€๊ณ  ๋นˆ์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋งŽ์•„์งˆ ๊ฒฝ์šฐ XML ํŒŒ์ผ์„ ๊ด€๋ฆฌํ•˜๋Š”๊ฒƒ์ด ๋ฒˆ๊ฑฐ๋กœ์›€
โ€ข
๋นˆ์œผ๋กœ ์‚ฌ์šฉ๋  ํด๋ž˜์Šค์— ํŠน๋ณ„ํ•œ annotation์„ ๋ถ€์—ฌํ•ด์ฃผ๋ฉด ์ž๋™์œผ๋กœ ๋นˆ ๋“ฑ๋ก ๊ฐ€๋Šฅ
โ€ข
"์˜ค๋ธŒ์ ํŠธ ๋นˆ ์Šค์บ๋„ˆ"๋กœ "๋นˆ ์Šค์บ๋‹"์„ ํ†ตํ•ด ์ž๋™ ๋“ฑ๋ก
โ—ฆ
๋นˆ ์Šค์บ๋„ˆ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ํด๋ž˜์Šค ์ด๋ฆ„์„ ๋นˆ์˜ ์•„์ด๋””๋กœ ์‚ฌ์šฉ
โ—ฆ
์ •ํ™•ํžˆ๋Š” ํด๋ž˜์Šค ์ด๋ฆ„์˜ ์ฒซ ๊ธ€์ž๋งŒ ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊พผ ๊ฒƒ์„ ์‚ฌ์šฉ
โ—ฆ
๋ฌผ๋ก  ๋‚ด๊ฐ€ ์ž„์˜๋กœ ๋งŒ๋“ค์–ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Œ
โ€ข
Annotaion์œผ๋กœ ๋นˆ์„ ์„ค์ • ํ•  ๊ฒฝ์šฐ ๋ฐ˜๋“œ์‹œ component-scan์„ ์„ค์ •ํ•ด์•ผ ํ•œ๋‹ค.
โ—ฆ
Stereotype annotation ์ข…๋ฅ˜
โ–ช
๋นˆ ์ž๋™๋“ฑ๋ก์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” annotation
โ–ช
๋นˆ ์ž๋™์ธ์‹์„ ์œ„ํ•œ annotation์ด ์—ฌ๋Ÿฌ๊ฐœ์ธ ์ด์œ ?
โ€ข
๊ณ„์ธต๋ณ„๋กœ ๋นˆ์˜ ํŠน์„ฑ์ด๋‚˜ ์ข…๋ฅ˜๋ฅผ ๊ตฌ๋ถ„
โ€ข
AOP pointcut ํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•˜๋ฉด ํŠน์ • annotation์ด ๋‹ฌ๋ฆฐ ํด๋ž˜์Šค๋งŒ ์„ค์ • ๊ฐ€๋Šฅ
โ€ข
ํŠน์ • ๊ณ„์ธต์˜ ๋นˆ์— ๋ถ€๊ฐ€๊ธฐ๋Šฅ์„ ๋ถ€์—ฌ
@Component("memberService") @Scope("singleton") public class MemberServiceImpl implements MemberService { @Override public int registerMember(MemberDto memberDto){ return 0; } }
Java
๋ณต์‚ฌ

DI (Dependency Ingection)

โ€ข
๊ฐ์ฒด ๊ฐ„์˜ ์˜์กด๊ด€๊ณ„๋ฅผ ์ž์‹ ์ด ์•„๋‹Œ ์™ธ๋ถ€์˜ ์กฐ๋ฆฝ๊ธฐ๊ฐ€ ์ˆ˜ํ–‰
โ€ข
์ œ์–ด์˜ ์—ญํ–‰(inversion of Control, IoC) ์ด๋ผ๋Š” ์˜๋ฏธ๋กœ ์‚ฌ์šฉ
โ€ข
DI๋ฅผ ํ†ตํ•ด ์‹œ์Šคํ…œ์— ์žˆ๋Š” ๊ฐ ๊ฐ์ฒด๋ฅผ ์กฐ์ •ํ•˜๋Š” ์™ธ๋ถ€ ๊ฐ์ฒด๊ฐ€ ๊ฐ์ฒด๋“ค์—๊ฒŒ ์ƒ์„ฑ์‹œ์— ์˜์กด ๊ด€๊ณ„๋ฅผ ์คŒ
โ€ข
๋Š์Šจํ•œ ๊ฒฐํ•ฉ(loose coupling)์˜ ์ฃผ์š” ๊ฐ•์ 
โ—ฆ
๊ฐ์ฒด๋Š” ์ธํ„ฐํŽ˜์ด์Šค์— ์˜ํ•œ ์˜์กด ๊ด€๊ณ„๋งŒ์„ ์•Œ๊ณ  ์žˆ์œผ๋ฉฐ, ์ด ์˜์กด ๊ด€๊ณ„๋Š” ๊ตฌํ˜„ ํด๋ž˜์Šค์— ๋Œ€ํ•œ ์ฐจ์ด๋ฅผ ๋ชจ๋ฅด๋Š” ์ฑ„ ์„œ๋กœ ๋‹ค๋ฅธ ๊ตฌํ˜„์œผ๋กœ ๋Œ€์ฒด๊ฐ€ ๊ฐ€๋Šฅ

Spring ์„ค์ • : xml

โ€ข
๊ธฐ๋ณธ์„ค์ • - ๋นˆ ๊ฐ์ฒด ์ƒ์„ฑ ๋ฐ ์ฃผ์ž…
โ—ฆ
์ฃผ์ž… ํ•  ๊ฐ์ฒด๋ฅผ ์„ค์ •ํŒŒ์ผ์— ์„ค์ •
โ–ช
<bean> : ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ๊ด€๋ฆฌํ•  Bean ๊ฐ์ฒด๋ฅผ ์„ค์ •
โ—ฆ
๊ธฐ๋ณธ ์†์„ฑ
โ–ช
name : ์ฃผ์ž… ๋ฐ›์„ ๊ณณ์—์„œ ํ˜ธ์ถœ ํ•  ์ด๋ฆ„
โ–ช
id : ์ฃผ์ž… ๋ฐ›์„ ๊ณณ์—์„œ ํ˜ธ์ถœ ํ•  ์ด๋ฆ„ ์„ค์ •(์œ ์ผ ๊ฐ’)
โ–ช
class : ์ฃผ์ž… ํ•  ๊ฐ์ฒด์˜ ํด๋ž˜์Šค
โ–ช
factory-method : Singleton ํŒจํ„ด์œผ๋กœ ์ž‘์„ฑ๋œ ๊ฐ์ฒด์˜ factory ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ

์Šคํ”„๋ง ๋นˆ ์˜์กด ๊ด€๊ณ„์„ค์ • : xml

โ€ข
์ƒ์„ฑ์ž ์ด์šฉ
โ—ฆ
๊ฐ์ฒด ๋˜๋Š” ๊ฐ’์„ ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด ์ฃผ์ž… ๋ฐ›๋Š”๋‹ค
โ—ฆ
<constructor-arg> : <bean>์˜ ํ•˜์œ„ํƒœ๊ทธ๋กœ ์„ค์ •ํ•œ bean ๊ฐ์ฒด ๋˜๋Š” ๊ฐ’์„ ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด ์ฃผ์ž…ํ•˜๋„๋ก ์„ค์ •
โ–ช
์„ค์ • ๋ฐฉ๋ฒ• : <ref>, <value>์™€ ๊ฐ™์€ ํ•˜์œ„ํƒœ๊ทธ๋ฅผ ์ด์šฉํ•˜์—ฌ ์„ค์ •ํ•˜๊ฑฐ๋‚˜ ๋˜๋Š” ์†์„ฑ์„ ์ด์šฉํ•˜์—ฌ ์„ค์ •
1.
ํ•˜์œ„ํƒœ๊ทธ ์ด์šฉ
a.
๊ฐ์ฒด ์ฃผ์ž… ์‹œ : <ref bean = "bean name"/>
b.
๋ฌธ์ž์—ด(String), primitive data ์ฃผ์ž… ์‹œ : <value>๊ฐ’</value>
โ€ข
type ์†์„ฑ : ๊ฐ’์€ ๊ธฐ๋ณธ์ ์œผ๋กœ String์œผ๋กœ ์ฒ˜๋ฆฌ
๊ฐ’์˜ ํƒ€์ž…์„ ๋ช…์‹œํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ ์‚ฌ์šฉ
ex) <value type="int">10</value>
2.
์†์„ฑ ์ด์šฉ
a.
๊ฐ์ฒด ์ฃผ์ž… ์‹œ : <constructor-arg ref ="bean name"/>
b.
๋ฌธ์ž์—ด(String), primitive data ์ฃผ์ž… ์‹œ : <constructor-arg value="๊ฐ’"/>
โ€ข
Property ์ด์šฉ
โ—ฆ
property๋ฅผ ํ†ตํ•ด ๊ฐ์ฒด ๋˜๋Š” ๊ฐ’์„ ์ฃผ์ž… ๋ฐ›๋Š”๋‹ค. - setter method
โ–ช
์ฃผ์˜ : setter๋ฅผ ํ†ตํ•ด์„œ๋Š” ํ•˜๋‚˜์˜ ๊ฐ’๋งŒ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.
โ—ฆ
<property> : <bean>์˜ ํ•˜์œ„ํƒœ๊ทธ๋กœ ์„ค์ •ํ•œ bean ๊ฐ์ฒด ๋˜๋Š” ๊ฐ’์„ property๋ฅผ ํ†ตํ•ด ์ฃผ์ž…ํ•˜๋„๋ก ์„ค์ •
โ–ช
์„ค์ • ๋ฐฉ๋ฒ• : <ref>, <value>์™€ ๊ฐ™์€ ํ•˜์œ„ํƒœ๊ทธ๋ฅผ ์ด์šฉํ•˜์—ฌ ์„ค์ • ํ•˜๊ฑฐ๋‚˜ ๋˜๋Š” ์†์„ฑ์„ ์ด์šฉํ•˜์—ฌ ์„ค์ •
1.
ํ•˜์œ„ํƒœ๊ทธ ์ด์šฉ
a.
๊ฐ์ฒด ์ฃผ์ž… ์‹œ : <ref bean = "bean name"/>
b.
๋ฌธ์ž์—ด(String), primitive data ์ฃผ์ž… ์‹œ : <value>๊ฐ’</value>
2.
์†์„ฑ ์ด์šฉ : name - ๊ฐ’์„ ์ฃผ์ž…ํ•  property ์ด๋ฆ„(setter์˜ ์ด๋ฆ„)
a.
๊ฐ์ฒด ์ฃผ์ž… ์‹œ : <property name = "propertyname" ref ="bean name"/>
b.
๋ฌธ์ž์—ด(String), primitive data ์ฃผ์ž… ์‹œ : <property name ="propertyname" value="๊ฐ’"/>
3.
xml namespace๋ฅผ ์ด์šฉํ•˜์—ฌ ์„ค์ •

์Šคํ”„๋ง ๋นˆ ์˜์กด ๊ด€๊ณ„์„ค์ • : Annotation

โ€ข
Annotation : ๋ฉค๋ฒ„๋ณ€์ˆ˜์— ์ง์ ‘ ์ •์˜ ํ•˜๋Š” ๊ฒฝ์šฐ setter method๋ฅผ ๋งŒ๋“ค์ง€ ์•Š์•„๋„ ๋จ
โ€ข
ํŠน์ • Bean์˜ ๊ธฐ๋Šฅ ์ˆ˜ํ–‰์„ ์œ„ํ•ด ๋‹ค๋ฅธ Bean์„ ์ฐธ์กฐํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ ์‚ฌ์šฉํ•œ๋‹ค.
โ€ข
@Resource
โ—ฆ
Spring 2.5๋ถ€ํ„ฐ ์ง€์›
โ—ฆ
๋ฉค๋ฒ„๋ณ€์ˆ˜, setter method์— ์‚ฌ์šฉ ๊ฐ€๋Šฅ
โ—ฆ
ํƒ€์ž…์— ๋งž์ถฐ์„œ ์—ฐ๊ฒฐ
โ€ข
@Autowired
โ—ฆ
Spring 2.5๋ถ€ํ„ฐ ์ง€์›
โ—ฆ
Spring Framework์—์„œ ์ง€์›ํ•˜๋Š” Dependency ์ •์˜ ์šฉ๋„์˜ Annotation
โ—ฆ
Spring Framework์— ์ข…์†์ ์ด๊ธด ํ•˜์ง€๋งŒ ์ •๋ฐ€ํ•œ Dependency Injection์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ์— ์œ ์šฉํ•˜๋‹ค.
โ—ฆ
๋ฉค๋ฒ„๋ณ€์ˆ˜, setter, constructor, ์ผ๋ฐ˜ method ์‚ฌ์šฉ ๊ฐ€๋Šฅ
โ—ฆ
ํƒ€์ž…์— ๋งž์ถฐ์„œ ์—ฐ๊ฒฐ
โ–ช
๋™์ผํ•œ ํƒ€์ž…์˜ bean์ด ์—ฌ๋Ÿฌ๊ฐœ์ผ ์ˆ˜ ์žˆ๋‹ค!
โ–ช
์ด๋Ÿฐ ๊ฒฝ์šฐ์—๋Š” @Qualifire("name")์œผ๋กœ ์‹๋ณ„
//@Repository @Repository(value = "aaa") public class GuestBookDaoImpl implements GuestBookDao { ... } public class GuestBookServiceImpl implements GuestBookService { @Autowired @Qualifier("aaa") //@Qualifier("guestBookDaoImpl") -> value ์ง€์ •์„ ์•ˆํ•œ ๊ฒฝ์šฐ class ์ด๋ฆ„์˜ ์ฒซ๊ธ€์ž๋ฅผ ์†Œ๋ฌธ์ž๋กœ private GuestBookDao guestBookDao; ... }
Java
๋ณต์‚ฌ
โ€ข
@Inject
โ—ฆ
Spring 3.0๋ถ€ํ„ฐ ์ง€์›
โ—ฆ
Framework์— ์ข…์†์ ์ด์ง€ ์•Š์Œ
โ—ฆ
Javax.inject-x.x.x.jar ํ•„์š”
โ—ฆ
๋ฉค๋ฒ„๋ณ€์ˆ˜, setter, constructor, ์ผ๋ฐ˜ method ์‚ฌ์šฉ ๊ฐ€๋Šฅ
โ—ฆ
์ด๋ฆ„์œผ๋กœ ์—ฐ๊ฒฐ

์Šคํ”„๋ง ๋นˆ ์˜์กด ๊ด€๊ณ„์„ค์ • : Java Configuration

โ€ข
xml ํŒŒ์ผ ๋Œ€์‹  Java ํŒŒ์ผ์„ ์ด์šฉํ•ด์„œ Configuration์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
๊ธฐ์กด XML (ApplicationContext.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-4.3.xsd"> <context:component-scan base-package="com.ssafy.model"></context:component-scan> <bean class="org.springframework.jdbc.datasource.SimpleDriverDataSource"> <property name="driverClass" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://127.0.0.1:3306/ssafyweb?serverTimezone=UTC&amp;useUniCode=yes&amp;characterEncoding=UTF-8"/> <property name="username" value="ssafy"/> <property name="password" value="ssafy"/> </bean> </beans>
XML
๋ณต์‚ฌ
์ž๋ฐ” config
// ApplicationConfig.java // applicationcontext.xml ํŒŒ์ผ ๋Œ€์ฒด @Configuration // ๊ตฌ์„ฑํŒŒ์ผ์ž„์„ ๋ช…์‹œ @ComponentScan(basePackages = {"com.ssafy.model"}) // component sacn ๋Œ€์ƒ ์ง€์ • public class ApplicationConfig { @Bean public DataSource getDataSource() { SimpleDriverDataSource dataSource = new SimpleDriverDataSource(); dataSource.setDriverClass(com.mysql.cj.jdbc.Driver.class); dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/ssafyweb?serverTimezone=UTC&useUniCode=yes&characterEncoding=UTF-8"); dataSource.setUsername("ssafy"); dataSource.setPassword("ssafy"); return dataSource; } }
Java
๋ณต์‚ฌ
์ž๋ฐ” ์ฝ”๋“œ์—์„œ์˜ ์‚ฌ์šฉ
//com/ssafy/configuration/applicationContext.xml ์„ค์ • ApplicationContext context = new ClassPathXmlApplicationContext("com/ssafy/configuration/applicationContext.xml"); //ApplicationConfig.java ์„ค์ • ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class);
Java
๋ณต์‚ฌ