Spring - IOC, DI, Bean
๐DI(Dependency Injection)
์์กด๊ด๊ณ ์ฃผ์
๊ฐ์ฒด ๊ฐ์ ์์กด ๊ด๊ณ๋ฅผ ์ธ๋ถ์์ ๊ฒฐ์ ํ๊ณ ์ฃผ์ ํ๋ ๋์์ธํจํด์ค ํ๋์ด๋ค. DI๋ฅผ ํตํด ๊ฐ์ฒด๋ ์ง์ ์์ ์ด ํ์๋ก ํ๋ ์์กด์ฑ์ ์์ฑํ๊ฑฐ๋ ์ฐธ์กฐํ๋๊ฒ์ด ์๋ ์ธ๋ถ์์ ์ฃผ์ ๋ ์์กด์ฑ์ ์ฌ์ฉํจ. ์ด๋ฅผํตํด ๊ฒฐํฉ๋๋ฅผ ๋ฎ์ถ๊ณ ์ ์ฐ์ฑ ํฅ์์ด ๋ชฉํ
- ํ๋ฆ์นด๋ฉ๋ผ์ ํ๋ฆ์ด ํ๊ตฐ๋ฐ์์ ์์กดํ๊ณ ์๋ค๋ฉด ๊ฒฐํฉ๋๊ฐ ๋์ ์ผํ์ฉ์นด๋ฉ๋ผ (X) - ๋ณ๋ก์
- ํ๋ฆ์นด๋ฉ๋ผ์ ํ๋ฆ์ ์ฃผ์ ํ์ฌ ์์กดํ๊ณ ์๋ค๋ฉด ๊ฒฐํฉ๋๊ฐ ๋ฎ์ ํ๋ฆ๊ต์ฒด์ฉ์นด๋ฉ๋ผ (O) - ๊ตฟ
1.์์ฑ์ ์ฃผ์ (๐ค์ฃผ๋ก์ฌ์ฉ )
- ์์ฑ์ ํธ์ถ์์ ์ ๋ฑ 1๋ฒ๋ง ํธ์ถ๋๋ ๊ฒ์ด ๋ณด์ฅ๋ฉ๋๋ค. ์์ฑ์ ์ฃผ์ ์ ๊ฐ์ฒด๋ฅผ ์์ฑํ ๋ ๋ฑ 1๋ฒ๋ง ํธ์ถ๋๋ฏ๋ก ์ดํ์ ํธ์ถ๋๋ ์ผ์ด ์์ ๋ฐ๋ผ์ ๋ถ๋ณํ๊ฒ ์ค๊ณํ ์ ์๋ค.
- ์์ฑ์ ์ฃผ์ ์ ํ์ฉํ ๊ฒฝ์ฐ ๊ฐ์ ๋ถ๋ณ์ ๋ณด์ฅํ ์ ์์ผ๋ฏ๋ก final์ ํ์ฉ๊ฐ๋ฅ ์์ฑ์๋ฅผ ํตํด ์์กด์ฑ์ ์ค์ ํ๊ณ ๋ณ๊ฒฝํ ์ผ์ด ์์ผ๋ฏ๋ก final์ผ๋ก ์์ ํ๊ฒ ๋ถ๋ณ์ ๋ณด์ฅ
1
2
3
4
5
@Service
@RequiredArgsConstructor <-- Lombok์ผ๋ก ์คํ๋ง์์ DI(์์กด์ฑ ์ฃผ์
)์ ๋ฐฉ๋ฒ ์ค์ ์์ฑ์ ์ฃผ์
์ ์๋์ผ๋ก ์ค์ ํด์ฃผ๋ ์ด๋
ธํ
์ด์
public class CommonServiceImpl implements ICommonService {
private final SpeakerInfoRepository speakerInfoRepository;
}
2.setter(์์ ์) ์ฃผ์
- ๋๋ถ๋ถ์ ์์กด๊ด๊ณ ์ฃผ์ ์ ํ๋ฒ ์ผ์ด๋๋ฉด ์ ํ๋ฆฌ์ผ์ด์ ์ข ๋ฃ์์ ๊น์ง ์์กด๊ด๊ณ๋ฅผ ๋ณ๊ฒฝํ ์ผ์ด ๊ฑฐ์ ์๋ค. ํ์ง๋ง setter๋ ์ธ์ ๋ ๋ณ๊ฒฝ๋๊ฒ ํ ์ํ์ด ์์ -์์ ์ ์ฃผ์ ์ ์ฌ์ฉํ๋ฉด, setXxx ๋ฉ์๋๋ฅผ public์ผ๋ก ์ด์ด๋์ด์ผ ํจ. ์ฆ ์ธ์ ์ด๋์๋ ๋ณ๊ฒฝ์ด ๊ฐ๋ฅํ๋ค๋ ๋ง์
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class ExampleCase{
private ChocolateService chocolateService;
private DrinkService drinkService;
@Autowired
public void setChocolateService(ChocolateService chocolateService){
this.chocolateService = chocolateService;
}
@Autowired
public void setDrinkService(DrinkService drinkService){
this.drinkService = drinkService;
}
}
3.ํ๋ ์ฃผ์
- ์ฝ๋๊ฐ ๊ฐ๊ฒฐํ์ง๋ง, ์ธ๋ถ์์ ๋ณ๊ฒฝํ๊ธฐ ํ๋ค๋ค. ํ๋ ์์ํฌ์ ์์กด์ ์ด๊ณ ๊ฐ์ฒด์งํฅ์ ์ผ๋ก ์ข์ง ์๋ค.
1
2
3
4
5
6
7
8
9
10
@Component
public class OrderServiceImpl implements OrderService {
@Autowired
private final MemberRepository memberRepository;
@Autowired
private final DiscountPolicy discountPolicy;
}
4.method ์ฃผ์
1
2
3
4
5
6
7
8
9
10
11
@Component
public class OrderServiceImpl implements OrderService {
private MemberRepository memberRepository;
private DiscountPolicy discountPolicy;
@Autowired
public void init(MemberRepository memberRepository, DiscountPolicy discountPolicy){
this.memberRepository = memberRepository;
this.discountPolicy = discountPolicy;
}
๐IOC(Inversion of Control)
์ ์ด์ ์ญ์
๋ณดํต ์ผ๋ฐ์ ์ผ๋ก ๊ฐ๋ฐ์๊ฐ ์ํ๋๋๋ก ์ง์ ํธ์ถํ๊ณ ์ ์ดํ๋ ์คํ์ผ๋ก ๊ฐ๋ฐ์ ํ๋๋ฐ ์ ์ด์ ์ญ์ ์ ๊ฐ๋ ์ ํ๋ ์์ํฌ๊ฐ์๊ฒ ๋ด์ฝ๋๋ฅผ ๋์ ํธ์ถํด์ฃผ๋๊ฒ์
ex) ์คํ๋งํ๋ ์์ํฌ ์์ ๋น์ ๋ฑ๋กํ๋ฉด IOC ์ปจํ ์ด๋์ ์ ์ฅํ๊ณ ๋์ ๊ด๋ฆฌํด์ฃผ๋ ๊ฒ ๊ฐ๋ฐ์๊ฐ ํ๋๊ฒ ์๋ ํ๋ ์์ํฌ๊ฐ ๋์ ํด์ฃผ๋ฏ๋ก ์ ์ด์ ์ญ์ ์ด ์ผ์ด๋ฌ๋ค๊ณ ๋ณผ์์์.
์คํ๋ง์ ์ฌ์ฉํ์ง์์ ์ผ๋ฐ ์๋ฐ์ฝ๋์์์ ๊ฐ์ฒด ์์ฑ๊ณผ ์์กด์ฑ ์ฃผ์ ์์
1
2
3
4
5
6
public class OrderService {
private OrderServiceImPl orderServiceImpl;
public OrderService() {
this.orderServiceImpl = new OrderServiceImPl();
}
}
-์ ์ฝ๋์์ OrderService ํด๋์ค๋ OrderServiceImPl ํด๋์ค์ ์์กดํ๊ณ ์๋ค. ๊ทธ๋ฆฌ๊ณ OrderService์ ์์ฑ์์์ ์ง์ OrderServiceImPl ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ณ ์๋ค. ์ด๋ฌํ ๊ฒฝ์ฐ, OrderService๋ OrderServiceImPl ๊ฐํ๊ฒ ๊ฒฐํฉ๋์ด ์์ด ์ ์ฐ์ฑ์ด ๋จ์ด์ง๊ฒ๋๋ค.
์คํ๋ง์ ์ฌ์ฉํ์ฌ ์ ์ด์ ์ญ์ ์ด ์ผ์ด๋ ์์
1
2
3
4
5
6
7
public class OrderService {
private final OrderDAO orderDAO;
public OrderService(OrderDAO orderDAO) {
this.orderDAO = orderDAO;
}
}
- ์ ์ฝ๋์์๋ OrderServiceํด๋์ค๊ฐ OrderDAO๋ฅผ ์ฃผ์ ๋ฐ์ ์ฌ์ฉํ๊ณ ์๊ธฐ ๋๋ฌธ์ ์์กด์ฑ์ด ์ฝํ๋ฉฐ ๊ฒฐํฉ๋๊ฐ ๋ฎ์ ์ ์ฐํด์ง๋ค.
๐Bean
-Spring IoC ์ปจํ ์ด๋๊ฐ ๊ด๋ฆฌํ๋ ์๋ฐ ๊ฐ์ฒด
์คํ๋ง Bean์ ์์ฌ์ฉํ๋๊ฐโ
- ๊ฐ์ฅ ํฐ ์ด์ ๋ ์คํ๋ง ๊ฐ ๊ฐ์ฒด๊ฐ ์์กด๊ด๊ณ๋ฅผ ๊ด๋ฆฌํ๋๋ก ํ๋ ๊ฒ์ ๊ฐ์ฅ ํฐ ๋ชฉ์ ์ด ์๋ค. ๊ฐ์ฒด๊ฐ ์์กด๊ด๊ณ๋ฅผ ๋ฑ๋กํ ๋ ์คํ๋ง ์ปจํ ์ด๋์์ ํด๋นํ๋ ๋น์ ์ฐพ๊ณ , ๊ทธ ๋น๊ณผ ์์กด์ฑ์ ๋ง๋ ๋ค.
Bean์ ๋ฑ๋กํ๋ ๋ฐฉ๋ฒ
- xml์ ์ง์ ๋ฑ๋กํ๊ธฐ
- @Bean ์ด๋ ธํ ์ด์ ์ด์ฉํ๊ธฐ
- @Component, @Controller, @Service, @Repository ์ด๋ ธํ ์ด์ ์ ์ด์ฉํ๊ธฐ
1.xml์ ์ง์ ๋ฑ๋กํ๊ธฐ
1
2
<bean id="helloService" class="com.example.myapp.di.HelloService"/>
<bean id="helloController" class="com.example.myapp.di.HelloController" p:helloService-ref="helloService"</bean>
2.@Bean ์ด๋ ธํ ์ด์ ์ฌ์ฉํ๊ธฐ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@Configurable
@ComponentScan(basePackages= {"com.example.myapp"})
@ImportResource(value= {"classpath:application-config.xml"})
public class AppConfig {
@Bean
public IHelloService helloService() {
return new HelloService();
}
@Bean
public HelloController helloController() {
HelloController controller = new HelloController();
controller.setHelloService(helloService());
return controller;
}
}
3. @Component, @Controller, @Service, @Repository ์ด๋ ธํ ์ด์ ์ ์ด์ฉ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Controller
public class EmpController {
private IEmpService empService;
@Autowired
public EmpController(IEmpService empService) {
this.empService = empService;
}
void printInfo() {
int count = empService.getEmpCount(50);
System.out.println("์ฌ์์ ์ : " + count);
}
}