[WIL] ๐Ÿƒ ์˜์กด์„ฑ ์ฃผ์ž…(DI)๊ณผ ์ œ์–ด์˜ ์—ญ์ „(IoC) ๊ทธ๋ฆฌ๊ณ  ์Šคํ”„๋ง ๋นˆ(Spring Bean)

์ปดํด๋กœ๋”ฉยท2022๋…„ 7์›” 9์ผ
0

Spring-TIL

๋ชฉ๋ก ๋ณด๊ธฐ
2/5
post-thumbnail

๐Ÿ“Œ Intro

  • ํ•ญํ•ด99๋ฅผ ์‹œ์ž‘ํ•œ์ง€ ์ด์ œ 4์ฃผ์ฐจ๊ฐ€ ๋œ๋‹ค. 3์ฃผ์ฐจ ์ฃผํŠน๊ธฐ ์ž…๋ฌธ์—์„œ ์ฒ˜์Œ์œผ๋กœ Spring์„ ์‚ฌ์šฉํ•ด๋ดค๋‹ค. Spring์ด ์–ด๋ ต๋‹ค๊ณ  ์ด์•ผ๊ธฐ๋งŒ ๋“ค์—ˆ๋Š”๋ฐ ์ƒ๊ฐ๋ณด๋‹ค ๋„ˆ๋ฌด ํŽธํ•˜๋‹ค. ํ•˜์ง€๋งŒ ํŽธํ•œ๋งŒํผ ๋‚ด๋ถ€์ ์œผ๋กœ ์–ด๋–ป๊ฒŒ ๋™์ž‘ํ•˜๋Š”์ง€ ์ž˜ ์ดํ•ด๊ฐ€๊ฐ€์ง€ ์•Š๋Š” ๋‹จ์ ์ด ์žˆ๋‹ค. ์ด์ œ Spring์„ ์ œ๋Œ€๋กœ ๊ณต๋ถ€ํ•  ์‹œ๊ฐ„์ด 4์ฃผ์ฐจ 5์ฃผ์ฐจ 2์ฃผ๊ฐ„์˜ ์‹œ๊ฐ„์ด ๋‚จ์•˜๋Š”๋ฐ ์ธํ”„๋Ÿฐ์˜ Spring๊ฐ•์˜๋ฅผ ๊ตฌ๋งคํ•ด์„œ ๋“ค์–ด๋ณผ๊นŒ ๊ณ ๋ฏผ์ค‘์ด๋‹ค.

  • Spring์—์„œ ์ค‘์š”ํ•œ ๊ฐœ๋… 3๊ฐ€์ง€๋ฅผ ํ•œ ๋ฒˆ ์ •๋ฆฌํ•ด๋ณผ๊นŒํ•œ๋‹ค.

1. DI(์˜์กด์„ฑ ์ฃผ์ž…)

