SpringBoot Part1 (3)
Dependecy injection, Circular Dependency
IoC๋ฅผ ๊ตฌํํ๋ ๋ค์ํ ํจํด๋ค(์ ๋ต ํจํด, ์๋น์ค ๋ก์ผ์ดํฐ ํจํด, ํฉํ ๋ฆฌ ํจํด, ์์กด๊ด๊ณ ์ฃผ์ ํจํด) ์ค ํ๋์ด๋ค.
๊ฐ์ฒด๋ฅผ ์ฃผ์ ๋ฐ๋ ์ฑํด์ ์์ฑ์ ์ฃผ์ ํจํด์ด๋ผ๊ณ ๋ถ๋ฅด๋ฉฐ, ๊ทธ ์ธ์๋ ์คํ๋ง์ ์ธํฐ ๊ธฐ๋ฐ์ ์์กด ๊ด๊ณ๋ ์ง์ํฉ๋๋ค.
Dependency Resoulation Precess
์์กด๊ด๊ณ๊ฐ ํ์ฑ๋๋ค.
- ApplicationContext๋ฅผ ๋ง๋ค์ด์ง๋ Configuration Metadata๋ฅผ ์ ๋ฌํ๊ณ ๊ทธ ์์ Bean๋ค์ ๋ํ ์ ๋ณด๊ฐ ์๋ค. ( new AnnotationConfigApplicationContext(AppConfiguration.class) )
- ์ด๋ ๊ฐ Bean์ ๋ํ ์์กด๊ด๊ณ๊ฐ class ์์ฑ์ด๋ ์์ฑ์, ์ธ์๋ค์ ํตํด ํ์ฑ๋๋ค.
Circular Dependecy
: ์์กด๊ด๊ณ๊ฐ ์๋ชป ํ์ฑ๋๋ ๊ฒฝ์ฐ !
: A→B ๋ฅผ ์ฐธ์กฐํ๊ณ B→A๋ฅผ ์ฐธ์กฐํ ๋, ์ํ ์์กด๊ด๊ณ๊ฐ ํ์ฑ๋๋ฉฐ BeanCurrentlyInCreateionException ์์ธ ๋ฐ์
์ปดํฌ๋ํธ ์ค์บ
: Spring์ด ์ง์ ํด๋์ค๋ฅผ ๊ฒ์ํด์ Bean๋ค์ด ์๋์ผ๋ก IoC ์ปจํ ์ด๋์ ๋ฑ๋กํด์ฃผ๋ ๊ธฐ๋ฅ. ์ค์ ํด๋์ค๋ฅผ ๋น์ผ๋ก ๋ฑ๋กํ์ง ์์๋ ์ํ๋ ํด๋์ค๋ฅผ ๋น์ผ๋ก ๋ฑ๋กํ ์ ์๋ค.
: Configuration class์ Bean์ ์ง์ class๋ฅผ ์์ฑ, ์ ์ํด์ฃผ์ง ์์๋ ๋๋ค.
์คํ๋ง์ ์ด๋ค ์์ผ๋ก ์๋์ผ๋ก ๋ฑ๋ก๋ Bean์ ์ธ์ํ ๊น?
⇒ ์คํธ๋ ์ค ํ์
: Stereotype ์ด๋ ธํ ์ด์ ์ ์ด์ฉํ๋ฉด ์ค์บ๋์์ ์ง์ ํ ์ ์๋ค.
: ๊ณ ์ ๊ด๋ ์ด๋ผ๋ ๋ป์ ์ฉ์ด๋ก UML์์ ์จ ๊ฐ๋
→ UML ๋ค์ด์ด๊ทธ๋จ์ ํ์ฅ์์ผ์ฅฌ๋ ๋๊ตฌ๋ก ์ํฉ์ด๋ ๋๋ฉ์ธ์ ๋ง๊ฒ ๋ถ๋ฅ๋ฅผ ํด์ค๋ค.
์ถ์ฒ - https://incheol-jung.gitbook.io/docs/q-and-a/spring/stereo-type
Spring 2.5๋ถํฐ ๋ฒ์ฉ์ ์ผ๋ก ์ฌ์ฉํ์์ผ๋ฉฐ ์ด์ ๊น์ง๋ xml ํ์ผ์ bean์ ๋ฑ๋กํ์ฌ ๊ด๋ฆฌํ์๋๋ค.
Stereo type์ ํตํด ๊ฐ๊ฐ์ ์ฉ๋์ ๋ง๊ฒ Bean์ ๋ถ๋ฅํด์ค๋ค.
- @Component
@Configuration : ํด๋น class๊ฐ java ๊ธฐ๋ฐ์ผ๋ก meta ๋ฐ์ดํฐ๋ฅผ ์ค์ ํ๋ class์ด๋ค.
@Controller : MVC controller๋ก ์ธ์
@Service : ๋น์ง๋์ค ์๋น์ค ๊ณ์ธต์ผ๋ก ์ธ์
@Repository : ์คํ๋ง ๋ฐ์ดํฐ ์ ๊ทผ ๊ณ์ธต์ผ๋ก ์ธ์ํ๊ณ ํด๋น ๊ณ์ธต์์ ๋ฐ์ํ๋ ์์ธ๋ ๋ชจ๋ DataAccessException์ผ๋ก ๋ณํํ๋ค.
- @ComponetScan
: ์ด๋ ธํ ์ด์ ์ ํตํด ์๋์ผ๋ก ๋ฑ๋ก๋๊ฒ ์๋ ค์ค๋ค. → ํด๋น class๊ฐ ์๋ ํจํค์ง ๊ธฐ์ค์ผ๋ก Beand์ ์ฐพ์.
: (basePackages = {”org.progrms.kdt.order”,”org.progrms.kdt.voucher”}) ํน์
(basePackageClasses = {Order.class,Voucher.class}) ์ธ์๋ฅผ ์ถ๊ฐํ์ฌ ์ค์บํ ํจํค์ง๋ฅผ ์ค์ ํ ์ ์๋ค.
: excludeFilters๋ฅผ ํตํด ์ ์ธ๋ ๊ฐ๋ฅ
Autowired
์์กด๊ด๊ณ ์๋ ์ฃผ์ .
์์ฑ์๋ฅผ ์๋ตํ๊ณ field ํน์ set๋ฉ์๋์ @Autowired ์ด๋ ธํ ์ด์ ์ ๋ถ์ฌ ์๋ ์ฃผ์ ์ด ๊ฐ๋ฅํ๋ค.
๋ณ๊ฒฝ์
@Service
public class VoucherService {
private final VoucherRepository voucherRepository;
public VoucherService(VoucherRepository voucherRepository) {
this.voucherRepository = voucherRepository;
}
public Voucher getVoucher(UUID voucherId) {
return voucherRepository.findById(voucherId)
.orElseThrow(() -> new RuntimeException("Can not find a voucher for " + voucherId));
}
public void useVoucher(Voucher voucher) {
}
}
๋ณ๊ฒฝํ
@Service
public class VoucherService {
@Autowired
private VoucherRepository voucherRepository;
public Voucher getVoucher(UUID voucherId) {
return voucherRepository.findById(voucherId)
.orElseThrow(() -> new RuntimeException("Can not find a voucher for " + voucherId));
}
public void useVoucher(Voucher voucher) {
}
}
- ์์ฑ์๊ฐ 2๊ฐ์ผ ๊ฒฝ์ฐ, ์๋์ฃผ์ ์ด ๋์ด์ผํ๋ ๋ฉ์๋์ @Autowired๋ฅผ ๋ฌ์์ค์ผํ๋ค.
@Service
public class VoucherService {
private VoucherRepository voucherRepository;
public VoucherService() {}
@Autowired
public VoucherService(VoucherRepository voucherRepository) {
this.voucherRepository = voucherRepository;
}
public Voucher getVoucher(UUID voucherId) {
return voucherRepository.findById(voucherId)
.orElseThrow(() -> new RuntimeException("Can not find a voucher for " + voucherId));
}
public void useVoucher(Voucher voucher) {
}
}
์์ฑ์ ๊ธฐ๋ฐ ์์กด๊ด๊ณ ์ฃผ์ ์ ์ ํํด์ผํ๋ ์ด์
- ์ด๊ธฐํ์ ํ์ํ ๋ชจ๋ ์์กด๊ด๊ณ๊ฐ ํ์ฑ๋๊ธฐ ๋๋ฌธ์ ์์ ํ๋ค. (๋ง์ฝ field๋ฅผ ๋์ค์ setํด์ผํด์ ์์๊ฒฝ์ฐ Optional ํ์ ์ผ๋ก ์ ์)
- ์๋ชป๋ ํจํด์ ์ฐพ์ ์ ์๊ฒ ๋์์ค๋ค.
- ํ ์คํธ๋ฅผ ์ฝ๊ฒ ํด์ค๋ค.
- ๋ถ๋ณ์ฑ์ ํ๋ณดํ๋ค. final ํค์๋ →thread safe ํ๋ณด
๋๊ฐ์ ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ ๊ฐ์ฒด๊ฐ ์ฌ๋ฌ๊ฐ์ผ๋
์ธํฐํ์ด์ค๋ฅผ ์ฃผ์ ๋ฐ๋๋ฐ ์ฌ๋ฌ๊ฐ์ bean์ด ๊ตฌํ๋์ด์์๋ ์ด๋ค bean์ด ์ฃผ์ ์ด ๋์ด์ผํ๋์ง ์๋ ค์ค์ผํ๋ค.
- @Primary ๋ฑ๋กํด์ ์ฐ์ ์์ ์ฃผ๊ธฐ
- @Qualifier(”์ด๋ฆ”) ์ ํตํด ๊ตฌ๋ถํ๊ธฐ
Bean์ด ์ฌ๋ฌ๊ฐ์ผ๋ Configuration์ ์ฌ๋ฌ๊ฐ ์์ฑํ์ฌ Bean ์ค์ ์ ๋ถ๋ฅ์ ๋ง๊ฒ ์ ๊ตฌ๋ถํด ์ค์ผํ๋ค.
: @Configuration ์ ๋ ธํ ์ด์ ๋ํ ์ปดํฌ๋ํธ ์ค์บ์ ๋์์ด ๋๊ธฐ ๋๋ฌธ์ ์ค์ ์ ์ฌ๋ฌ ํ์ผ๋ก ๋ถ ๋ฆฌํ๊ณ ๋ฉ์ธ์ด ๋๋ ๊ณณ์์ ์ปดํฌ๋ํธ ์ค์บ์ผ๋ก ๋ถ๋ฌ์ฌ ์๋์๋ค. ์ค์ ๋ก ๋ณต์กํ ์์ฉ ์คํ๋ง ์ด ํ๋ฆฌ์ผ์ด์ ์ ๋ง๋ค๋ค๋ณด๋ฉด ์ค์ ํ ๋ด์ฉ์ด ๋ง๊ธฐ ๋๋ฌธ์ ์ฌ๋ฌ ํ์ผ๋ก ๋๋๊ฒ ๋ฉ๋๋ค.
: SpringBoot๋ฅผ ์ฌ์ฉํ๋ฉด @SpringBootApplication์ ์ด์ฉํ๋๋ฐ ์ค์ ์ด๊ณณ์์ ์ปดํฌ๋ํธ ์ค์บ์ด ์ผ์ด๋๊ธฐ ๋๋ฌธ์ ํด๋น ํด๋์ค ๊ธฐ์ค์ผ๋ก ์ค์บ์ด ์ผ์ด๋๊ณ , ๋ณ๋์ ๋ฃจํธ @Configuration ํ์ผ์ ๋ง๋ค์ด ์ค ํ์๊ฐ ์๋ค.
Beans Scope
๊ณต์๋ฌธ์๋ฅผ ๋ณด๋ฉด ์ด 6๊ฐ์ ์ค์ฝฅ์ ๋ณผ ์ ์์ผ๋ฉฐ, ๊ธฐ๋ณธ์ ์ผ๋ก ์ฑ๊ธํค ์ค์ฝฅ์ ๊ฐ์ง๋ค.(default)
์ฑ๊ธํค ์ค์ฝฅ
ApplicationContext์์ getBean์ ํ ๋ ๋ง๋ค ์๋ก์ด ๊ฐ์ฒด๊ฐ ๋ง๋ค์ด์ง๋ ๊ฒ์ด ์๋๋ผ ํ๋์ ๊ฐ์ฒด๊ฐ ๋ฐํ๋๋ค.
var voucherRepository = applicationContext.getBean(VoucherRepository.class);
var voucherRepository2 = applicationContext.getBean(VoucherRepository.class);
System.out.println(voucherRepository == voucherRepository2); // True
ํ๋กํ ํ์ ์ค์ฝฅ
๋งค๋ฒ ์๋ก์ด ๊ฐ์ฒด๋ฅผ ๋ง๋ค๊ณ ์ถ์๋ ํ๋กํ ํ์ ์ค์ฝฅ์ ์ ์ํด ์ค๋ค.
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Repository
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class MemoryVoucherRepository implements VoucherRepository {
.....
}
var voucherRepository = applicationContext.getBean(VoucherRepository.class);
var voucherRepository2 = applicationContext.getBean(VoucherRepository.class);
System.out.println(voucherRepository == voucherRepository2); // False
Bean Lifescycle Callbacks
Spring Application Context๋ ๊ฐ์ฒด ์์ฑ๊ณผ ์๋ฉธ์ฃผ๊ธฐ๋ฅผ ๊ด๋ฆฌ
Bean ์์ฑ ์๋ช ์ฃผ๊ธฐ ์ฝ๋ฐฑ
- @PostConstruct ์ ๋ ธํ ์ด์ ์ด ์ ์ฉ๋ ๋ฉ์๋ ํธ์ถ
- Bean์ด InitializingBean ์ธํฐํ์ด์ค ๊ตฌํ์ afterPropertiesSet ํธ์ถ
- @Bean ์ ๋ ธํ ์ด์ ์ initMethod ์ ์ค์ ํ ๋ฉ์๋ ํธ์ถ
: ์์ฑ๋ ๋ ์ํ๋์ผํ๋ ๋์์ด ์์ ๊ฒฝ์ฐ ๊ตฌํ
Bean ์๋ฉธ ์๋ช ์ฃผ๊ธฐ ์ฝ๋ฐฑ
- @PreDestory ์ ๋ ธํ ์ด์ ์ด ์ ์ฉ๋ ๋ฉ์๋ ํธ์ถ
- Bean์ด DisposableBean ์ธํฐํ์ด์ค ๊ตฌํ์ destroy ํธ์ถ
- @Bean ์ ๋ ธํ ์ด์ ์ destroyMethod ์ ์ค์ ํ ๋ฉ์๋ ํธ์ถ
→ clean up , ๋คํธ์ํฌ close๊ฐ์ ์์ ์ด ์์๋ ์ฌ์ฉํ๋ค.
Daliy Mission
- ์ปดํฌ๋ํธ ์ค์บ์ ํตํด ๋น์ด ๋ฑ๋ก๋๊ฒ ๊ตฌํํด๋ณด๊ธฐ
- ๋ฉ๋ชจ๋ฆฌ ๊ด๋ฆฌ๊ฐ ์๋ ํ์ผ๋ก ๊ด๋ฆฌ๊ฐ ๋๋ Repository ๋ง๋ค์ด๋ณด๊ธฐ
- ๊ธฐ์กด ๋ฉ๋ชจ๋ฆฌ ๋ ํฌ์งํ ๋ฆฌ ๋ณ๋๋ก ๋ณด๊ด
์ถ์ฒ - ํด๋ฆฌ : 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] 221101 - SpringBoot Part1 : IoC, DDD, ApplicationContext (0) | 2022.11.03 |
[TIL] 221031 - SpringBoot Part1 (0) | 2022.11.02 |
๋๊ธ