Skip to content

Latest commit

ย 

History

History
62 lines (47 loc) ยท 1.96 KB

File metadata and controls

62 lines (47 loc) ยท 1.96 KB

1.Bean

Spring IoC ์ปจํ…Œ์ด๋„ˆ์— ์˜ํ•ด ๊ด€๋ฆฌ๋˜๋Š” ๊ฐ์ฒด
๋นˆ์€ ์Šคํ”„๋ง ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ํ•ต์‹ฌ ๊ตฌ์„ฑ ์š”์†Œ๋กœ, 
์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋‹ค์–‘ํ•œ ๋ถ€๋ถ„์—์„œ ์žฌ์‚ฌ์šฉ๋˜๊ณ , ๊ด€๋ฆฌ๋ฉ๋‹ˆ๋‹ค.

Spring Boot๋Š” ์ž๋™ ์„ค์ •, ๊ฐ„ํŽธํ•œ ๊ตฌ์„ฑ ๋ฐ ์Šคํ”„๋ง์˜ ๊ฐ•๋ ฅํ•œ 
๋นˆ ๊ด€๋ฆฌ ๊ธฐ๋Šฅ์„ ํ†ตํ•ด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ฐœ๋ฐœ์„ ๊ฐ„์†Œํ™”ํ•ฉ๋‹ˆ๋‹ค.

@Service, @Controller, @Model, @Repository์™€ ๊ฐ™์€ ์• ๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ
์ •์˜๋œ ํด๋ž˜์Šค๋“ค์€ ๋ชจ๋‘ ๋นˆ(Bean)์œผ๋กœ ๋“ฑ๋กํ•ฉ๋‹ˆ๋‹ค.

2.Bean ์˜ˆ์‹œ

2-1.์„œ๋น„์Šค ๊ณ„์ธต์˜ ๋นˆ

@Service ์„์‚ฌ์šฉํ•˜์—ฌ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ์ฒ˜๋ฆฌํ•˜๋Š” ์„œ๋น„์Šค ๊ณ„์ธต์˜ ๋นˆ์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
import org.springframework.stereotype.Service;

@Service
public class MyService {
    // ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง
}

2-2.์ปจํŠธ๋กค๋Ÿฌ ๊ณ„์ธต์˜ ๋นˆ

@Controller๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์›น ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๊ณ , ๋ชจ๋ธ์„ ์—…๋ฐ์ดํŠธํ•˜๊ณ , 
๋ทฐ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ์ปจํŠธ๋กค๋Ÿฌ ๊ณ„์ธต์˜ ๋นˆ์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. MVC ํŒจํ„ด์—์„œ ์ปจํŠธ๋กค๋Ÿฌ ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/example")
public class MyController {

    @GetMapping("/greeting")
    public String greeting() {
        return "greetingView";
    }
}

2-3.๋ฐ์ดํ„ฐ ์ ‘๊ทผ ๊ณ„์ธต์˜ ๋นˆ

@Repository์„ ์‚ฌ์šฉํ•˜์—ฌ๋ฐ์ดํ„ฐ ์ ‘๊ทผ ๊ณ„์ธต์˜ ๋นˆ์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. 
๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€์˜ ์ƒํ˜ธ์ž‘์šฉ์„ ๋‹ด๋‹นํ•˜๋ฉฐ, ์˜ˆ์™ธ๋ฅผ ๋ฐ์ดํ„ฐ ์ ‘๊ทผ ๊ด€๋ จ ์˜ˆ์™ธ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
import org.springframework.stereotype.Repository;

@Repository
public class MyRepository {
    // ๋ฐ์ดํ„ฐ ์ ‘๊ทผ ๋กœ์ง
}

2-4.์ผ๋ฐ˜์ ์ธ ๋นˆ

@Component์„ ์‚ฌ์šฉํ•˜์—ฌ ์ผ๋ฐ˜์ ์ธ ๋นˆ์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. 
ํŠน์ • ์—ญํ• ์— ๊ตฌ์• ๋ฐ›์ง€ ์•Š๊ณ  ๋ชจ๋“  ์ข…๋ฅ˜์˜ ๋นˆ์„ ์ •์˜ํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.