์˜์กด์„ฑ ์ฃผ์ž…( Dependency Injection ) : ํ•˜๋‚˜์˜ ๊ฐ์ฒด๊ฐ€ ๋‹ค๋ฅธ ๊ฐ์ฒด์˜ ์˜์กด์„ฑ์„ ์ œ๊ณตํ•˜๋Š” ํ…Œํฌ๋‹‰์ด๋‹ค.

  • ์˜์กด์„ฑ ์ฃผ์ž…์˜ ์˜๋„๋Š” ๊ฐ์ฒด์˜ ์ƒ์„ฑ๊ณผ ์‚ฌ์šฉ์˜ ๊ด€์‹ฌ์„ ๋ถ„๋ฆฌํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ด๋Š” ๊ฐ€๋…์„ฑ๊ณผ ์ฝ”๋“œ ์žฌ์‚ฌ์šฉ์„ ๋†’ํ˜€์ค€๋‹ค.
  • ์˜์กด์„ฑ ์ฃผ์ž…์€ ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•œ๋‹ค.
    • ์–ด๋–ป๊ฒŒ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด๋‚˜ ํด๋ž˜์Šค๊ฐ€ ๊ฐ์ฒด์˜ ์ƒ์„ฑ ๋ฐฉ์‹๊ณผ ๋…๋ฆฝ์ ์ผ ์ˆ˜ ์žˆ๋Š”๊ฐ€?
    • ์–ด๋–ป๊ฒŒ ๊ฐ์ฒด์˜ ์ƒ์„ฑ ๋ฐฉ์‹์„ ๋ถ„๋ฆฌ๋œ ๊ตฌ์„ฑ ํŒŒ์ผ์—์„œ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋Š”๊ฐ€?
    • ์–ด๋–ป๊ฒŒ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ๋‹ค๋ฅธ ๊ตฌ์„ฑ์„ ์ง€์›ํ•  ์ˆ˜ ์žˆ๋Š”๊ฐ€?
  • ํ”„๋กœ๊ทธ๋žจ ๋””์ž์ธ์ด ๊ฒฐํ•ฉ๋„๋ฅผ ๋Š์Šจํ•˜๊ฒŒ ๋˜๋„๋กํ•˜๊ณ  ์˜์กด๊ด€๊ณ„ ์—ญ์ „ ์›์น™๊ณผ ๋‹จ์ผ ์ฑ…์ž„ ์›์น™์„ ๋”ฐ๋ฅด๋„๋ก ํด๋ผ์ด์–ธํŠธ(์ˆ˜์‹ ๊ฐ์ฒด)์˜ ์ƒ์„ฑ์— ๋Œ€ํ•œ ์˜์กด์„ฑ์„ ํด๋ผ์ด์–ธํŠธ์˜ ํ–‰์œ„๋กœ๋ถ€ํ„ฐ ๋ถ„๋ฆฌํ•˜๋Š” ๊ฒƒ์ด๋‹ค.
  • ์˜์กด์„ฑ ์ฃผ์ž…์˜ ๊ธฐ๋ณธ ๋‹จ์œ„์ธ ์ฃผ์ž…์€ ์ƒˆ๋กญ๊ฑฐ๋‚˜ ๊ด€์Šต์ ์ธ ๋ฉ”์ปค๋‹ˆ์ฆ˜์ด ์•„๋‹ˆ๋‹ค. "๋งค๊ฐœ๋ณ€์ˆ˜ ์ „๋‹ฌ"๊ณผ ๋™์ผํ•˜๊ฒŒ ๋™์ž‘ํ•œ๋‹ค.

  • ์„œ๋น„์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋Š” ํด๋ผ์ด์–ธํŠธ(์ˆ˜์‹  ๊ฐ์ฒด)๊ฐ€ ํ•ด๋‹น ์„œ๋น„์Šค์˜ ๊ตฌ์„ฑ๋ฐฉ๋ฒ•์„ ์•Œ ํ•„์š”๊ฐ€ ์—†๊ฒŒ ํ•˜๋Š” ๊ฒƒ.
  • ์˜์กด์„ฑ ์ฃผ์ž…์€ ํ”„๋ ˆ์ž„์›Œํฌ๊ฐ€ ์•„๋‹Œ ๋””์ž์ธ ํŒจํ„ด, IoC ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜.
  • ์˜์กด์„ฑ ์ฃผ์ž…์€ ์ปดํŒŒ์ผํƒ€์ž„์ด ์•„๋‹Œ ๋Ÿฐํƒ€์ž„์— ์ข…์†์„ฑ์„ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋จ.
  • ๋ฌด์–ธ๊ฐ€๋ฅผ ์‰ฝ๊ฒŒ ๊ต์ฒดํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋งŒ๋“ค์–ด์ค€๋‹ค.

์žฅ์ 

  • ๋ชจ๋“ˆ๋“ค์„ ์‰ฝ๊ฒŒ ๊ต์ฒดํ•  ์ˆ˜ ์žˆ๋Š” ๊ตฌ์กฐ -> ํ…Œ์ŠคํŒ…์ด ์‰ฝ๊ณ  ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ํ•˜๊ธฐ๋„ ์‰ฝ๋‹ค.
  • ๊ตฌํ˜„ํ•  ๋•Œ ์ถ”์ƒํ™” ๋ ˆ์ด์–ด๋ฅผ ๋„ฃ๊ณ  ์ด๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ตฌํ˜„์ฒด๋ฅผ ๋„ฃ์–ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์˜์กด์„ฑ ๋ฐฉํ–ฅ์ด ์ผ๊ด€๋˜๊ณ , ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‰ฝ๊ฒŒ ์ถ”๋ก  ๊ฐ€๋Šฅ. ๐Ÿ‘‰๐Ÿป ๋ชจ๋“ˆ ๊ฐ„ ๊ด€๊ณ„๊ฐ€ ์กฐ๊ธˆ ๋” ๋ช…ํ™•ํ•ด์ง.

