[Spring Boot] Spring Bean๊ณผ ์˜์กด๊ด€๊ณ„

2021. 8. 12. 08:00ใ†Back-End ์ž‘์—…์‹ค/Spring Framework

728x90
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š”? ์ฃผ๋‹ˆํ•˜๋ž‘ ์ž…๋‹ˆ๋‹ค.

์ฃผ๋‹ˆํ•˜๋ž‘์€ Spring Boot์— ๋Œ€ํ•ด์„œ ๊ณต๋ถ€ํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ์ด์—์š”.

์˜ค๋Š˜์€ Spring Bean๊ณผ ์˜์กด๊ด€๊ณ„์— ๋Œ€ํ•ด ๊ณต๋ถ€ ํ•ด ๋ณด๋„๋ก ํ•  ๊ฒƒ์ด์—์š”.

๋ฐ”๋กœ ์‹œ์ž‘ ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค!

 

์ฝ”๋“œ์— ๊ด€๋ จํ•œ ๋‚ด์šฉ์€ ์ฃผ๋‹ˆํ•˜๋ž‘์˜ Github์—์„œ ํ™•์ธ ํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!



 


 

๐Ÿ“‹ ๋ชฉ์ฐจ


01.[Spring Boot] thymeleaf์™€ Spring Boot
02.[Spring Boot] Spring Web ๊ฐœ๋ฐœ ๊ธฐ์ดˆ
03.[Spring Boot] ํšŒ์› ๊ด€๋ฆฌ ์˜ˆ์ œ - Backend
04.[Spring Boot] Service ๊ฐœ๋ฐœ ๋ฐ Test Case ์ž‘์„ฑ
05.[Spring Boot] Spring Bean๊ณผ ์˜์กด๊ด€๊ณ„
06.[Spring Boot] Java Code๋กœ ์ง์ ‘ Spring Bean ๋“ฑ๋ก
07.[Spring Boot] ๋“ฑ๋ก, ๋ชฉ๋ก ๋ณด๊ธฐ ๊ตฌํ˜„ํ•˜๊ธฐ
08.[Spring Boot] ๊ธฐ์กด ์ฝ”๋“œ ์† ์•ˆ๋Œ€๊ณ , ์„ค์ •์œผ๋กœ ๊ตฌํ˜„ Class ๋ณ€๊ฒฝ
09.[Spring Boot] ํ†ตํ•ฉ Test
10.[Spring Boot] JPA
11.[Spring Boot] Spring Data JPA
12.[Spring Boot] AOP

 

 


 

 

 

๐Ÿ“Œ Spring Bean๊ณผ ์˜์กด๊ด€๊ณ„


Spring Bean์„ ๋“ฑ๋กํ•˜๋Š” 2๊ฐ€์ง€ ๋ฐฉ๋ฒ•!

  • Component Scan / ์ž๋™ ์˜์กด๊ด€๊ณ„ ์„ค์ •
  • Java code๋กœ ์ง์ ‘ Spring Bean ๋“ฑ๋ก

 

๋จผ์ € Component Scan๊ณผ ์ž๋™ ์˜์กด๊ด€๊ณ„์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณผ ๊ฒƒ์ด์—์š”.

ํšŒ์› Controller๊ฐ€ ํšŒ์› Service์™€ ํšŒ์› Repository๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ์˜์กด ๊ด€๊ณ„๋ฅผ ์ค€๋น„ํ•ด๋ด์š”!

 

 

ํšŒ์› Controller์— ์˜์กด ๊ด€๊ณ„ ์ถ”๊ฐ€

package hello.hellospring.controller;

import hello.hellospring.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

// ์ปดํฌ๋„ŒํŠธ ์Šค์บ” ๋ฐฉ์‹์˜ Controller
// ์ด ์–ด๋…ธํ…Œ์ด์…˜์„ ์Šคํ”„๋ง์ด ๋ณด๊ณ , ํ•ด๋‹น Controller Class๋ฅผ ๊ฐ์ฒด๋กœ ๋งŒ๋“  ๋’ค ์Šคํ”„๋ง์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š”๋‹ค.
@Controller     
// ๊ทธ๊ฒƒ์„ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์—์„œ ๋นˆ์œผ๋กœ ๊ด€๋ฆฌ ๋œ๋‹ค๊ณ  ํ‘œํ˜„ํ•œ๋‹ค.
public class MemberController {
        // ์•„๋ž˜์™€ ๊ฐ™์ด MemberService๋ฅผ new๋ฅผ ํ†ตํ•ด ์ƒ์„ฑํ•˜๊ฒŒ ๋˜๋ฉด ๋‹ค๋ฅธ Controller์—์„œ๋„ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•œ ๋ฌธ์ œ
//    private final MemberService memberService = new MemberService();

