Tiny Bunny
๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ป/SpringBoot

[Spring Boot] ์Šคํ”„๋ง ์˜์กด์„ฑ ์ฃผ์ž… @Autowired, @Resource, @Inject

by soonybutter 2024. 12. 14.
728x90

 

 

์Šคํ”„๋ง ์˜์กด์„ฑ ์ฃผ์ž… @Autowired, @Resource, @Inject (Java Spring Dependency Injection)

โ€‹

์˜์กด์„ฑ ์ฃผ์ž…์€ ๊ฐ์ฒด๋ฅผ ์ง์ ‘ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ  @Service์„ ์ด์šฉํ•˜์—ฌ class annotation์œผ๋กœ ๋‹ฌ์•„์ฃผ๋ฉด ์Šคํ”„๋ง์€ beanFactory์— ๋‹ด์•„ ๋‘๊ณ  ์‚ฌ์šฉํ•˜๋Š” ๊ณณ์— @Autowired์„ ์‚ฌ์šฉํ•˜๋Š” ๊ณณ์— ํ•ด๋‹น bean์„ ์ฐพ์•„ ์ฃผ์ž…์‹œ์ผœ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋„์™€์ค€๋‹ค.

โ€‹

1. @Autowired

@Autowired๋Š” ์ฃผ์ž…ํ•˜๋ ค๊ณ  ํ•˜๋Š” ๊ฐ์ฒด์˜ ํƒ€์ž…์ด ์ผ์น˜ํ•˜๋Š” ๊ฐ์ฒด๋ฅผ loc ์ปจํ…Œ์ด๋„ˆ ์•ˆ์— ์กด์žฌํ•˜๋Š” Bean์„ ์ž๋™์œผ๋กœ ์ฃผ์ž…ํ•œ๋‹ค.

โ€‹

์ฃผ์ž… ๋ฐฉ๋ฒ•์€ 3๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค.

โ€‹

ํ•„๋“œ ์ฃผ์ž… (Field Injection)

ํ•„๋“œ์— @Autowired ์–ด๋…ธํ…Œ์ด์…˜์„ ๋ถ™์—ฌ ์ฃผ๋ฉด ์ž๋™์œผ๋กœ ์˜์กด์„ฑ์ด ์ฃผ์ž…๋œ๋‹ค. ๋งค์šฐ ๊ฐ„๋‹จํ•˜๋ฉฐ ์˜ˆ์ œ๋กœ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.

public class TestController { @Autowired private InjectSampleService injectSampleService; @GetMapping("/persion") public List<Person> getPersions(@RequestParam(name = "name") String name) { return injectSampleService.query(name); } }

โ€‹

์ƒ์„ฑ์ž ์ฃผ์ž… (Constructor Injection)

๋‹จ์ผ ์ƒ์„ฑ์ž์ธ ๊ฒฝ์šฐ @Autowired ์–ด๋…ธํ…Œ์ด์…˜์„ ๋ถ™์ด์ง€ ์•Š์•„๋„ ๋˜์ง€๋งŒ ์ƒ์„ฑ์ž 2๊ฐœ ์ด์ƒ์ผ ๊ฒฝ์šฐ์—๋Š” ์–ด๋…ธํ…Œ์ด์…˜์„ ๊ผญ ๋ถ™์—ฌ์ฃผ์–ด์•ผ ํ•œ๋‹ค.