๋‹จ์ 

  • ๋ชจ๋“ˆ๋“ค์˜ ๋ถ„๋ฆฌ๊ฐ€ ์ฆ๊ฐ€ -> ํด๋ž˜์Šค ์ˆ˜ ์ฆ๊ฐ€ -> ๋ณต์žก์„ฑ์ด ์ฆ๊ฐ€๋จ.
  • ๋ณต์žก์„ฑ์ด ์ฆ๊ฐ€๋จ์— ๋”ฐ๋ผ ์•ฝ๊ฐ„์˜ ๋Ÿฐํƒ€์ž„ ํŽ˜๋„ํ‹ฐ๊ฐ€ ์ƒ๊ธฐ๊ธฐ๋„ ํ•œ๋‹ค.
  • ์ข…์†์„ฑ์€ ๋Ÿฐํƒ€์ž„์—์„œ ํ•ด๊ฒฐ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ข…์†์„ฑ ์˜ค๋ฅ˜๋Š” ์ปดํŒŒ์ผ ์‹œ๊ฐ„์— ํฌ์ฐฉํ•  ์ˆ˜ ์—†๋‹ค.

์˜์กด์„ฑ ์ฃผ์ž… ์‚ฌ์šฉ๋ฒ•

Lombok์—†์ด

@Controller
public class MemberController {

    private final MemberService memberService;

    @Autowired
    public MemberController(MemberService memberService) {
        this.memberService = memberService;
    }

    @PostMapping("members/new")
    public String create(MemberForm form){
        Member member = new Member();
        member.setName(form.getName());
        memberService.join(member);
        return "redirect:/";
    }
}
  • ํด๋ผ์ด์–ธํŠธ(์ˆ˜์‹ ๊ฐ์ฒด)์— ์‚ฌ์šฉํ•  memberService๋ฅผ private final ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ์„ ์–ธ.
  • ์ƒ์„ฑ์ž๋กœ ์˜์กด์„ฑ ์ฃผ์ž… ํ›„ ํ•ด๋‹น ๋ฉ”์†Œ๋“œ ์œ„์— @Autowired ์–ด๋…ธํ…Œ์ด์…˜ ์ถ”๊ฐ€
        public MemberController(MemberService memberService) {
            this.memberService = memberService;
        }

Lombokํ™œ์šฉ

@RestController
@RequiredArgsConstructor
public class PostController {
    private final PostRepository postRepository;
    private final PostService postService;

    @PostMapping("/posts")
    public Post createPost(@RequestBody PostRequestDto requestDto){
        Post post = new Post(requestDto);
        return postRepository.save(post);
    }
}
  • ํด๋ผ์ด์–ธํŠธ(์ˆ˜์‹ ๊ฐ์ฒด)์— ์‚ฌ์šฉํ•  postRepository, postService๋ฅผ private final ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ์„ ์–ธ.
  • Controller ํด๋ž˜์Šค ์œ„์— @RequiedArgsConstructor ์–ด๋…ธํ…Œ์ด์…˜ ์ถ”๊ฐ€๋งŒ ํ•ด์ฃผ๋ฉด ๋จ.

2. IoC(์ œ์–ด์˜ ์—ญ์ „)

