SpringBoot Part1 (2)
IoC (์ ์ด์ ์ญ์ )
Inversion of Control
: ์์ ์ด ์ฌ์ฉํ ๊ฐ์ฒด๋ฅผ ์ค์ค๋ก ์ ํํ์ง๋, ์์ฑํ์ง๋ ์๋๋ค.
: ์๋ธ๋ฆฟ, ์คํ๋ง๊ณผ ๊ฐ์ ํ๋ ์์ํฌ์์๋ ์ด๋ฌํ ์ ์ด ๊ถํ์ด ๊ฐ์ฒด ์์ ์๊ฒ ์๊ณ ํ๋ ์์ํฌ์ ์๊ฒ ๋๋ค.
ํ๋ ์์ํฌ๋ ๋จ์ง ๋ฏธ๋ฆฌ ๋ง๋ค์ด๋ ๋ฐ ์ ํ์ด๋ ํ์ฅํด์ ์ฌ์ฉํ ์ ์๊ฒ ์ค๋น๋ ๋ฐ์ ํ์ด ์๋ ์ ์ฒด ํ๋ฆ์ ์ ์ด์ ๊ถํ์ ๊ฐ์ง๊ณ ์๊ฒ ๋๋ค.
๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ๋ ์ดํ๋ฆฌ์ผ์ด์ ์ฝ๋๋ ์ ํ๋ฆฌ์ผ์ด์ ํ๋ฆ์ ์ง์ ์ ์ดํ์ง๋ง ํ๋ ์์ํฌ๋ ๊ฑฐ๊พธ๋ก ์ ํ๋ฆฌ์ผ์ด์ ์ฝ๋๊ฐ ํ๋ ์์ํฌ์ ์ํด ์ฌ์ฉ๋๋ค.
์ฆ, ์ ํ๋ฆฌ์ผ์ด์ ์ฝ๋๊ฐ ํ๋ ์์ํฌ๊ฐ ์ง๋์ ํ์์ ์๋์ ์ผ๋ก ๋์ํ๋ ๊ฒ์ด๋ค.
๋ด๊ฐ ํธ์ถํ์ง์๊ณ ํ๋ ์์ํฌ๊ฐ ํธ์ถํ ๋๊น์ง ๊ธฐ๋ค๋ฆฐ๋ค.
- OrderContext : ์ฃผ๋ฌธ์ ๋ํ ์ ๋ฐ์ ์ด ๋๋ฉ์ธ ๊ฐ์ฒด์ ๋ํ ์์ฑ๊ณผ ๊ด๊ณ์ค์ ์ ์ฑ ์ ์ง๋ค.
- OrderService : ๋น์ง๋์ค ๋ก์ง์ ๋ด์ ํด๋์ค(order ์์ฑ). ์์ ์ด ์ด๋ค OrderRepository๋ฅผ ์ฌ์ฉํญ์ง VocherService๋ฅผ ์ฌ์ฉํ ์ง ์ง์ ์ ํ, ์์ฑํ์ง์๊ณ OrderContext์๊ฒ ์ ์ด๊ถ์ ๋๊ธด๋ค
⇒ IoC๊ฐ ์ผ์ด๋ ์ํฉ. IoC๊ฐ ์ผ์ด๋๋ ์ฅ์(OrderContext)๋ฅผ IoC ์ปจํ ์ด๋๋ผ๊ณ ๋ถ๋ฅด๊ณ ๊ฐ์ฒด๋ค์ ๋ํ ์์ฑ๊ณผ ํ๊ดด๋ฅผ ๋ชจ๋ ๊ด์ฅํ๋ค. ( ๊ฐ์ฒด์ ๋ํ ์์กด๊ด๊ณ๋ฅผ ๋งบ์ด์ค.)
repository๊ฐ interface์ธ ์ด์ ?
reference. ํ ๋น์ ์คํ๋ง 3.1.1๋ถ 1์ฅ ์ค๋ธ์ ํธ์ ์์กด๊ด๊ณ
DDD (๋๋ฉ์ธ ์ฃผ๋ ์ค๊ณ)
- Repository patten
Aggregate
: ๊ฐ์ฅ ์์ ๋๋ฉ์ธ ๋ชจ๋ธ์ ๋ชจ๋ ๋จ์.
: ๋๋ฉ์ธ์ ์ค์ฒด ๊ฐ๋ ์ ํํํ๋ ๊ฐ์ฒด(Entity)๋ค์ ์งํฉ
: ๊ฐ๊ฐ์ Aggregate์ Root๊ฐ ์กด์ฌ. Aggregate Root๋ ๊ณง ํ๋์ Entity
: Aggregate ๋จ์๋ก ํ๋์ ํธ๋์ญ์ ๋ณด์ฅ๋์ด์ผํ๋ค.
Service
: Aggregate์ ๋ฌ๋ฆฌ ์ํ(Data)๊ฐ ์๋ค.
: ๋น์ง๋์ค์์ ์ค์ํ ํ๋ก์ธ์ค, ํน์ ๋น์ง๋์ค ๋ฃฐ์ ๊ฐ๊ณ ์๋ ํน์ ํ *ํ์(Behavior)*๋ฅผ ๋ด๊ณ ์๋ค. (๋ฉ์๋๋ง ๊ฐ์ง)
: ๊ฐ๊ฐ์ Aggregate๋ฅผ ๊ฐ์ง๊ณ ํธ๋์ญ์ ์ ์ผ์ผํค๊ณ ๊ทธ๊ฒ์ ๋ํ service์์ ๋ณด์ฅ์ ํ๋ค.
: Domain Model Layer์ ์กด์ฌ
Repository
: Entity๋ฅผ ์ ์ฅํ๋ ์ ์ฅ์
: order์ ๋ํ ์ํ(Data)๋ฅผ ๋ณต๊ตฌํ๊ณ ์ ์ฅํ๋ ์ญํ ์ ํ๋ค.
ApplicationContext
์ผ์ข ์ IoC ์ปจํ ์ด๋
์ด๋ ํ class๋ค์ ์ฌ์ฉํ ๊ฒ์ด๋ค๋ผ๊ณ registerํ๋ฉด ํ์์ ์ํด ์์กด๊ด๊ณ๋ฅผ ๋งบ์ด์ฃผ๊ณ ์๋ช ์ฃผ๊ธฐ๋ฅผ Managing ํ๋ฉฐ ๊ฐ์ฒด๋ค์ ์ธ์คํด์ค๋ฅผ ๋ง๋ค์ด์ค๋ค.
Spring์์ ์ ๊ณตํ๋ ApplicationContext๋ผ๋ ์ธํฐํ์ด์ค๋ฅผ ํตํด IoC Container ์ ๊ณต
BeanFactory๋ฅผ ์์๋ฐ๋๋ค. → BeanFactory : ๊ฐ์ฒด์ ๋ํ ์์ฑ๊ณผ ์กฐํฉ, ์์กด ๊ด๊ณ ์ค์ ์ ์ ์ดํ๋ IoC ๊ธฐ๋ณธ๊ธฐ๋ฅ ์ ๊ณต
๐ Bean
→ ์คํ๋ง์์ ์ ๊ณตํ๋ ApplicationContext, BeanFactory, IoC ์ปจํ ์ด๋์ ์ํด ๊ด๋ฆฌ๋์ด์ง๋ ๊ฐ์ฒด
→ JVM์ ์ฌ๋ผ๊ฐ๋ ๊ฐ์ฒด๋ค ์ค IoC์์ ๊ด๋ฆฌ๋๋ ๊ฐ์ฒด์ ์๋ ๊ฐ์ฒด๋ฅผ ๋ถ๋ฆฌํ๊ธฐ ์ํด์ Bean์ด๋ผ๋ ์ฉ์ด ์ ๊ณตํ๋ค.
→ Bean ์ ์ํ๋ ๋ฐฉ๋ฒ @Bean
BeanFactory์ ApplicationContext์ ์ฐจ์ด
→ https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-beanfactory
Configuration Metadata
์คํ๋ง์ ApplicationContext๋ ์ค์ ๋ง๋ค์ด์ผํ ๋น ์ ๋ณด๋ฅผ Configuration Metadata(์ค์ ๋ฉํ๋ฐ์ดํฐ)๋ก ๋ถํฐ ๋ฐ์์จ๋ค.
์ด ๋ฉํ๋ฐ์ดํฐ๋ฅผ ์ด์ฉํด IoC ์ปจํ ์ด๋์ ์ํด ๊ด๋ฆฌ๋๋ ๊ฐ์ฒด๋ค์ ์์ฑํ๊ณ ๊ตฌ์ฑํ๋ค. ๊ฐ์ฒด๋ค์ ๋๋ฉด์ด๋ผ๊ณ ๋ณผ ์ ์๋ค.
XML ์ด๋ Java ํ์ผ ๊ธฐ๋ฐ์ผ๋ก ์์ฑ๊ฐ๋ฅ.
XML ์ฌ์ฉํ ๊ฒฝ์ฐ → GenericXmlApplicationContext ๊ตฌํ์ฒด
Java ์ฌ์ฉํ ๊ฒฝ์ฐ → AnnotationConfigApplicationContext ๊ตฌํ์ฒด๋ฅผ ํตํด ApplicationContext ๊ตฌํ
๋ณ๊ฒฝ ์
public static void main(String[] args) {
var customId = UUID.randomUUID();
var orderContext = new OrderContext();
var orderService = orderContext.orderService();
var order = orderService.createOrder(customId,new ArrayList<OrderItem>() {{
add(new OrderItem(UUID.randomUUID(), 100L, 1));
}});
Assert.isTrue(order.totalAmount() == 100L, MessageFormat.format("totalAmount {0} is not 90L",order.totalAmount()));
}
๋ณ๊ฒฝ ํ (OrderContext๋ฅผ AppConfiguration(@Configuration)๋ก ๋ณ๊ฒฝ )
public static void main(String[] args) {
var applicationContext = new AnnotationConfigApplicationContext(AppConfiguration.class);
var customId = UUID.randomUUID();
var orderService = applicationContext.getBean(OrderService.class);
var order = orderService.createOrder(customId,new ArrayList<OrderItem>() {{
add(new OrderItem(UUID.randomUUID(), 100L, 1));
}});
Assert.isTrue(order.totalAmount() == 100L, MessageFormat.format("totalAmount {0} is not 90L",order.totalAmount()));
}
Dependecy Injection
์์กด๊ด๊ณ ์ฃผ์ ํจํด
Order๊ฐ ์ด๋ค Voucher ๊ฐ์ฒด๋ฅผ ์์ฑํ ์ง, ์ด๋ค OrderRepository ๊ฐ์ฒด๋ฅผ ์์ฑํ ์ง ์ค์ค๋ก ๊ฒฐ์ ํ์ง ์๊ณ ์์ฑ์๋ฅผ ํตํด์ ๊ฐ์ฒด๋ฅผ ์ฃผ์ ๋ฐ๋ ํจํด ⇒ ์์ฑ์ ์ฃผ์ ํจํด
Spring์ ์์ฑ์ ์ฃผ์ ํจํด ์ธ์ ์ธํฐ ๊ธฐ๋ฐ ์์กด๊ด๊ณ ์ฃผ์ ๋ ์ง์ํ๋ค.
@Configuration
public class AppConfiguration {
@Bean
public VoucherRepository voucherRepository(){
return new VoucherRepository() {
@Override
public Optional<Voucher> findById(UUID voucherId) {
return Optional.empty();
}
};
}
@Bean
public OrderRepository orderRepository(){
return new OrderRepository() {
@Override
public void insert(Order order) {
}
};
}
// ์์กด๊ด๊ณ์ ์๋ ํด๋น ํด๋์ค์ ๋ฉ์๋๋ฅผ ๋งค๊ฐ๋ณ์๋ก ์์ฑํด์ฃผ๋ฉด spring์ด ๋น์ ํ ๊ฐ์ฒด๋ฅผ ๋ง๋ค๋ ์์์ ์ ๋ฌํด์ค
@Bean
public VoucherService voucherService(VoucherRepository voucherRepository){
return new VoucherService(voucherRepository);
}
@Bean
public OrderService orderService(VoucherService voucherService,OrderRepository orderRepository){
return new OrderService(voucherService,orderRepository);
}
}
Daliy Mission
๋ฐ์ฐ์ฒ ๊ด๋ฆฌ Command-line Application ์ถ๊ฐ
- CommandLineApplication ํด๋์ค ์์ฑ
- AnnotationConfigApplicationContext๋ฅผ ์ด์ฉํด์ IoC ์ปจํ ์ด๋๋ฅผ ์์ฑํ๊ณ ์๋น์ค, ๋ ํฌ์งํ ๋ฆฌ๋ฅผ ๋น์ผ๋ก ๋ฑ๋ก
- create ์ปค๋งจ๋ : ๋ฐ์ฐ์ฒ ์์ฑ / list ์ปค๋งจ๋ : ์ปค๋ฉ๋๋ฅผ ํตํด ๋ง๋ค์ด์ง ๋ฐ์ฐ์ฒ๋ฅผ ์กฐํ
์ถ์ฒ - ํด๋ฆฌ : SpringBoot Part1
'Back-end ๋ฐ๋ธ์ฝ์ค > week 03 - 05 TIL (Spring)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[TIL] 221107 - SpringBoot Part2 : Spring Test ์์ํ๊ธฐ (0) | 2022.11.08 |
---|---|
[TIL] 221104 - SpringBoot Part1 : logging, SpringBoot (2) | 2022.11.06 |
[TIL] 221103 - SpringBoot Part1 - ์ ํ๋ฆฌ์ผ์ด์ ์์ฑ ๊ด๋ฆฌ (0) | 2022.11.06 |
[TIL] 221102 - SpringBoot Part1 : Dependecy injection, ์ปดํฌ๋ํธ ์ค์บ (0) | 2022.11.03 |
[TIL] 221031 - SpringBoot Part1 (0) | 2022.11.02 |
๋๊ธ