1. Inversion of Control(์ ์ด์ ์ญ์ )
IoC๋ ์ ์ด์ ํ๋ฆ์ด ์ญ์ ๋๋ ๊ฒ์ ๋งํ๋ค.
ํด๋น ์ํฉ์์๋ ๊ฐ์ฒด๊ฐ ์์ ์ด ์ฌ์ฉํ ๊ฐ์ฒด๋ฅผ ์ค์ค๋ก ์ ํ, ์์ฑํ์ง ์๋๋ค.
๐ก ์คํ๋ง ํ๋ ์์ํฌ์์๋ ์ ์ด์ ๊ถํ์ด ๊ฐ์ฒด ์์ ์๊ฒ ์๊ณ ํ๋ ์์ํฌ์ ์๋ค.=> ํ๋ ์์ํฌ๋ ์ ์ฒด ํ๋ฆ์ ์ ์ด ๊ถํ์ ๊ฐ์ง๊ณ ์๋ค.
=> ํ๋ ์์ํฌ๊ฐ ํ๋ฆ์ ์ฃผ๋ํ๋ฉด์ ๊ฐ๋ฐ์๊ฐ ๋ง๋ ์ ํ๋ฆฌ์ผ์ด์ ์ฝ๋๋ฅผ ์ฌ์ฉํ๋ค.
SpringBoot Part1-1์์ ์์ฑํ ์ฝ๋๋ฅผ ์ดํด๋ณด์.
public class Main {
public static void main(String[] args) {
FixedAmountVoucher fixedAmountVoucher = new FixedAmountVoucher(10L);
Order order = new Order(fixedAmountVoucher);
}
}
Order ์ํฐํฐ๊ฐ ์ฌ์ฉํ ํด๋์ค๋ฅผ ๊ฒฐ์ ํ๊ณ ํด๋น ํด๋์ค์ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค.
=> ๋ชจ๋ ์์ ์ ์ฌ์ฉํ๋ ์ชฝ(Order)์์ ์ ์ดํ๋ค.
๊ธฐ์กด ๊ตฌ์กฐ ๋ณ๊ฒฝ
OrderContext: ์ฃผ์ ๊ฐ์ฒด์ ์์ฑ๊ณผ ๊ด๊ณ ์ค์ ๋ด๋น
OrderService: Order์ ๋น์ฆ๋์ค ๋ก์ง ๋ด๋น(OrderRepository, VoucherService๋ฅผ ์ง์ ์์ฑํ์ง ์๋๋ค.)
๊ฐ์ฒด๋ฅผ ๋ง๋๋ ์ ์ด๊ถ์ OrderContext์ ๋๊ฒผ๋ค!! => IoC
* OrderContext๋ ์ผ์ข ์ IoC ์ปจํ ์ด๋
IoC์ปจํ ์ด๋: IoC๋ฅผ ๊ฐ๋ฅํ๊ฒ ํ๋ ๊ณต๊ฐ, ๊ฐ๋ณ ๊ฐ์ฒด๋ค์ ์์กด ๊ด๊ณ ์ค์ ์ด ์ด๋ค์ง๊ณ ๊ฐ์ฒด์ ์์ฑ๊ณผ ํ๊ดด๋ฅผ ๊ด์ฅํ๋ค.
๐คRepository๊ฐ interface๋ก ๊ตฌํ๋ ์ด์
Repository๋ jdbc๋ฅผ ์ด์ฉํ์ฌ ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ๊ฐ์ง๊ณ ์ฌ ์๋ ์๊ณ , ํด๋น ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ข ๋ฅ๊ฐ ๋ค์ํ๊ธฐ ๋๋ฌธ์(ex. PostgreSQL, mongoDB) ๊ด๋ จ ์ฝ๋๊ฐ ๋ณ๊ฒฝ๋ ์ ์๋ ์ ์ ๊ณ ๋ คํด ๋๋ถ๋ถ interface๋ก ๊ตฌํํ๋ค.
2. DDD
Aggregate
- Entity๋ค์ ์งํฉ, ์ฐ๊ด๋ ๋๋ฉ์ธ ๊ฐ์ฒด๋ค์ ์งํฉ
- ํ๋์ Root Entity๋ฅผ ๊ฐ์ง๋ฉฐ, ์ด Root Entity๋ฅผ ํตํด ๋ค๋ฅธ Entity๋ค์ ์ ๊ทผํ๋ค.
- ์ผ๊ด์ฑ ๊ฒฝ๊ณ๋ฅผ ๊ฐ์ง๋ฉฐ, ํธ๋์ญ์ ์ ๋จ์๋ก ๋ค๋ฃจ์ด์ง๋ค.
Repository
- ๋๋ฉ์ธ ๊ฐ์ฒด๋ค์ ์์ํํ๊ณ , ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ ์ธ๋ถ ์ ์ฅ์์์ ์ํธ ์์ฉ์ ์ถ์ํํ๋ค.
- ๋๋ฉ์ธ ๊ฐ์ฒด๋ฅผ ์ ์ฅ, ๊ฒ์, ์์ , ์ญ์ ํ๋ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ค.
- Aggreagate๋จ์๋ก ๋์ํ๋ค.
- ์ธํฐํ์ด์ค๋ก ์ฃผ๋ก ๊ตฌ์ฑ๋๋ฉฐ, ๊ตฌํ์ ๋ํ ์ธ๋ถ์ฌํญ์ ์จ๊ธฐ๊ณ ๋๋ฉ์ธ์ ๋ฐ์ดํฐ ์ ๊ทผ์ ์ถ์ํํ์ฌ ์ ์ฐ์ฑ๊ณผ ์ ์ง ๋ณด์์ฑ์ ๋์ธ๋ค.
Service
- Domain Model Layer์ ์กด์ฌ
- Repository๋ฅผ ์ด์ฉํด์ ์ค์ Entity์ ๋ํ ํธ๋์ ์ ์ ์ผ์ผํจ๋ค.
- ํน์ ํ ๋๋ฉ์ธ ์์ ์ ์ํํ๊ณ , ์ฌ๋ฌ ๊ฐ์ฒด ๊ฐ์ ํ๋ ฅ์ ์กฐ์ ํ๋ฉฐ, ํธ๋์ญ์ ์ฒ๋ฆฌ ๋ฑ์ ๋ด๋นํ๋ค.
- ๋๋ฉ์ธ ๊ฐ์ฒด์ ๋น์ฆ๋์ค ๋ก์ง์ ์ถ๊ฐํ๊ธฐ ์ด๋ ต๊ฑฐ๋, ์ฌ๋ฌ ๋๋ฉ์ธ ๊ฐ์ฒด ๊ฐ์ ์ํธ ์์ฉ์ด ํ์ํ ๊ฒฝ์ฐ ์ฌ์ฉํ๋ค.
- ๋๋ฉ์ธ ๊ฐ์ฒด์ ๋ณต์ก์ฑ์ ๊ด๋ฆฌํ๊ณ ๋๋ฉ์ธ์ ์ผ๊ด์ฑ์ ์ ์งํ๋๋ฐ ๋์์ ์ค๋ค.
3. ApplicationContext
ApplicationContext, BeanFactory
ApplicationContext๋ BeanFactory๋ฅผ ์์ํ๊ณ ๊ฐ์ฒด์ ๋ํ ์์ฑ, ์กฐํฉ, ์์กด๊ด๊ณ ์ค์ ๋ฑ์ ์ ์ดํ๋ IoC ๊ธฐ๋ณธ๊ธฐ๋ฅ์ BeanFactory๊ฐ ๋ด๋นํ๋ค.
ApplicationContext๊ฐ BeanFactory๋ฅผ ์์ํ๊ณ ๊ทธ ์ธ์ ๋ค๋ฅธ ๊ฒ๋ค๋ ์์ํ๊ธฐ ๋๋ฌธ์ BeanFactory์ ๊ธฐ๋ฅ๋ฟ๋ง ์๋๋ผ ๋ ๋ง์ ๊ธฐ๋ฅ์ ์ฌ์ฉํ ์ ์๋ค.
IoC Container
- ๊ฐ๋ณ ๊ฐ์ฒด๋ค์ ์์กด๊ด๊ณ ์ค์ ์ด ์๋์ผ๋ก ์ด๋ฃจ์ด์ง๊ณ ๊ฐ์ฒด๋ค์ ์์ฑ๊ณผ ํ๊ดด, ์กฐํฉ๋ฑ์ ๊ด์ฅํ๋ค.
- ์๋ช ์ฃผ๊ธฐ๋ฅผ ๋ค๋ฃจ๋ฉด์ ์ธ์คํด์ค๋ฅผ ์์ฑํ๊ณ ๊ด๋ฆฌํ๋ค.
IoC ์ปจํ ์ด๋๋ฅผ ์คํ๋ง์์๋ ApplicationContext ์ธํฐํ์ด์ค๋ก ์ ๊ณตํ๋ค!
Bean
- IoC Container๊ฐ ๊ด๋ฆฌํ๋ ๊ฐ์ฒด
- @Bean ์ด๋ ธํ ์ด์ ์ ํตํ์ฌ IoC ์ปจํ ์ด๋์ ์ํด ์์ฑ, ๊ด๋ฆฌ๋๋ค.
Configuration Metadata (์ค์ ๋ฉํ๋ฐ์ดํฐ)
Spring Framework์์ ์ ํ๋ฆฌ์ผ์ด์ ์ ๊ตฌ์ฑ์ ์ ์ํ๊ณ ์ค์ ํ๋ ๋ฐ ์ฌ์ฉ๋๋ ์ ๋ณด
=> ์ ํ๋ฆฌ์ผ์ด์ ์์ ๊ฐ์ฒด๋ค์ ๋์ํํ ๊ฒ
- Spring ์ปจํ ์ด๋๊ฐ ์ ํ๋ฆฌ์ผ์ด์ ์ ๊ฐ์ฒด ์์ฑ, ์์กด์ฑ ์ฃผ์ , ๋ผ์ดํ์ฌ์ดํด ๊ด๋ฆฌ ๋ฑ์ ์ํํ๋ ๋ฐ ํ์ํ ์ ๋ณด๋ฅผ ์ ๊ณตํ๋ค.
- ApplicationContext๋ ์ค์ ๋ง๋ค์ด์ผํ ์ ๋ณด๋ฅผ Configuration Metadata๋ก๋ถํฐ ๋ฐ์์จ๋ค.
- ํด๋น ๋ฐ์ดํฐ๋ฅผ ์ด์ฉํด์ IoC ์ปจํ ์ด๋์ ์ํด ๊ด๋ฆฌ๋๋ ๊ฐ์ฒด๋ค์ ์์ฑํ๊ณ ๊ตฌ์ฑํ๋ค.
๐ข ์ค์ ๋ฐฉ์
๐ ์ด๋ ธํ ์ด์ ๊ธฐ๋ฐ
@Component(+@Service, @Respository, @Controller)์ ํตํด์ ๊ฐ์ฒด๋ฅผ ๋ฑ๋กํ๊ณ @Autowired๋ฅผ ์ฌ์ฉํ์ฌ ์์กด์ฑ์ ์ฃผ์ ํ๋ค.
๐ ์๋ฐ ๊ธฐ๋ฐ
Java ํด๋์ค๋ฅผ ์ฌ์ฉํ์ฌ ๊ตฌ์ฑ ๋ฉํ๋ฐ์ดํฐ๋ฅผ ์ ์ํ๋ ๋ฐฉ์
ํน์ง
- @Configuration์ ์ฌ์ฉํ ํด๋์ค๋ฅผ ์์ฑํ๊ณ , @Bean์ ์ฌ์ฉํ์ฌ ๊ฐ์ฒด์ ์์ฑ๊ณผ ๊ตฌ์ฑ์ ์ ์ํ๋ค.
- AnnotationConfigApplicationContext ํด๋์ค๋ Java ํ์ผ ๊ธฐ๋ฐ ๊ตฌ์ฑ ๋ฉํ๋ฐ์ดํฐ๋ฅผ ๋ก๋ํ์ฌ Spring ์ปจํ
์ด๋๋ฅผ ์์ฑํ๋ ๋ฐ ์ฌ์ฉ๋๋ค.
- AnnotationConfigApplicationContext๋ @Configuration ์ด๋
ธํ
์ด์
์ด ์ง์ ๋ ํด๋์ค๋ฅผ ์ฐพ์ ๊ตฌ์ฑ ๋ฉํ๋ฐ์ดํฐ๋ก ์ฌ์ฉํ๋ค.
ex) AppConfig๋ @Configuration ์ด๋
ธํ
์ด์
์ด ์ง์ ๋ Java ์ค์ ํด๋์ค
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
OrderService orderService = context.getBean(OrderService.class);
๐ XML ๊ธฐ๋ฐ
applicationContext.xml๊ณผ ๊ฐ์ XML ํ์ผ์ ๊ฐ์ฒด์ ์ ์์ ๊ตฌ์ฑ์ ์ ์ํ๋ ๋ฐฉ์
ํน์ง
- XML ํ์ผ์ applicationContext.xml์ผ๋ก ์์ฑ๋๋ฉฐ, <bean> ์์๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ์ฒด์ ์์ฑ, ์์กด์ฑ ์ฃผ์ , ์์ฑ ์ค์ ๋ฑ์ ์ ์ํ๋ค.
- GenericXmlApplicationContext ํด๋์ค๋ XML ๊ธฐ๋ฐ ๊ตฌ์ฑ ๋ฉํ๋ฐ์ดํฐ๋ฅผ ๋ก๋ํ์ฌ Spring ์ปจํ
์ด๋๋ฅผ ์์ฑํ๋ ๋ฐ ์ฌ์ฉ๋๋ค.
- GenericXmlApplicationContext๋ Spring ์ปจํ
์ด๋๋ฅผ ๊ตฌ์ฑํ๊ณ ์ด๊ธฐํํ๋ ๋ฐ ํ์ํ XML ํ์ผ์ ๊ฒฝ๋ก๋ฅผ ์ธ์๋ก ๋ฐ๋๋ค.
ex)
GenericXmlApplicationContext context = new GenericXmlApplicationContext("applicationContext.xml");
4. Dependency Injection
- IoC๋ฅผ ๊ตฌํํ๋ ํ๋์ ํจํด
- ๊ฐ์ฒด ๊ฐ์ ์์กด ๊ด๊ณ๋ฅผ ์ธ๋ถ์์ ์ค์ ํด์ฃผ๋ ๋์์ธ ํจํด
์ผ๋ฐ์ ์ผ๋ก ๊ฐ์ฒด๋ ํ์๋ก ํ๋ ๋ค๋ฅธ ๊ฐ์ฒด์ ๋ํ ์์กด์ฑ์ ์ง์ ์์ฑํ๊ฑฐ๋ ์ฐธ์กฐํ๋ค.
ํ์ง๋ง DI ํจํด์ ์ฌ์ฉํ๋ฉด ๊ฐ์ฒด๊ฐ ์ง์ ์์กดํ๋ ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ฑฐ๋ ๊ด๋ฆฌํ์ง ์๊ณ , ์ธ๋ถ์์ ์์กด์ฑ์ ์ฃผ์ ๋ฐ์ ์ฌ์ฉํ ์ ์๋ค.
DI ์ข ๋ฅ
๐ ์์ฑ์ ์ฃผ์ (Constructor Injection)
๊ฐ์ฒด์ ์์ฑ์๋ฅผ ํตํด ์์กด์ฑ์ ์ฃผ์ ํ๋ ๋ฐฉ์
๊ฐ์ฒด์ ์์ฑ ์์ ์ ํ์ํ ์์กด์ฑ์ ์ธ๋ถ์์ ์ ๋ฌ๋ฐ์ ์ฌ์ฉํ๋ค.
๐ setter ์ฃผ์
(Setter Injection)
๊ฐ์ฒด์ setter ๋ฉ์๋๋ฅผ ํตํด ์์กด์ฑ์ ์ฃผ์ ํ๋ ๋ฐฉ์
์์กด์ฑ์ ์ค์ ํ๊ธฐ ์ํด setter ๋ฉ์๋๋ฅผ ํธ์ถํ์ฌ ์ธ๋ถ์์ ์์กด์ฑ์ ์ฃผ์
ํ๋ค.
๐ ํ๋ ์ฃผ์
(Field Injection)
์์กด์ฑ์ ํด๋์ค์ ๋ฉค๋ฒ ํ๋๋ก ์ง์ ์ฃผ์ ํ๋ ๋ฐฉ์
์ฃผ๋ก @Autowired ์ด๋
ธํ
์ด์
์ ์ฌ์ฉํ์ฌ ํ๋์ ์๋์ผ๋ก ์์กด์ฑ์ ์ฃผ์
ํ๋ค.
@Bean
public VoucherService voucherService(VoucherRepository voucherRepository) {
return new VoucherService(voucherRepository);
}
@Bean
public OrderService orderService(VoucherService voucherService, OrderRepository orderRepository) {
return new OrderService(voucherService, orderRepository);
}
'๐๏ธ ํ๋ก๊ทธ๋๋จธ์ค ๋ฐ๋ธ์ฝ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
SpringBoot Part1-5 (0) | 2023.06.26 |
---|---|
SpringBoot Part1-4 (0) | 2023.06.23 |
SpringBoot Part1-3 (0) | 2023.06.22 |
SpringBoot Part 1-1 (0) | 2023.06.21 |
ํ๋ ์์ํฌ๋ฅผ ์ํ JAVA - OOP (0) | 2023.06.11 |
๋๊ธ