public class TestController { private InjectSampleService injectSampleService; @Autowired // spring 4.3 ๋ฒ„์ „ ์ด์ƒ๋ถ€ํ„ฐ๋Š” ์ƒ๋žต ๊ฐ€๋Šฅ public TestController(InjectSampleService injectSampleService) { this.injectSampleService= injectSampleService; } @GetMapping("/persion") public List<Person> getPersions(@RequestParam(name = "name") String name) { return injectSampleService.query(name); } }

โ€‹

์„ธํ„ฐ ์ฃผ์ž… (Setter Injection)

๋ฉ”์„œ๋“œ ์ด๋ฆ„์ด ์„ธํ„ฐ ๋„ค์ด๋ฐ ํŒจํ„ด(setXXXX)์ด ์•„๋‹ˆ์–ด๋„ ๋™์ผํ•œ ๊ธฐ๋Šฅ์„ ํ•˜์ง€๋งŒ ์ผ๊ด€์„ฑ๊ณผ ๋ช…ํ™•ํ•œ ์ฝ”๋“œ๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„œ

์ •ํ™•ํ•œ ์ด๋ฆ„์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•œ๋‹ค.

public class TestController { private InjectSampleService injectSampleService; @Autowired // spring 4.3 ๋ฒ„์ „ ์ด์ƒ๋ถ€ํ„ฐ๋Š” ์ƒ๋žต ๊ฐ€๋Šฅ public void setInjectSampleService(InjectSampleService injectSampleService) { this.injectSampleService= injectSampleService; } }
 

โ€‹

์—ฌ๊ธฐ์„œ ์•Œ์•„์•ผ ํ•  ๋‚ด์šฉ์€ ํ•„๋“œ ์ฃผ์ž…์„ ์‚ฌ์šฉํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฒฝ๊ณ ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

Spring Team recommends: “Always use constructor based dependency injection in your beans. Always use assertions for mandatory dependencies”.

ํ•„๋“œ ์ฃผ์ž…์„ ์‚ฌ์šฉํ•˜์ง€ ๋ง๊ณ  ์ƒ์„ฑ์ž ์ฃผ์ž…์„ ์‚ฌ์šฉํ•˜๋ผ๋Š”๋ฐ ์ƒ์„ฑ์ž ์ฃผ์ž…์„ ๊ถŒ์žฅํ•˜๋Š” ์ด์œ ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

https://madplay.github.io/post/why-constructor-injection-is-better-than-field-injection

- ์ˆœํ™˜ ์ฐธ์กฐ ๋ฐฉ์ง€ (A๊ฐ€ B๋ฅผ ์ฐธ์กฐํ•˜๊ณ  B๊ฐ€ A๋ฅผ ์ฐธ์กฐํ•  ๋•Œ ๋ฐœ์ƒ๋  ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ)

- ํ…Œ์ŠคํŠธ์— ์šฉ์ด

- final ์„ ์–ธ ๊ฐ€๋Šฅ

- ์˜ค๋ฅ˜ ๋ฐฉ์ง€ (๋ถˆ๋ณ€(immutable) ๊ฐ์ฒด ๋˜๋Š” null ๋ฐฉ์ง€ ๊ฐ€๋Šฅ)

โ€‹

Qualifier ์–ด๋…ธํ…Œ์ด์…˜

๋™์ผํ•œ ํƒ€์ž…์˜ ๋นˆ ๊ฐ์ฒด๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ ์ •์˜๋˜์–ด ์žˆ์„ ๊ฒฝ์šฐ,

์šฐ์„ ์ ์œผ๋กœ ์‚ฌ์šฉํ•  ๋นˆ ์ฑ…์ฒด์˜ <bean> ํƒœ๊ทธ ํ•˜์œ„์— <qualifier>ํƒœ๊ทธ๋ฅผ ์„ค์ •ํ•˜์—ฌ ํŠน์ • ๋นˆ์„ ์ฃผ์ž…ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค.

<bean id="testInject1" class="com.example.demo.services" > <qualifier value="qualifierTest"/> </bean> <bean id="testInject2" class="com.example.demo.services" />
public class WordRegisterServiceUseAutowired { @Autowired @Qualifier("qualifierTest") private InjectSampleService injectSampleService; @GetMapping("/persion") public List<Person> getPersions(@RequestParam(name = "name") String name) { return injectSampleService.query(name); } }

โ€‹

โ€‹

2. @Resource

@Resource๋Š” ์ฃผ์ž…ํ•˜๋ ค๊ณ  ํ•˜๋Š” ๊ฐ์ฒด์˜ ์ด๋ฆ„(id)์ด ์ผ์น˜ํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ์ž๋™์œผ๋กœ ์ฃผ์ž…ํ•˜๋ฉฐ ํ•„๋“œ, Setter์— ๋ถ™์ผ ์ˆ˜ ์žˆ์ง€๋งŒ ์ƒ์„ฑ์ž์—๋Š” ๋ถ™์ผ ์ˆ˜ ์—†๋‹ค. @Autowired์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ฐ˜๋“œ์‹œ ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๊ฐ€ ์ •์˜๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค.

 

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

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

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

 

ํ•„๋“œ ์ฃผ์ž… (Field Injection)

import javax.annotation.Resource; public class WordRegisterServiceUseResource { @Resource private WordDao wordDao; public WordRegisterServiceUseResource() { } }

 

์„ธํ„ฐ ์ฃผ์ž… (Setter Injection)

import javax.annotation.Resource; public class WordRegisterServiceUseResource { private WordDao wordDao; public WordRegisterServiceUseResource() { } @Resource public WordRegisterServiceUseResource(WordDao wordDao) { this.wordDao = wordDao; } }
 

โ€‹

โ€‹

3. @Inject

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

 

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

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

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

โ€‹

ํ•„๋“œ ์ฃผ์ž… (Field Injection)

import javax.inject.Inject; public class WordRegisterServiceUseAutowired { @Inject private WordDao wordDao; public WordRegisterServiceUseAutowired() { } }

 

์ƒ์„ฑ์ž ์ฃผ์ž… (Constructor Injection)

import javax.inject.Inject; public class WordRegisterServiceUseAutowired { private WordDao wordDao; @Inject public WordRegisterServiceUseAutowired(WordDao wordDao) { this.wordDao = wordDao; } }

 

์„ธํ„ฐ ์ฃผ์ž… (Setter Injection)

import javax.inject.Inject; public class WordRegisterServiceUseAutowired { private WordDao wordDao; public WordRegisterServiceUseAutowired() { } @Inject public void setWordDao(WordDao wordDao) { this.wordDao = wordDao; } }

 

Named ์–ด๋…ธํ…Œ์ด์…˜

@Autowired์˜ @Qualifier ์™€ ๋น„์Šทํ•œ ์—ญํ• ์„ ํ•˜๋Š”๊ฒŒ @Inject์—์„œ์˜ @Named์ด๋‹ค.

@Qualifier์™€ ๋‹ฌ๋ฆฌ @Named์—๋Š” bean id ์„ ์ง€์ •ํ•˜๋ฏ€๋กœ

@Autowired, @Qualifier๋ฅผ ์‚ฌ์šฉํ• ๋•Œ์— ๋น„ํ•ด XML ์„ค์ • ํŒŒ์ผ์ด ๋‹ค์†Œ ์งง์•„์ง„๋‹ค๋Š” ํŠน์ง•์ด ์žˆ๋‹ค.

 

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

<Qualifier> ํƒœ๊ทธ๊ฐ€ ํ•„์š”ํ•œ @Qualifier์™€ ๋‹ฌ๋ฆฌ @Named๋Š” 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"/>

 

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

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

import javax.inject.Inject; import javax.inject.Named; public class WordRegisterServiceUseInject { @Inject @Named("wordDao1") private WordDao wordDao; public WordRegisterServiceUseInject() { } }

 

 

 

*๊ณต๋ถ€์šฉ ๊ธฐ๋ก์ž…๋‹ˆ๋‹ค.

728x90

TOP

Designed by ํ‹ฐ์Šคํ† ๋ฆฌ