๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Spring

[Spring] ์˜์กด์„ฑ ์ฃผ์ž… ์• ๋…ธํ…Œ์ด์…˜ ์ •๋ฆฌ - @Autowired, @Resource, @Inject

by Leica 2020. 4. 13.
๋ฐ˜์‘ํ˜•

[Spring] ์˜์กด์„ฑ ์ฃผ์ž… ์• ๋…ธํ…Œ์ด์…˜ ์ •๋ฆฌ - @Autowired, @Resource, @Inject

1. @Autowired

@Autowired๋Š” ์ฃผ์ž…ํ•˜๋ ค๊ณ  ํ•˜๋Š” ๊ฐ์ฒด์˜ ํƒ€์ž…์ด ์ผ์น˜ํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ์ž๋™์œผ๋กœ ์ฃผ์ž…ํ•œ๋‹ค.

@Autowired๋Š” ํ•„๋“œ, ์ƒ์„ฑ์ž, Setter์— ๋ถ™์ผ ์ˆ˜ ์žˆ๋‹ค.

๋‹จ, @Autowired๋ฅผ ํ•„๋“œ, Setter์— ๋ถ™์—ฌ์„œ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ๋ฐ˜๋“œ์‹œ ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๊ฐ€ ์ •์˜๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค.

 

1) @Autowired - ํ•„๋“œ ์ฃผ์ž…

public class WordRegisterServiceUseAutowired {

	@Autowired
	private WordDao wordDao;
	
	public WordRegisterServiceUseAutowired() { }
}

 

2) @Autowired - ์ƒ์„ฑ์ž ์ฃผ์ž…

public class WordRegisterServiceUseAutowired {

	private WordDao wordDao;
	
	@Autowired
	public WordRegisterServiceUseAutowired(WordDao wordDao) {
		this.wordDao = wordDao;
	}
}

 

3) @Autowired - Setter ์ฃผ์ž…

public class WordRegisterServiceUseAutowired {

	private WordDao wordDao;
	
	public WordRegisterServiceUseAutowired() { }

	@Autowired
	public void setWordDao(WordDao wordDao) {
		this.wordDao = wordDao;
	}
}

 

4) @Qualifier ์• ๋…ธํ…Œ์ด์…˜

XML ์„ค์ • ํŒŒ์ผ

<bean id="wordDao1" class="com.word.dao.WordDao" >
	 <qualifier value="usedDao"/> 
</bean>
<bean id="wordDao2" class="com.word.dao.WordDao" />
<bean id="wordDao3" class="com.word.dao.WordDao" />

์œ„์™€ ๊ฐ™์ด ๋™์ผํ•œ ํƒ€์ž…์˜ ๋นˆ ๊ฐ์ฒด๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ ์ •์˜๋˜์–ด ์žˆ์„ ๊ฒฝ์šฐ ์šฐ์„ ์ ์œผ๋กœ ์‚ฌ์šฉํ•  ๋นˆ ๊ฐ์ฒด์˜ <bean> ํƒœ๊ทธ ํ•˜์œ„์— <qualifier> ํƒœ๊ทธ๋ฅผ ์„ค์ •ํ•œ๋‹ค.

 

์ž๋ฐ” ์ฝ”๋“œ

public class WordRegisterServiceUseAutowired {

	@Autowired
	@Qualifier("usedDao")
	private WordDao wordDao;
	
	public WordRegisterServiceUseAutowired() { }
}

๊ทธ๋ฆฌ๊ณ  ์ž๋ฐ” ์ฝ”๋“œ์—์„œ๋Š” @Autowired์™€ ํ•จ๊ป˜ @Qualifier๋ฅผ ์‚ฌ์šฉํ•ด์„œ @Qualifier์— XML ์„ค์ • ํŒŒ์ผ์—์„œ ์„ค์ •ํ•œ <qualifier> ํƒœ๊ทธ์˜ value ๊ฐ’์„ ์ง€์ •ํ•ด์ค€๋‹ค.

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋™์ผํ•œ ํƒ€์ž…์˜ ๋นˆ์ด ์—ฌ๋Ÿฌ ๊ฐœ์ผ ๊ฒฝ์šฐ ์šฐ์„ ์ ์œผ๋กœ ํŠน์ • ๋นˆ์ด ์ฃผ์ž…๋œ๋‹ค.

 

2. @Resource

@Resource๋Š” ์ฃผ์ž…ํ•˜๋ ค๊ณ  ํ•˜๋Š” ๊ฐ์ฒด์˜ ์ด๋ฆ„(id)์ด ์ผ์น˜ํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ์ž๋™์œผ๋กœ ์ฃผ์ž…ํ•œ๋‹ค.

@Resource๋Š” Java ์ œ๊ณต ์• ๋…ธํ…Œ์ด์…˜์ด๋ฉฐ ํ•„๋“œ, Setter์— ๋ถ™์ผ ์ˆ˜ ์žˆ๋‹ค. ์ƒ์„ฑ์ž์—๋Š” ๋ถ™์ผ ์ˆ˜ ์—†๋‹ค.