    private final MemberService memberService;

    // Dependency Injection (DI)
    // @controller๊ฐ€ ์žˆ๋Š” Controller ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ, @Autowired๊ฐ€ ๋ถ™์€ ์ƒ์„ฑ์ž๋Š” ์Šคํ”„๋ง์ด ์ปจํ…Œ์ด๋„ˆ์—์„œ ๋นˆ์œผ๋กœ ๊ด€๋ฆฌํ•  ๋•Œ, ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•˜๋Š”๋ฐ, ์ด ๋•Œ MemberService๋ฅผ ๋„ฃ์–ด์ค€๋‹ค.
    @Autowired
    public MemberController(MemberService memberService) {
        this.memberService = memberService;
    } // ์ƒ์„ฑ์ž () ๋
} // Class () ๋

 

๋จผ์ € 13๋ฒˆ Line์— ์ฃผ์„์„ ์ณ๋†จ๋Š”๋ฐ, ์ด๊ฒƒ์ฒ˜๋Ÿผ new๋ฅผ ํ†ตํ•ด Service๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ฒŒ ๋˜๋ฉด Member์™€ ๊ด€๊ณ„ ์—†๋Š” ์—ฌ๋Ÿฌ Controller๋„ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ์ด์—์š”.
๊ทธ๋ž˜์„œ ๋จผ์ € ํ•„๋“œ ๋ณ€์ˆ˜๋กœ MemberService๋ฅผ ์„ ์–ธํ•œ ๋’ค์— '@Autowired'๋ฅผ ํ†ตํ•ด์„œ ์ƒ์„ฑ์ž๋ฅผ ์ฃผ์ž…ํ•ด ์ฃผ๋Š” ๋ฐฉ์‹์œผ๋กœ DI๋ฅผ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด์—์š”.

์ด๋ ‡๊ฒŒ ๋˜๋ฉด @Controller๊ฐ€ ์žˆ๋Š” Controller ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ, @Autowired๊ฐ€ ๋ถ™์€ ์ƒ์„ฑ์ž๋Š” Spring์ด Container์—์„œ Bean์œผ๋กœ ๊ด€๋ฆฌํ•  ๋•Œ, ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•˜๋Š”๋ฐ, ์ด ๋•Œ ํ•„๋“œ ๋ณ€์ˆ˜๋ฅผ ๋„ฃ์–ด ์ฃผ๋„๋ก ํ•ด ์ฃผ๋Š” ์นœ๊ตฌ์—์š”!

 

์ข€ ๋” ์ •๋ฆฌ๋ฅผ ํ•˜์ž๋ฉด ์ƒ์„ฑ์ž์— @Autowired๊ฐ€ ์žˆ์œผ๋ฉด Spring์ด ์—ฐ๊ด€ ๋œ ๊ฐ์ฒด๋ฅผ Spring Container์—์„œ ์ฐพ์•„์„œ ๋„ฃ์–ด์ค€๋‹ค๋กœ ์ •๋ฆฌํ•  ์ˆ˜ ์žˆ๊ฒ ๋„ค์š”!
์ด๋ ‡๊ฒŒ ๊ฐ์ฒด ์˜์กด๊ด€๊ณ„๋ฅผ ์™ธ๋ถ€์—์„œ ๋„ฃ์–ด์ฃผ๋Š” ๊ฒƒ์„ Dependency Injection (DI)๋ผ๊ณ  ํ•˜๊ณ , ์šฐ๋ฆฌ๋‚˜๋ผ ๋ง๋กœ ํ•˜๋ฉด ์˜์กด์„ฑ ์ฃผ์ž…์ด ๋˜๋Š” ๊ฒƒ์ด๋ž๋‹ˆ๋‹ค!

 

 

๐Ÿ’ก์ฐธ๊ณ  :
helloController๋Š” Spring์ด ์ œ๊ณตํ•˜๋Š” Controller์—ฌ์„œ Spring Bean์œผ๋กœ ์ž๋™ ๋“ฑ๋ก.
@Controller๊ฐ€ ์žˆ์œผ๋ฉด ์ž๋™ ๋“ฑ๋ก.

 

 

   ๐Ÿ“ Component Scan ์›๋ฆฌ