์ œ์–ด์˜ ์—ญ์ „( Inversion of Control ) : ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ์ž‘์„ฑํ•œ ํ”„๋กœ๊ทธ๋žจ์ด ์žฌ์‚ฌ์šฉ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ํ๋ฆ„ ์ œ์–ด๋ฅผ ๋ฐ›๊ฒŒ ๋˜๋Š” ์†Œํ”„ํŠธ์›จ์–ด ๋””์ž์ธ ํŒจํ„ด์„ ๋งํ•œ๋‹ค.

  • ์ œ์–ด์˜ ์—ญ์ „์€ ์˜์กด์„ฑ ์ฃผ์ž…์˜ ์ƒ์œ„ ๊ฐœ๋…์ด๋‹ค.
  • Spring Container๊ฐ€ ํ•„์š”์— ๋”ฐ๋ผ ๊ฐœ๋ฐœ์ž ๋Œ€์‹  Bean๋“ค์„ ๊ด€๋ฆฌ(์ œ์–ด)ํ•ด์ฃผ๋Š” ํ–‰์œ„
    • Spring Container๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด์˜ ๊ฐ์ฒด ์ƒ์„ฑ์„ ์ง€ํ–ฅํ•œ๋‹ค.
    • ๋ณ„๋„๋กœ ์„ค์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ๋ชจ๋“  ์ปจํ…Œ์ด๋„ˆ ์•ˆ์˜ ๋นˆ(Bean) ๊ฐ์ฒด๋Š” ๋”ฑ ํ•˜๋‚˜๋งŒ ์ƒ์„ฑ๋˜์–ด ๊ณ„์† ์žฌ์‚ฌ์šฉ๋œ๋‹ค. Why? ๋‚ด๋ถ€ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ฐ”๋€Œ์ง€ ์•Š๋Š” ๊ฐ์ฒด๋“ค์„ ์—ฌ๋Ÿฌ ๊ฐœ ์ƒ์„ฑํ•ด์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ์ž์› ๋‚ญ๋น„์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
    • ๊ทธ๋ž˜์„œ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์‹ฑ๊ธ€ํ†ค ๊ด€๋ฆฌ ์ปจํ…Œ์ด๋„ˆ๋ผ๊ณ  ๋ถ€๋ฅด๋Š” ์‚ฌ๋žŒ๋“ค๋„ ์žˆ๋Š” ๊ฒƒ ๊ฐ™๋‹ค.
  • ์ผ๋ฐ˜์ ์ธ ์ƒํ™ฉ์—์„œ๋Š” ๊ฐœ๋ฐœ์ž๊ฐ€ ์ง์ ‘ ๊ฐ์ฒด๋ฅผ ์ œ์–ดํ•ด์•ผ ํ–ˆ๋‹ค. new ์—ฐ์‚ฐ์ž๋ฅผ ํ†ตํ•ด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ๊ฐ์ฒด์˜ ์˜์กด์„ฑ์„ ๋งบ์–ด์ฃผ๊ณ , ์ดˆ๊ธฐํ™”๋ฅผ ํ•ด์ฃผ๊ณ  ๋“ฑ๋“ฑ..
  • Spring ์—์„œ๋Š” xmlํŒŒ์ผ ๋˜๋Š” ์–ด๋…ธํ…Œ์ด์…˜ ๋ฐฉ์‹์œผ๋กœ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์— Bean(๊ฐ์ฒด)๋ฅผ ๋“ฑ๋กํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด, ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์—์„œ Bean์˜ ์ƒ๋ช…์ฃผ๊ธฐ(์ƒ์„ฑ -> ์˜์กด์„ฑ ์„ค์ • -> ์ดˆ๊ธฐํ™” -> ์†Œ๋ฉธ)๋ฅผ ์ „๋ถ€ ๊ด€๋ฆฌํ•ด์ค€๋‹ค.
  • ์ฆ‰, ๊ฐ์ฒด์— ๋Œ€ํ•œ ์ œ์–ด๊ถŒ์ด ์ปจํ…Œ์ด๋„ˆ๋กœ ์—ญ์ „๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ œ์–ด์˜ ์—ญ์ „์ด๋ผ๊ณ  ํ•˜๋Š” ๊ฒƒ

  • ์ผ๋ฐ˜์ ์œผ๋กœ ์ฒ˜์Œ์— ๋ฐฐ์šฐ๋Š” ์ž๋ฐ” ํ”„๋กœ๊ทธ๋žจ์—์„œ๋Š” ๊ฐ ๊ฐ์ฒด๋“ค์ด ํ”„๋กœ๊ทธ๋žจ์˜ ํ๋ฆ„์„ ๊ฒฐ์ •ํ•˜๊ณ  ๊ฐ ๊ฐ์ฒด๋ฅผ ์ง์ ‘ ์ƒ์„ฑํ•˜๊ณ  ์กฐ์ž‘ํ•˜๋Š” ์ž‘์—…(๊ฐ์ฒด๋ฅผ ์ง์ ‘ ์ƒ์„ฑํ•˜์—ฌ ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ)์„ ํ–ˆ๋‹ค. ์ฆ‰, ๋ชจ๋“  ์ž‘์—…์„ ์‚ฌ์šฉ์ž๊ฐ€ ์ œ์–ดํ•˜๋Š” ๊ตฌ์กฐ์˜€๋‹ค.
  • ์˜ˆ๋ฅผ ๋“ค์–ด A ๊ฐ์ฒด์—์„œ B ๊ฐ์ฒด์— ์žˆ๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์œผ๋ฉด, B ๊ฐ์ฒด๋ฅผ ์ง์ ‘ A ๊ฐ์ฒด ๋‚ด์—์„œ ์ƒ์„ฑํ•˜๊ณ  ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.
  • ํ•˜์ง€๋งŒ IOC๊ฐ€ ์ ์šฉ๋œ ๊ฒฝ์šฐ, ๊ฐ์ฒด์˜ ์ƒ์„ฑ์„ ํŠน๋ณ„ํ•œ ๊ด€๋ฆฌ ์œ„์ž„ ์ฃผ์ฒด์—๊ฒŒ ๋งก๊ธด๋‹ค. ์ด ๊ฒฝ์šฐ ์‚ฌ์šฉ์ž๋Š” ๊ฐ์ฒด๋ฅผ ์ง์ ‘ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ , ๊ฐ์ฒด์˜ ์ƒ๋ช…์ฃผ๊ธฐ๋ฅผ ์ปจํŠธ๋กคํ•˜๋Š” ์ฃผ์ฒด๋Š” ๋‹ค๋ฅธ ์ฃผ์ฒด๊ฐ€ ๋œ๋‹ค.
  • ์ฆ‰, ์‚ฌ์šฉ์ž์˜ ์ œ์–ด๊ถŒ์„ ๋‹ค๋ฅธ ์ฃผ์ฒด์—๊ฒŒ ๋„˜๊ธฐ๋Š” ๊ฒƒ์„ IOC(์ œ์–ด์˜ ์—ญ์ „) ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