@Autowired์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ฐ˜๋“œ์‹œ ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๊ฐ€ ์ •์˜๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค.

 

1) ์˜์กด์„ฑ ์„ค์ •

<dependency>
    <groupId>javax.annotation</groupId>
    <artifactId>javax.annotation-api</artifactId>
    <version>1.3.2</version>
</dependency>

ํ”„๋กœ์ ํŠธ์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด javax.annotation-api ์˜์กด์„ฑ์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

 

2) @Resource - ํ•„๋“œ ์ฃผ์ž…

import javax.annotation.Resource;

public class WordRegisterServiceUseResource {

	@Resource
	private WordDao wordDao;
	
	public WordRegisterServiceUseResource() { }
}

 

3) @Resource - Setter ์ฃผ์ž…

import javax.annotation.Resource;

public class WordRegisterServiceUseResource {

	private WordDao wordDao;
	
	public WordRegisterServiceUseResource() { }
	
	@Resource
	public WordRegisterServiceUseResource(WordDao wordDao) {
		this.wordDao = wordDao;
	}
}

 

3. @Inject

@Inject๋Š” @Autowired์™€ ์œ ์‚ฌํ•˜๊ฒŒ ์ฃผ์ž…ํ•˜๋ ค๊ณ  ํ•˜๋Š” ๊ฐ์ฒด์˜ ํƒ€์ž…์ด ์ผ์น˜ํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ์ž๋™์œผ๋กœ ์ฃผ์ž…ํ•œ๋‹ค.

@Resource๋Š” Java ์ œ๊ณต ์• ๋…ธํ…Œ์ด์…˜์ด๋ฉฐ ํ•„๋“œ, ์ƒ์„ฑ์ž, Setter์— ๋ถ™์ผ ์ˆ˜ ์žˆ๋‹ค.

@Autowired์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํ•„๋“œ, Setter์— ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ๋ฐ˜๋“œ์‹œ ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๊ฐ€ ์ •์˜๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค.

 

1) ์˜์กด์„ฑ ์„ค์ •

<dependency>
    <groupId>javax.inject</groupId>
    <artifactId>javax.inject</artifactId>
    <version>1</version>
</dependency>

ํ”„๋กœ์ ํŠธ์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด javax.inject ์˜์กด์„ฑ์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

 

2) @Inject - ํ•„๋“œ ์ฃผ์ž…

import javax.inject.Inject;

public class WordRegisterServiceUseAutowired {

	@Inject
	private WordDao wordDao;
	
	public WordRegisterServiceUseAutowired() { }
}

 

3) @Inject - ์ƒ์„ฑ์ž ์ฃผ์ž…

import javax.inject.Inject;

public class WordRegisterServiceUseAutowired {

	private WordDao wordDao;
	
	@Inject
	public WordRegisterServiceUseAutowired(WordDao wordDao) {
		this.wordDao = wordDao;
	}
}

 

4) @Inject - Setter ์ฃผ์ž…

import javax.inject.Inject;

public class WordRegisterServiceUseAutowired {

	private WordDao wordDao;
	
	public WordRegisterServiceUseAutowired() { }

	@Inject
	public void setWordDao(WordDao wordDao) {
		this.wordDao = wordDao;
	}
}

 

5) @Named ์• ๋…ธํ…Œ์ด์…˜

@Autowired์˜ @Qualifier์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด @Inject์—์„œ๋Š” @Named์ด๋‹ค.

@Qualifier์™€ ๋‹ฌ๋ฆฌ @Named์—๋Š” ๋นˆ ์ด๋ฆ„(id)๋ฅผ ์ง€์ •ํ•˜๋ฏ€๋กœ @Autowired, @Qualifier๋ฅผ ์‚ฌ์šฉํ• ๋•Œ์— ๋น„ํ•ด XML ์„ค์ • ํŒŒ์ผ์ด ๋‹ค์†Œ ์งง์•„์ง„๋‹ค๋Š” ํŠน์ง•์ด ์žˆ๋‹ค.

 

XML ์„ค์ • ํŒŒ์ผ

<bean id="wordDao1" class="com.word.dao.WordDao"/> 
<bean id="wordDao2" class="com.word.dao.WordDao"/>
<bean id="wordDao3" class="com.word.dao.WordDao"/>

<Qualifier> ํƒœ๊ทธ๊ฐ€ ํ•„์š”ํ•œ @Qualifier์™€ ๋‹ฌ๋ฆฌ @Named๋Š” XML ์„ค์ • ํŒŒ์ผ์— ์ถ”๊ฐ€์ ์œผ๋กœ ์„ค์ •ํ•  ๊ฒƒ์ด ์—†๋‹ค.

 

์ž๋ฐ” ์ฝ”๋“œ

import javax.inject.Inject;
import javax.inject.Named;

public class WordRegisterServiceUseInject {

	@Inject
	@Named("wordDao1")
	private WordDao wordDao;
	
	public WordRegisterServiceUseInject() {
		
	}
}

@Named์— ๋นˆ ์ด๋ฆ„(id)๋ฅผ ์ง€์ •ํ•œ๋‹ค.

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€