Spring IoC ์ปจํ
์ด๋์ ์ํด ๊ด๋ฆฌ๋๋ ๊ฐ์ฒด
๋น์ ์คํ๋ง ์ ํ๋ฆฌ์ผ์ด์
์ ํต์ฌ ๊ตฌ์ฑ ์์๋ก,
์ ํ๋ฆฌ์ผ์ด์
์ ๋ค์ํ ๋ถ๋ถ์์ ์ฌ์ฌ์ฉ๋๊ณ , ๊ด๋ฆฌ๋ฉ๋๋ค.
Spring Boot๋ ์๋ ์ค์ , ๊ฐํธํ ๊ตฌ์ฑ ๋ฐ ์คํ๋ง์ ๊ฐ๋ ฅํ
๋น ๊ด๋ฆฌ ๊ธฐ๋ฅ์ ํตํด ์ ํ๋ฆฌ์ผ์ด์
๊ฐ๋ฐ์ ๊ฐ์ํํฉ๋๋ค.
@Service, @Controller, @Model, @Repository์ ๊ฐ์ ์ ๋
ธํ
์ด์
์ ์ฌ์ฉํ์ฌ
์ ์๋ ํด๋์ค๋ค์ ๋ชจ๋ ๋น(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 {
// ๋ฐ์ดํฐ ์ ๊ทผ ๋ก์ง
}
@Component์ ์ฌ์ฉํ์ฌ ์ผ๋ฐ์ ์ธ ๋น์ ์ ์ํฉ๋๋ค.
ํน์ ์ญํ ์ ๊ตฌ์ ๋ฐ์ง ์๊ณ ๋ชจ๋ ์ข
๋ฅ์ ๋น์ ์ ์ํ ๋ ์ฌ์ฉํฉ๋๋ค.