์์กด์ฑ ์ฃผ์
์์กด์ฑ ์ฃผ์ (Dependency Injection)์ ์๋ฏธ๋ฅผ ์์๋ณด๊ณ ์คํ๋ง ๋ถํธ๋ก ์์กด์ฑ ์ฃผ์ ์ ํด๋ณด์.
์์
์์กด์ฑ์ ์๋ฏธ๋ ๋ฌด์์ผ๊น?
๊ฐ๋จํ Test ์ฉ Repository์ Controller๊ฐ ์๋ค๊ณ ํด๋ณด์.
package com.vividswan.spring.web.domain.test;
import org.springframework.stereotype.Component;
public class TestRepository {
public String findStrig(){
return "This Is Test!!";
};
}
package com.vividswan.spring.web;
import com.vividswan.spring.web.domain.test.TestRepository;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
TestRepository testRepository = new TestRepository();
//์์ฑ์ ์ฌ์ฉ
@GetMapping("/test")
public String test(){
return testRepository.findStrig();
}
}
/test
๊ฒฝ๋ก๋ก ๋งคํ์ ํ๋ฉด "This Is Test!!"๋ฅผ ๋ฐํํ๋ ๊ฐ๋จํ ์ปจํธ๋กค๋ฌ๋ค.
์ด๋, ์ด ์ปจํธ๋กค๋ฌ์์ TestRepository
๋ผ๋ ๊ฐ์ฒด๊ฐ ํ์ํ๋ค๋ฉด ์์ ์ฝ๋์ฒ๋ผ ์์ฑ์๋ฅผ ์ด์ฉํด์ผ ํ๋ค.
์์กด์ด๋?
์์ ์ํฉ์ด TestController ๊ฐ์ฒด๊ฐ TestRepository์ ์์กดํ๋ ์ํฉ์ด๋ค.
A๋ผ๋ ๊ฐ์ฒด๊ฐ B์ ์์กดํ๋ค๋ ๊ฒ์, A๊ฐ B๋ฅผ ์ฌ์ฉํ๋ค๋ ๋ป์ด๊ณ , ๊ทธ ๋ง์ B๊ฐ ๋ฐ๋๋ฉด A์๊ฒ๋ ์ํฅ์ ๋ผ์น๋ค๋ ์๋ฏธ๋ค.
์์ ์์ ๋ ๊ฐ๋จํ ์์ค์์ง๋ง, Repository๊ฐ ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ ๊ฐ์ฒด์๊ณ , ๋ค๋ฅธ ๊ฒ์ผ๋ก Repository๊ฐ ๊ตฌํ๋๋ค๋ฉด, TestController์ ๋ด์ฉ๋ ์์ ํด์ผ ํ๋ค.
์์ ๊ท๋ชจ๊ฐ ์๋ ํฐ ํ๋ก์ ํธ์์ ์ด๋ฌํ ์์กด์ฑ์ด ์๋ ๊ฐ์ฒด๋ค๋ผ๋ฆฌ์ ์ ์ง ๋ณด์์ฑ์ด ์ด๋ ค์์ง๋ค.
์คํ๋ง ํ๋ ์์ํฌ์์๋ ์ด๊ฒ์ ์คํ๋ง์ด ์ง์ ์์กด์ฑ ๊ด๋ฆฌ๋ฅผ ํ๋ ๋ฐฉ๋ฒ์ผ๋ก ์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ค.
Spring IoC
์คํ๋ง์์ , Spring IoC ์ปจํ
์ด๋์ ๊ฐ์ฒด๋ค์ ๋ฑ๋กํด ์ฃผ๋ฉด, ์คํ๋ง์ด ๊ทธ ์์์ ๊ฐ์ฒด๋ค์ ์์กด์ฑ ์ฃผ์
์ ๊ด๋ฆฌํด ์ค๋ค.
์ฌ๋ฌ ๊ฐ์ง ๋ฐฉ๋ฒ์ด ์์ง๋ง, ์คํ๋ง์ด ๊ฐ์ฒด๋ฅผ ์ธ์ํ๊ณ ์ปจํ
์ด๋์ ๋ฑ๋กํ ์ ์๊ฒ ์ด๋
ธํ
์ด์
์ ์ด์ฉํด์ ์์กด์ฑ ์ฃผ์
์ ๊ด๋ฆฌํ๊ฒ ํด์ฃผ์.
์ด๋ ธํ ์ด์ ์ฌ์ฉ
package com.vividswan.spring.web.domain.test;
import org.springframework.stereotype.Component;
@Component
public class TestRepository {
public String findStrig(){
return "This Is Test!!";
};
}
package com.vividswan.spring.web;
import com.vividswan.spring.web.domain.test.TestRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Autowired
TestRepository testRepository;
@GetMapping("/test")
public String test(){
return testRepository.findStrig();
}
}
Repository์๋ ์คํ๋ง ์ปจํ
์ด๋๊ฐ ์ธ์ํ ์ ์๊ฒ @Component๋ฅผ ์ฌ์ฉํ๋ค.
Controller์์ ์์ฑ์๋ฅผ ์ด์ฉํ๋ ๋ฐฉ์์ ์์ ๊ณ , @Autowired๋ฅผ ํตํด Repository๋ฅผ ์๋์ผ๋ก ์์กด์ฑ ์ฃผ์
ํ๊ฒ ํ๋ค.
์ด๋ ๊ฒ ํด๊ฒฐํ๋ฉด Repository์ ๊ตฌํ์ฒด๊ฐ ๋ณํ๋ค๊ณ ํด๋, TestController์ ์ ์ง ๋ณด์๊ฐ ํ์ํ์ง ์๋ค.
์๋ฅผ ๋ค์ด, TestRepository๋ฅผ ์ธํฐํ์ด์ค๋ก ๋ฐ๊พธ๊ณ , ๊ทธ์ ๋ํ ๊ตฌํ์ฒด๋ฅผ ๋ฐ๋ก ๋ง๋๋ ๊ฒ์ผ๋ก Repository๊ฐ ๋ณํ๋ ์ํฉ์ด๋ผ๊ณ ํ์.
package com.vividswan.spring.web.domain.test;
public interface TestRepository {
public String findStrig();
}
package com.vividswan.spring.web.domain.test;
import org.springframework.stereotype.Component;
@Component
public class TestRepositoryImp implements TestRepository {
public String findStrig(){
return "This Is Test!!";
};
}
์ด๋ฐ ์์ผ๋ก ๊ธฐ์กด์ TestRepository๋ ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ๋ TestRepositoryImp
๋ก ๋ณ๊ฒฝํ๋ค.
๋ ๊ธฐ์กด์ TestRepository
์ ์ด๋ฆ์ผ๋ก ์ธํฐํ์ด์ค๋ฅผ ๋ง๋ ๋ค.
ํธ์์ฑ
์์ ๊ฐ์ ์ํฉ์ด ๋ผ๋ TestController๋ ๋ฐ๋ก ์ ์ง ๋ณด์ํ ํ์๊ฐ ์๋ค.
@Autowired
TestRepository testRepository;
์ด ์์ค๋ง์ผ๋ก @Component ์ด๋
ธํ
์ด์
์ด ์๋ TestRepositoryImp
๊ตฌํ ๊ฐ์ฒด๋ฅผ ์์กด์ฑ ์ฃผ์
ํด ์ค๋ค.
์ด๋ ๊ฒ Repository์ ๊ตฌํ์ฒด๊ฐ ๋ฐ๋์ด๋ ์คํ๋ง IoC๋ ๊ทธ์ ๋ง๋ ๊ตฌํ์ฒด๋ฅผ ์ฃผ์
ํด ์ฃผ๋ ํธ์์ฑ์ด ์๋ค.
'Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
enum ํ์ฉ(RoleType) (0) | 2022.03.10 |
---|---|
Inversion of Control (์ ์ด์ ์ญ์ ) (0) | 2022.03.09 |
MockMvc - ๊ฐ๋จํ ํ ์คํธ ์ฝ๋ (0) | 2022.03.09 |
์คํ๋ง์์ h2 ์ฌ์ฉ (0) | 2022.03.09 |
Lombok ์ค์น ๋ฐ ์ค๋ฅ ํด๊ฒฐ (0) | 2022.03.08 |