์žฅ์ 

  • ๊ฐœ๋ฐœ์ž๋Š” ๊ฐ์ฒด ๊ด€๋ฆฌ์— ๋œ ์‹ ๊ฒฝ์“ธ ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.
  • ์•ฝํ•œ ๊ฒฐํ•ฉ์„ ์ด์šฉํ•˜์—ฌ ๊ฐ์ฒด ๊ฐ„ ์˜์กด ๊ด€๊ณ„๋ฅผ ์‰ฝ๊ฒŒ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๊ฒฐ๊ณผ์ ์œผ๋กœ ์ฝ”๋“œ์˜ ์žฌ์‚ฌ์šฉ์„ฑ๊ณผ ์œ ์ง€๋ณด์ˆ˜์„ฑ์„ ๋†’์ธ๋‹ค.

3. Spring Bean(์Šคํ”„๋ง ๋นˆ)

์Šคํ”„๋ง ๋นˆ(Spring Bean) : Spring IoC ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ๊ด€๋ฆฌํ•˜๋Š” ์ž๋ฐ” ๊ฐ์ฒด๋ฅผ ๋งํ•œ๋‹ค.

  • ์šฐ๋ฆฌ๊ฐ€ ์•Œ๋˜ ๊ธฐ์กด์˜ Java Programming ์—์„œ๋Š” Class๋ฅผ ์ƒ์„ฑํ•˜๊ณ  new๋ฅผ ์ž…๋ ฅํ•˜์—ฌ ์›ํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ์ง์ ‘ ์ƒ์„ฑํ•œ ํ›„์— ์‚ฌ์šฉํ–ˆ์—ˆ๋‹ค.
  • ํ•˜์ง€๋งŒ Spring์—์„œ๋Š” ์ง์ ‘ new๋ฅผ ์ด์šฉํ•˜์—ฌ ์ƒ์„ฑํ•œ ๊ฐ์ฒด๊ฐ€ ์•„๋‹ˆ๋ผ, Spring์— ์˜ํ•˜์—ฌ ๊ด€๋ฆฌ๋‹นํ•˜๋Š” ์ž๋ฐ” ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ์ด๋ ‡๊ฒŒ Spring์— ์˜ํ•˜์—ฌ ์ƒ์„ฑ๋˜๊ณ  ๊ด€๋ฆฌ๋˜๋Š” ์ž๋ฐ” ๊ฐ์ฒด๋ฅผ Bean์ด๋ผ๊ณ  ํ•œ๋‹ค.
  • Spring Framework ์—์„œ๋Š” Spring Bean ์„ ์–ป๊ธฐ ์œ„ํ•˜์—ฌ ApplicationContext.getBean() ์™€ ๊ฐ™์€ ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Spring ์—์„œ ์ง์ ‘ ์ž๋ฐ” ๊ฐ์ฒด๋ฅผ ์–ป์–ด์„œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