@Component : Spring Bean์œผ๋กœ ์ž๋™ ๋“ฑ๋ก

@Controller : Cotroller๊ฐ€ Spring Bean์œผ๋กœ ์ž๋™ ๋“ฑ๋ก๋œ ์ด์œ ๋Š” Component Scan ๋•Œ๋ฌธ

 

@Component๋ฅผ ํฌํ•จํ•˜๋Š” ๋‹ค์Œ Annotation๋„ Spring Bean์œผ๋กœ ์ž๋™ ๋“ฑ๋ก

  - @Contrller

  - @Service

  - @Repository

 

 

ํšŒ์› Service Spring Bean ๋“ฑ๋ก

@Service
public class MemberService {

	private final MemberRepository memberRepository;
    
    @Autowired
    public MemberService ( MemberRepository memberRepository ) {
    	this.memberRepository = memberRepository;
    } // ์ƒ์„ฑ์ž ๋

} // Class ๋

 

๐Ÿ’ก์ฐธ๊ณ  :

์ƒ์„ฑ์ž์— '@Autowired' ์ƒ์„ฑ ์‹œ ๊ฐ์ฒด ์ƒ์„ฑ ์‹œ์ ์— Spring Container์—์„œ ํ•ด๋‹น Spring Bean์„ ์ฐพ์•„ ์ฃผ์ž…
์ƒ์„ฑ์ž๊ฐ€ 1๊ฐœ๋งŒ ์žˆ๋‹ค๋ฉด 'Autowired' ์ƒ๋žต ๊ฐ€๋Šฅ

 

 

ํšŒ์› Repository Spring Bean ๋“ฑ๋ก

@Repository
public class MemoryMemberRepository implements MemberRepository {
} // MemoryMemberRepository() ๋

์œ„์™€ ๊ฐ™์ด ๊ธฐ์กด์— ๋งŒ๋“ค์—ˆ๋˜ MemoryMemberRepository์— '@Repository'๋ฅผ ์ถ”๊ฐ€ํ•ด์คŒ์œผ๋กœ์จ,
Spring Bean์„ ๋“ฑ๋กํ•˜์—ฌ ์ค€ ๊ฒƒ์ด์—์š”!

 

 

memberService์™€ memberRepository๊ฐ€ Spring Container์— Spring Bean์œผ๋กœ ๋“ฑ๋ก ๋˜์–ด ์žˆ๋Š” ๊ฒƒ์ด์—์š”.

 

๐Ÿ’ก์ฐธ๊ณ  :

Spring์€ Spring Container์— Spring Bean์„ ๋“ฑ๋ก ์‹œ ๊ธฐ๋ณธ์œผ๋กœ Sigle Tone์œผ๋กœ ๋“ฑ๋ก (์œ ์ผํ•˜๊ฒŒ ํ•˜๋‚˜๋งŒ ๋“ฑ๋กํ•ด์„œ ๊ณต์œ )
๋”ฐ๋ผ ๊ฐ™์€ Spring Bean์ด๋ฉด ๋ชจ๋‘ ๊ฐ™์€ ์ธ์Šคํ„ด์Šค์ธ ์…ˆ์ด๋‹ค.
์„ค์ •์œผ๋กœ Single Tone์ด ์•„๋‹ˆ๊ฒŒ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์œผ๋‚˜, ํŠน๋ณ„ํ•œ ๊ฒฝ์šฐ๊ฐ€ ์•„๋‹ˆ๊ณ ์„œ๋Š” ๋Œ€๋ถ€๋ถ„ Single Tone ์‚ฌ์šฉ

 

 

 


์ฃผ๋‹ˆํ•˜๋ž‘์˜ ๊ธ€์ด ๋งˆ์Œ์— ๋“œ์…จ๋‚˜์š”? ๊ตฌ๋…๊ณผ ๊ณต๊ฐ! ๊ทธ๋ฆฌ๊ณ , ๋Œ“๊ธ€์€ ์ฃผ๋‹ˆํ•˜๋ž‘์—๊ฒŒ ๋งŽ์€ ํž˜์ด ๋ฉ๋‹ˆ๋‹ค

728x90
๋ฐ˜์‘ํ˜•