3-1 Spring Bean์„ Spring IoC Container์— ๋“ฑ๋กํ•˜๋Š” ๋ฐฉ๋ฒ•

  • Spring Bean์„ Spring IoC Container์— ๋“ฑ๋กํ•˜๋Š” ๋ฐฉ๋ฒ•์€ 2๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค.

3-1-1 ์ž๋ฐ” ์–ด๋…ธํ…Œ์ด์…˜(Java Annotation)์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•

  • ์ž๋ฐ” ์–ด๋…ธํ…Œ์ด์…˜ ์ฐธ๊ณ !

    • JAVA์—์„œ ์–ด๋…ธํ…Œ์ด์…˜ ์ด๋ผ๋Š” ๊ธฐ๋Šฅ์ด ์žˆ๋‹ค. ์‚ฌ์ „์ƒ์œผ๋กœ๋Š” ์ฃผ์„์˜ ์˜๋ฏธ์ด์ง€๋งŒ Java ์—์„œ๋Š” ์ฃผ์„ ์ด์ƒ์˜ ๊ธฐ๋Šฅ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.
    • ์–ด๋…ธํ…Œ์ด์…˜์€ ์ž๋ฐ” ์†Œ์Šค ์ฝ”๋“œ์— ์ถ”๊ฐ€ํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ์˜ ์ผ์ข…์ด๋‹ค.
    • ์†Œ์Šค์ฝ”๋“œ์— ์ถ”๊ฐ€ํ•˜๋ฉด ๋‹จ์ˆœ ์ฃผ์„์˜ ๊ธฐ๋Šฅ์„ ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ํŠน๋ณ„ํ•œ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์–ด๋…ธํ…Œ์ด์…˜ ์˜ˆ์ œ

    • Java์—์„œ๋Š” @Override, @Deprecated ์™€ ๊ฐ™์€ ๊ธฐ๋ณธ์ ์ธ ์–ด๋…ธํ…Œ์ด์…˜์„ ์ œ๊ณตํ•œ๋‹ค. ์•„๋ž˜์˜ ์ƒ์† ์˜ˆ์ œ์—์„œ๋Š” @Override ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ƒ์†์ž„์„ ๋ช…์‹œํ•ด์ค€๋‹ค.
    public class Parent { 
        public void doSomething() { 
            System.out.println("This is Parent"); 
        } 
    } 
    
    public class Son extends Parent{ 
        @Override 
        public void doSomething() { 
            System.out.println("This is Son"); 
        } 
    }
  • ๊ทธ๋Ÿผ ์‹ค์ œ Spring์—์„œ ์–ด๋–ค ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•ด Bean ๋“ฑ๋กํ• ๊นŒ?

    • @Component ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•ด Bean์„ ๋“ฑ๋กํ•œ๋‹ค.
    • @Component ์–ด๋…ธํ…Œ์ด์…˜์ด ๋“ฑ๋ก๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ์—๋Š” Spring์ด Annotation์„ ํ™•์ธํ•˜๊ณ  ์ž์ฒด์ ์œผ๋กœ Bean์œผ๋กœ ๋“ฑ๋กํ•œ๋‹ค.
  • Spring @Component ์˜ˆ์ œ

    @Controller
    public class MemberController {
    
        private final MemberService memberService;
    
        @Autowired
        public MemberController(MemberService memberService) {
            this.memberService = memberService;
        }
    
        @GetMapping("/members/new")
        public String createForm(){
            return "/members/createMemberForm";
        }
    }
    • ์™œ @Component๊ฐ€ ์—†์ง€? ๋ผ๊ณ  ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•ด๋‹น ๋ถ€๋ถ„์„ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ธํ…”๋ฆฌ์ œ์ด๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด Ctrl์„ ๋ˆ„๋ฅธ ์ƒํƒœ๋กœ @Controller๋ฅผ ํด๋ฆญํ•ด๋ณธ๋‹ค. ๊ทธ๋Ÿผ ์•„๋ž˜์™€ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

      @Target(ElementType.TYPE)
      @Retention(RetentionPolicy.RUNTIME)
      @Documented
      @Component
      public @interface Controller {
      
          /**
           * The value may indicate a suggestion for a logical component name,
           * to be turned into a Spring bean in case of an autodetected component.
           * @return the suggested component name, if any (or empty String otherwise)
           */
          @AliasFor(annotation = Component.class)
          String value() default "";
      
      }
    • ์œ„ @Controller ์–ด๋…ธํ…Œ์ด์…˜ ์ฝ”๋“œ์—์„œ @Component๊ฐ€ ์žˆ์Œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

    • @Component ์–ด๋…ธํ…Œ์ด์…˜์„ ํ†ตํ•ด Spring์€ ํ•ด๋‹น Controller๋ฅผ Bean์œผ๋กœ ๋“ฑ๋กํ•œ๋‹ค.

3-1-2. Bean Configuration File์— ์ง์ ‘ Bean ๋“ฑ๋กํ•˜๋Š” ๋ฐฉ๋ฒ•

  • @Configuration๊ณผ @Bean ์–ด๋…ธํ…Œ์ด์…˜์„ ์ด์šฉํ•˜์—ฌ Bean์„ ๋“ฑ๋กํ•  ์ˆ˜ ์žˆ๋‹ค.

  • ์•„๋ž˜์˜ ์˜ˆ์ œ์™€ ๊ฐ™์ด @Configuration์„ ์ด์šฉํ•˜๋ฉด Spring ํ”„๋กœ์ ํŠธ์—์„œ์˜ Configuration ์—ญํ• ์„ ํ•˜๋Š” Class๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

  • ํ•ด๋‹น ํŒŒ์ผ ํ•˜์œ„์— Bean์œผ๋กœ ๋“ฑ๋กํ•˜๊ณ ์ž ํ•˜๋Š” Class์— @Bean ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•ด์ฃผ๋ฉด ๊ฐ„๋‹จํ•˜๊ฒŒ Bean์„ ๋“ฑ๋กํ•  ์ˆ˜ ์žˆ๋‹ค.

    @Configuration
    public class SpringConfig {
        private final DataSource dataSource;
        private final EntityManager em;
        @Autowired
        public SpringConfig(DataSource dataSource, EntityManager em) {
            this.dataSource = dataSource;
            this.em = em;
        }
    
        @Bean
        public MemberService memberService(){
            return new MemberService(memberRepository());
        }
    
        @Bean
        public MemberRepository memberRepository(){
            return new JpaMemberRepository(em);
        }
    }

์ฐธ๊ณ ์ž๋ฃŒ

์˜์กด์„ฑ ์ฃผ์ž…

์ œ์–ด์˜ ์—ญ์ „

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

profile
์–ด๋– ํ•œ ๊ฐ€์น˜๋ฅผ ์ฐฝ์ถœํ•  ์ˆ˜ ์žˆ์„๊นŒ๋ฅผ ๊ณ ๋ฏผํ•˜๋Š” ๊ฐœ๋ฐœ์ž. ์ฃผ๋กœ Spring์œผ๋กœ ๊ฐœ๋ฐœํ•ด์š”. https://comclothing.tistory.com/ ๐Ÿ‘ˆ๐Ÿปํ‹ฐ์Šคํ† ๋ฆฌ ๋ธ”๋กœ๊ทธ๋กœ ์ด์ „ ์™„๋ฃŒ

0๊ฐœ์˜ ๋Œ“๊ธ€