week 1 sangyun - GANGNAM-JAVA/JAVA-STUDY GitHub Wiki
์๋ฐ
JVM,JDK,JRE๋ ๋ฌด์์ธ๊ฐ?
- JDK๋ Java Development Kit์ ์ฝ์
- JDK๋ JRE๋ฟ ์๋๋ผ ๊ฐ๋ฐ์ ํ์ํ ํ๋ก๊ทธ๋จ๋ค๋ ํฌํจ๋์ด ์๋ค.
- ์ปดํ์ผ๋ฌ, ์คํํ์ผ์ ์คํํ๋ ํ๋ก๊ทธ๋จ๋ฑ์ด ํฌํจ
- JRE๋ Java Runtime Environment์ ์ฝ์
- JVM์ ์ค์นํ ๋ ์ ํํ๋ ๋๊ตฌ
- ์๋ฐ ์คํ์ ๊ด๋ จ๋ ํ์ผ๋ค๋ ํจ๊ป ๊ตฌ์ฑ
- Java 11๋ถํฐ๋ JDK์ JRE๊ฐ ํฌํจ๋์ด ์์
์๋ฐ ๋ฉ๋ชจ๋ฆฌ ์์ญ์ ๋ํด ์ค๋ช ํ์์ค
-
์๋ฐ์ ๋ฉ๋ชจ๋ฆฌ ์์ญ์ JVM์ด ์คํ๋๋ฉด์ OS๋ก๋ถํฐ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํ ๋น๋ฐ๋๋ค.
-
Runtime Data Area๊ฐ ์๋ฐ ํ๋ก๊ทธ๋จ์ ๋ฉ๋ชจ๋ฆฌ ์์ญ์ด๋ค.
-
Class(Method)
- ํด๋์ค ๋ฉค๋ฒ ๋ณ์์ ์ด๋ฆ, ๋ฐ์ดํฐ ํ์ , ์ ๊ทผ ์ ์ด์ ์ ๋ณด๊ฐ์ ํ๋ ์ ๋ณด์ ๋ฉ์๋์ ์ด๋ฆ
-
Stack
- ์ค๋ ๋๋ณ๋ก ๋ณ๋ ์์ฑ
- ์ง์ญ๋ณ์, ํ๋ผ๋ฏธํฐ, ๋ฆฌํด ๊ฐ, ์ฐ์ฐ์ ์ฌ์ฉ๋๋ ์์ ๊ฐ๋ฑ์ด ์์ฑ๋๋ ์์ญ
-
Heap
- new ํค์๋๋ก ์์ฑ๋ ๊ฐ์ฒด์ ๋ฐฐ์ด์ด ์์ฑ๋๋ ์์ญ
-
Native Method
- java๊ฐ ์๋ ๋ค์ดํฐ๋ธ ์ฝ๋๊ฐ ๋ค์ด๊ฐ๋ ์์ญ
-
PC Register
- ์ค๋ ๋๊ฐ ์์ฑ๋ ๋๋ง๋ค ์์ฑ๋๋ ์์ญ
- ํ์ฌ ์ค๋ ๋๊ฐ ์คํ๋๋ ๋ถ๋ถ์ ์ฃผ์์ ๋ช ๋ น์ ์ ์ฅ
-
Java8 ๋ถํฐ ํ์์ญ์ ๋ณํ๊ฐ ์๊น
- PERMEN GEN์ ์์ ๊ณ METASPACE๋ผ๋ ๊ณต๊ฐ์ด ์๊น
- Permen Gen ์์ญ์๋ ํด๋์ค,๋ฉ์๋์ ๋ฉํ์ ๋ณด, static ๋ฑ์ด ์ ์ฅ
- static object๋ฅผ ์์ฑํ๋๊ฒฝ์ฐ ๊ฐ์ฒด์ ๋ชจ๋ ๋ถ๋ถ์ด PermGen์ ์ ์ฅ๋๊ธฐ ๋๋ฌธ์ OOM ๋ฐ์
- Java8 ์์๋ Metaspace๋ฅผ ๋์ ํด์ Static Object ๋ฐ ์์ํ๋ String Object๋ฅผ heap ์์ญ์ผ๋ก ์ฎ๊ฒจ์ ์ต๋ํ GC ๋ ์ ์๋๋ก ์ค์
๊ฐ๋น์ง ์ปฌ๋ ํฐ ์๋ ์๋ฆฌ๋ฅผ ์ค๋ช ํ์์ค
minor GC : Heap ์์ญ์ young generation
young generation์ eden ์์ญ๊ณผ survivor์์ญ์ผ๋ก ๊ตฌ๋ถ ํ๋ค. ์๋ก์ด ๊ฐ์ฒด ๋๋ถ๋ถ์ eden ์์ญ์ ์ฌ๋ผ๊ฐ๊ณ ๋์ด์ ์ฌ์ฉํ์ง ์๋ ๊ฐ์ฒด๋ GC๋นํ๊ณ ์ด์ ๋จ๋ ๊ฐ์ฒด๋ survivor๋ก ์ด๋์ํจ๋ค.
์ด๋, survivor ์์ญ์ด ๊ฝ ์ฐจ๋ฉด ๊ทธ์ค ์ด์๋จ์ ๊ฐ์ฒด๋ ๋ค๋ฅธ survivor๋ก์ด๋ ์ํจ๋ค. ์ด ๊ณผ์ ์ ๋ฐ๋ณตํ๋ฉด์ ์ด์๋จ์ ๊ฐ์ฒด๋ old generation์ผ๋ก ์ด๋ํ๋ค.
major GC : Heap ์์ญ์ old generation
Serial GC
- ์ ์ ๋ฉ๋ชจ๋ฆฌ์ CPU์ฝ์ด๊ฐ ์ ์๋ ์ฌ์ฉ
- Old ์์ญ์์ ์ด์ ์๋ ๊ฐ์ฒด๋ฅผ ์๋ณ(Mark)
- heap ์ ๋ถ๋ถ๋ถํฐ ํ์ธํ์ฌ ์ด์์๋ ๊ฒ๋ง ๋จ๊ธด๋ค(Sweep)
- ๊ฐ ๊ฐ์ฒด๋ค์ด ์ฐ์๋๊ฒ ์์ด๋๋ก ํ์ ๊ฐ์ฅ ์๋ถ๋ถ๋ถํฐ ์ฑ์์ ๊ฐ์ฒด๊ฐ ์กด์ฌํ๋ ๋ถ๋ถ๊ณผ ์กด์ฌํ์ง ์๋ ๋ถ๋ถ์ผ๋ก ๊ตฌ๋ถํ๋ค(Compaction)
Parallel GC
- Serial GC์ ๊ฐ์ ๋ฐฉ์์ด์ง๋ง GC๋ฅผ ์ฒ๋ฆฌํ๋ ์ค๋ ๋๊ฐ ์ฌ๋ฌ๊ฐ์ด๋ค.
- ๋ฉ๋ชจ๋ฆฌ์ CPU ์ฝ์ด๊ฐ ๋ง์๋ ์ ๋ฆฌํ๋ค.
Concurrent Mark & Sweep GC (CMS)
- mark ๋จ๊ณ์์๋ ์ด์ ์๋ ๊ฐ์ฒด๋ง ์ฐพ๋ ๊ฒ์ผ๋ก ๋๋ธ๋ค.
- concurrent mark ๋จ๊ณ์์๋ ๋ฐฉ๊ธ ์ด์์๋ค๊ณ ํ์ธํ ๊ฐ์ฒด์์ ์ฐธ์กฐํ๊ณ ์๋ ๊ฐ์ฒด๋ค์ ๋ฐ๋ผ๊ฐ๋ฉด์ ํ์ธ(
์ค๋ ๋๊ฐ ์คํ ์ค์ธ ์ํ์์ ๋์์ ์งํ
) - remark ๋จ๊ณ์์๋ concurrent mark๋จ๊ณ์์ ์๋ก ์ถ๊ฐ๋๊ฑฐ๋ ์ฐธ์กฐ๊ฐ ๋๊ธด ๊ฐ์ฒด๋ฅผ ํ์ธ
- concurrent sweep ๋จ๊ณ์์๋ ๋ ์ด์ ์ฌ์ฉํ์ง ์๋ ๊ฐ์ฒด๋ฅผ ์ ๋ฆฌํ๋ ์์
(
์ค๋ ๋๊ฐ ์คํ ์ค์ธ ์ํ์์ ๋์์ ์งํ
) - JVM์ด ๋ฉ์ถ๋ ์๊ฐ์ด ๋งค์ฐ ์งง๋ค.
- ์๋ต ์๋๊ฐ ๋งค์ฐ ์ค์ํ ๋ ์ฌ์ฉ
- CPU ์ฝ์ด์ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋์ด ๋๋ค.
- compaction ๋จ๊ณ๊ฐ ์์
G1(Garbage First) GC
- initial mark
- root region scan
- concurrent mark
- remark
- cleanup
- copy
https://d2.naver.com/helloworld/1329 https://mirinae312.github.io/develop/2018/06/04/jvm_gc.html
์คํ๋ง
์คํ๋ง ๋ถํธ์ ์คํ๋ง์ ์ฐจ์ด
์คํ๋ง
- ์คํ๋ง ํ๋ ์ ์ํฌ๋ ์๋ฐ ํ๋ซํผ์ ์ด์ฉํด์ ์น์ ํ๋ฆฌ์ผ์ด์
์ ๊ฐ๋ฐํ๋๋ฐ ํ์ํ
ํ๋ถ ๊ตฌ์กฐ
๋ฅผ ์ ๊ณตํ๋ ํ๋ ์์ํฌ์ด๋ค. - ๊ฐ๋ฐ์๋ ์ ํ๋ฆฌ์ผ์ด์ ๊ฐ๋ฐ์ ์ง์ค
- ํต์ฌ์ ์ํฐํ๋ผ์ด์ฆ ์๋น์ค ๊ธฐ๋ฅ์
POJO
์๊ฒ ์ ๊ณต- POJO ๋ Plain Old Java Oeject์ ์ฝ์๋ก ํ๊ฒฝ๊ณผ ๊ธฐ์ ์ ์ข ์๋์ง ์๊ณ ํ์์ ๋ฐ๋ผ ์ฌํ์ฉ๋ ์ ์๋ ๋ฐฉ์์ผ๋ก ์ค๊ณ๋ ๊ฐ์ฒด
- Spring JDBC, Spring MVC, Spring Security, Spring AOP, Spring ORM ํ๋ก์ ํธ์ Dependency Injection, IoC๋ฅผ ์ ๊ณตํ๋ค.
๋ฆฌ์กํฐ๋ธ ์คํ
- ์คํ๋ง WebFlux
- ๋ฉํฐ์ฝ์ด, ์ฐจ์ธ๋ ํ๋ก์ธ์๋ฅผ ํ์ฉํ์ฌ ๋์์ ๋ง์ ์์ฒญ์ ์ฒ๋ฆฌํ๊ธฐ ์ํด ๋ง๋ค์ด์ง ๋ ผ๋ธ๋กํน I/O
์๋ธ๋ฆฟ ์คํ
- ์คํ๋ง MVC
- ์๋ธ๋ฆฟ API๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์ค๋ ๋๋น ํ๋์ ์์ฒญ์ ์ฒ๋ฆฌํ๋ ๋ชจ๋ธ๋ก ๋๊ธฐ ๋ธ๋กํน I/O ๋ฐฉ์์ด๋ค.
์คํ๋ง ๋ถํธ
- ์คํ๋ง ํ๋ ์์ํฌ๋ฅผ ์ฌ์ฉํ ๋ ๋ง์ ์ค์ ์ด ํ์ํ๋ค. ์คํ๋ง ๋ถํธ ํ๋ก์ ํธ๋ ์คํ๋ง์ ์ด์ฉํด์ ์น์ ํ๋ฆฌ์ผ์ด์ ์ ๊ฐ๋ฐํ๋๋ฐ ํ์ํ ๊ธฐ๋ณธ ์ค์ ์ ์๋ํํ ํ๋ก์ ํธ์ด๋ค.
์คํ๋ง์ ํน์ง์ ๋ํด์ ์ฌ์ฉ ์ฌ๋ก๋ฅผ ๋น๋์ด ์ค๋ช ํ์์ค
Dipendency Injection์ด ํ์ํ ์ด์ ?
- ์ผ๋ฐ์ ์ผ๋ก ์ํฐํ๋ผ์ด์ฆ ์ ํ๋ฆฌ์ผ์ด์ ์ ๊ฐ๋ฐํ ๋ ํ๋์ ์ฒ๋ฆฌ๋ฅผ ์ํํ๊ธฐ ์ํด ์ฌ๋ฌ๊ฐ์ ์ปดํฌ๋ํธ๋ฅผ ์กฐํฉํด์ ๊ตฌํํ๋ ๊ฒฝ์ฐ๊ฐ ๋ง๋ค.
- ์ฝ๋์ ๋ณต์ก์ฑ์ด ์ฌ๋ผ๊ฐ๊ฒ๋๋ฉด ์ปดํฌ๋ํธ๋ค๊ฐ์
๊ฒฐํฉ๋๊ฐ ์ฌํด์ง๊ณ
๋์ ๊ฒฐํฉ๋๋ก ์ธํด์ํ ์คํธํ๊ธฐ ํ๋ค์ด์ง๊ณ
๋ณ๊ฒฝํ๊ธฐ ํ๋ค์ด์ง๋ค
. - ์ปดํฌ๋ํธ๋ฅผ ๊ตฌ์ฑํ๋ ์ธ์คํด์ค์ ์์ฑ๊ณผ ์์กด ๊ด๊ณ์ ์ฐ๊ฒฐ ์ฒ๋ฆฌ๋ฅผ ํด๋น ์์ค์ฝ๋๊ฐ ์๋
DI ์ปจํ ์ด๋
๊ฐ ๋์ ํด์ค๋ค.
DI ์ปจํ ์ด๋๋ฅผ ์ด์ฉํด์ ์ธ์คํด์ค ๊ด๋ฆฌ์ ์ฅ์ ์?
- ์ธ์คํด์ค์
์ค์ฝํ
๋ฅผ ์ ์ด - ์ธ์คํด์ค์
์๋ช ์ฃผ๊ธฐ
๋ฅผ ์ ์ด - AOP ๋ฐฉ์์ผ๋ก
๊ณตํต ๊ธฐ๋ฅ
์ ์ง์ด๋ฃ์ ์ ์๋ค. - ์์กดํ๋ ์ปดํฌ๋ํธ ๊ฐ์
๊ฒฐํฉ๋๋ฅผ ๋ฎ์ถฐ์ ๋จ์ํ ์คํธ
๋ฅผ ์ฝ๊ฒ ๋ง๋ ๋ค.
BeanFactory, ApplicationContext ?
-
BeanFactory๋ ์คํ๋ง ์ค์ ํ์ผ(applicaitonContext.xml)์ ๋ฑ๋ก๋
bean ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ณ ๊ด๋ฆฌํ๋ ๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ ์ปจํ ์ด๋ ๊ธฐ๋ฅ
๋ง ์ ๊ณตํ๋ค. ๋ํ, ์ปจํ ์ด๋๊ฐ ๊ตฌ๋๋ ๋ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ๊ฒ์ด ์๋๋ผ ํด๋ผ์ด์ธํธ๋ก๋ถํฐ์ ์์ฒญ์ ์ํด์๋ง ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค.(์ง์ฐ ๋ก๋ฉ) -
ApplicationContext๋ BeanFactory๋ฅผ ์์ํ์๋๋ฐ BeanFactory๊ธฐ๋ฅ์ ๋ํด
ํธ๋์ญ์ ๊ด๋ฆฌ
๋๋ฉ์์ง ๊ธฐ๋ฐ์ ๋ค๊ตญ์ด ์ฒ๋ฆฌ
๋ฑ์ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ค. ์ปจํ ์ด๋๊ฐ ๊ตฌ๋๋๋ ์์ ์ bean์ ๋ฑ๋ก๋์ด ์๋ ํด๋์ค๋ค์ ๊ฐ์ฒดํํ๋ ์ฆ์ ๋ก๋ฉ ๋ฐฉ์์ผ๋ก ๋์. -
ApplicationContext์ ๊ตฌํ์ฒด์ธ
AnnotationConfigApplicationContext ํด๋์ค
๋ฅผ ์ด์ฉํด์ ์ ๋ ธํ ์ด์ ๊ธฐ๋ฐ์ ์ค์ ์ด ๊ฐ๋ฅํ๊ฒ ๋์๋ค.
Bean์ ์๋ช ์ฃผ๊ธฐ๋ฅผ ์ค๋ช ํด์ฃผ์ธ์
Initialize
: Bean Object๊ฐ ์์ฑ๋๊ณ DI๋ฅผ ๋ง์น ํ ์คํ๋๋ ๋ฉ์๋@PostConstruct
: ์ด๊ธฐํ ํ๊ณ ์ถ์ ๋ฉ์๋์ @PostConstruct ์ด๋ ธํ ์ด์ ์ ๋ถ์ด๋ฉด ์คํ๋ง์ด ํด๋น ๋ฉ์๋๋ฅผ ์ด๊ธฐํ@Bean(initMethod)
: @Bean์ด๋ ธํ ์ด์ ์ initMethod ์์ฑ์ ์ด์ฉํด ์ด๊ธฐํ ๋ฉ์๋๋ฅผ ์ง์ ํ ์ ์๋ค.InitializingBean
: InitializingBean ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ๋ฉด ์คํ๋ง์ด afterPropertiesSet ๋ฉ์๋๋ฅผ ์ด๊ธฐํ์์ ํธ์ถํ๋ค.@PreDestory
: ์ปจํ ์ด๋๊ฐ ์ข ๋ฃ ๋ ๋ ์คํํ๊ณ ์ถ์ ๋ฉ์๋์ ํด๋น ์ด๋ ธํ ์ด์ ์ ๋ถ์ด๋ฉด ๋๋ค.DisposableBean
: DisposableBean์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ๋ฉด ์คํ๋ง์ด destroy๋ฉ์๋๋ฅผ ํธ์ถ@Bean(destoryMethod)
: destroyMethod ์์ฑ์ ์ด์ฉํด ์ปจํ ์ด๋ ์ข ๋ฃ์ ์คํํ๊ณ ์ ํ๋ ๋ฉ์๋๋ฅผ ์ง์ ํ ์ ์๋ค.close & shutDown
: DisposableBeanAdapter์ ์ํด ์คํ
Bean ์ค์ฝํ๋ฅผ ์ค๋ช ํด์ฃผ์ธ์
-
Singleton
- ์คํ๋ง ๋น(Beam)์ ๊ธฐ๋ณธ์ ์ผ๋ก singleton ํ์ ์ผ๋ก ์์ฑ์ด๋๋ค. singleton ๋น์ ์คํ๋ง ์ปจํ ์คํธ์ ํ๋์ ์ธ์คํด์ค๋ง ์กด์ฌํ๋ค. ๋ฐ๋ผ์ ์๋๊ฒฐ๊ณผ์ฒ๋ผ applicationContext.getBean()์ผ๋ก ๊ฐ์ ํ์ ์ 2๊ฐ ๊ฐ์ง๊ณ ์๋ ๋๊ฐ์ ํด์ฌ์ฝ๋๋ ๊ฐ์์ ์ ์ ์๋ค.
package com.spring.core.springcore;
import com.spring.core.springcore.basic.BinarySearchimpl;
import com.spring.core.springcore.basic.BubbleSortAlgorithm;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class SpringCoreApplication {
public static void main(String[] args) {
/**
* How to check beans in Spring Framework
*
* 1. What are the beans? @Component annotation
* 2. What are the dependencies of a bean? @Autowired annotation
* 3. Where to search for beans? package com.spring.core.springcore์ ์์นํ SpringCoreApplication ํด๋์ค์ @SpringBootApplication ์ด๋
ธํ
์ด์
์ ์ํด ์ปดํฌ๋ํธ ์ค์บ์ด ์คํ๋๋ค.
*/
ApplicationContext applicationContext =
SpringApplication.run(SpringCoreApplication.class, args);
//singleton bean
BinarySearchimpl binarySearch1 = applicationContext.getBean(BinarySearchimpl.class);
//singleton bean
BinarySearchimpl binarySearch2 = applicationContext.getBean(BinarySearchimpl.class);
//same hashCode because of singleton bean
System.out.println(binarySearch1 == binarySearch2);
}
}
-
Prototype
- ์ฑ๊ธํค ๋น์ ์ด๋์์ ๋น์ ์ฃผ์
๋ฐ๋ ํญ์ ๋์ผํ ๋น์ ์ฃผ์
๋ฐ๊ฒ ๋๋ค. ํ์ง๋ง, ๋น ์ฃผ์
๋ง๋ค
์๋ก์ด ๋น์ ์์ฑํด์ผํ ํ์๊ฐ ์์๊ฒฝ์ฐ์๋ Prototype ๋น
์ ์์ฑํด์ผํ๋ค. Prototype ์ผ๋ก ๋น์ ์์ฑํ๋ฉด ๋น์ ์ฃผ์ ํ ๋๋ง๋ค ์๋ก์ด ์ธ์คํด์ค๊ฐ ์์ฑ๋๋ค. @Scope ์ด๋ ธํ ์ด์ ์ผ๋ก ํ์ ์ ์ค์ ํ๋ฉด ์ค์ ๋ ํ์ ์ผ๋ก ๋น์ด ์์ฑ๋๋ค. ์ฑ๊ธํค์ผ๋ก ์์ฑ๋ A ๋น
์ํ๋กํ ํ์ ์ธ B ๋น
์ ์ฃผ์ ํ๋ฉดA๋น ์์ฒด๊ฐ ๋จ ํ๋ฒ๋ง ์์ฑ๋๊ธฐ ๋๋ฌธ์ B๋ํ ํญ์ ๋์ผํ ๋น
์ ์ฌ์ฉํ๋ ํ์์ด ๋ฐ์ํ ์ ์๋ค.
- ์ฑ๊ธํค ๋น์ ์ด๋์์ ๋น์ ์ฃผ์
๋ฐ๋ ํญ์ ๋์ผํ ๋น์ ์ฃผ์
๋ฐ๊ฒ ๋๋ค. ํ์ง๋ง, ๋น ์ฃผ์
๋ง๋ค
package com.spring.core.springcore.basic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public class BinarySearchimpl{
@Autowired
@Qualifier("quick")
private SortAlgorithm sortAlgorithm;
public int binarySearch(int [] numbers, int numberToSearchFor) {
int [] sortedNumbers = sortAlgorithm.sort(numbers);
return 3;
}
}
package com.spring.core.springcore;
import com.spring.core.springcore.scope.PersonDAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
public class SpringCoreScopeApplication {
private static Logger logger = LoggerFactory.getLogger(SpringCoreScopeApplication.class);
public static void main(String[] args) {
ApplicationContext applicationContext =
SpringApplication.run(SpringCoreBasicApplication.class, args);
PersonDAO personDao1 =
applicationContext.getBean(PersonDAO.class);
PersonDAO personDao2 =
applicationContext.getBean(PersonDAO.class);
logger.info("{}", personDao1.getJdbcConnection());
logger.info("{}", personDao2.getJdbcConnection());
logger.info("{}", personDao1);
logger.info("{}", personDao2);
}
}
package com.spring.core.springcore.scope;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class JdbcConnection {
public JdbcConnection() {
System.out.println("JDBC Connection");
}
}
package com.spring.core.springcore.scope;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
public class PersonDAO {
@Autowired
JdbcConnection jdbcConnection;
public JdbcConnection getJdbcConnection() {
return jdbcConnection;
}
public void setJdbcConnection(JdbcConnection jdbcConnection) {
this.jdbcConnection = jdbcConnection;
}
}
Request
- request ๋จ์๋ง๋ค ์ ์ธ์คํด์ค๋ฅผ ์์ฑํ๋ค.
Session
- ํ๋์ Bean ์ ์์ ๋ํด์ ํ๋์ HTTP Session์ ์๋ช ์ฃผ๊ธฐ ์์ ๋จ ํ๋์ ๊ฐ์ฒด๋ง ์กด์ฌํ๋ค.
global session
- ํ๋์ Bean ์ ์์ ๋ํด์ ํ๋์ global HTTP Session์ ์๋ช ์ฃผ๊ธฐ ์์ ๋จ ํ๋์ ๊ฐ์ฒด๋ง ์กด์ฌํ๋ค.
Dependency Injection ๋ฐฉ๋ฒ ์ค๋ช
- Field Injection
- Setter Based Injection
- Constructor based Injection
Constructor based Injection์ ์ฌ์ฉํ์
- IoC์ปจํ ์ด๋์ ๋์จํ ๊ฒฐํฉ์ ํตํด ์ปจํ ์ด๋ ์์ด๋ ํ ์คํธ๊ฐ ๊ฐ๋ฅํ๋๋ก ํ๋ค. (POJO)
- final์ ์ ์ธํ ์ ์์ด์ ๋ถ๋ณํ๊ฒ ๋ง๋ค ์ ์๋ค. (์คํ ์ค์ ๊ฐ์ฒด๊ฐ ๋ณํ๋ ๊ฒ์ ๋ง์ ์ ์๋ค.)
- ์ํ ์ฐธ์กฐ๊ฐ ๋ฐ์ํ๋ ๊ฒฝ์ฐ ์ ํ๋ฆฌ์ผ์ด์ ์ด ๊ตฌ๋๋์ง ์๋๋ค. (์ฌ์ ์ ๊ฐ์ง ๊ฐ๋ฅ)
์ฃผ์ ๋์์ด ์ฌ๋ฌ๊ฐ์ธ ๊ฒฝ์ฐ ์ด๋ป๊ฒ ํ ๊น?
- @Autowired ์ด๋
ธํ
์ด์
์ผ๋ก ๊ฐ์ฒด๋ฅผ ์ฃผ์
ํ ๊ฒฝ์ฐ ์ปจํ
์ด๋์์ ์ฃผ์
ํ ๋์์ด ์ฌ๋ฌ๊ฐ์ธ ๊ฒฝ์ฐ ์์กด์ฑ์ ์ฃผ์
ํ์ง ๋ชปํ๋ ๋ฌธ์ ๊ฐ ๋ฐ์
- ์ด๋ค ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ ๊ตฌํ์ฒด๊ฐ 2๊ฐ ์์๊ฒฝ์ฐ @Autowired๋ Type์ ๊ธฐ์ค์ผ๋ก ์ฃผ์ ๊ฐ์ฒด๋ฅผ ์ ํ๊ธฐ ๋๋ฌธ์ ์๋ฌ๋ฅผ ๋ฐ์์ํจ๋ค.
- @Qualifier์ ๊ฐ์ ํ์ ์ ๋ํด์ ์ฌ๋ฌ๊ฐ์ bean๊ฐ์ฒด๊ฐ ์์๊ฒฝ์ฐ @Qualifier ์ด๋ ธํ ์ด์ ์ ์ ๋ฌด๋ฅผ ํ์ธํด์ ์กฐ๊ฑด์ ๋ง์กฑํ๋ ๊ฐ์ฒด๋ฅผ ์ฃผ์ ํ๋ค.
- @Primary๋ ๊ฐ์ ํ์ ์ ๋ํด์ ์ฌ๋ฌ๊ฐ์ bean ๊ฐ์ฒด๊ฐ ์์๊ฒฝ์ฐ ๊ทธ ์ค @Primary ์ด๋ ธํ ์ด์ ์ด ์ ์ฉ๋ ๊ฐ์ฒด๋ง ์ฐ์ ์์๋ก ์ก์์ ์ฃผ์ ์ด ๋๋ค.
AOP ์ค๋ช ํด์ฃผ์ธ์
AOP ๊ฐ๋
- Cross Cutting Concert(ํก๋จ ๊ด์ฌ์ฌ)๋ฅผ ํ ๊ณณ์ ๋ชจ์์ ์ฒ๋ฆฌํ์
- ์ธ์ฆ ์ฒ๋ฆฌ ํน์ ๊ณตํต ๋ก๊ทธ ์์ง๊ณผ ๊ฐ์ ํก๋จ ๊ด์ฌ์ฌ๋ฅผ AOP๋ฅผ ํตํด์ ๊ตฌํ
AOP๋ฅผ ์ฌ์ฉํ์ง ๋ชปํ๋ ๊ฒฝ์ฐ?
์ํํธ์จ์ด ๊ฐ๋ฐ์ ์์ค์ฝ๋์ ๊ท๋ชจ๊ฐ ์ปค์ง๋ฉด ๋ก๊น ์ด๋ ์บ์์ ๊ฐ์ด ๋น์ฆ๋์ค ๋ก์ง๊ณผ ๋ฌด๊ดํ ๋ด์ฉ์ด ์ฝ๋์ ํฌํจ
AOP๋ฅผ ์ด์ฉํ์ง ๋ชปํ๋ฉด ์๊ธฐ๋ ๋ฌธ์
- DRT(Do not Repeat Yourself)
- ๋ณ๊ฒฝ์ ์ทจ์ฝํ๋ค.
AOP ์๋ฆฌ
- Proxy๋ฅผ ์ด์ฉํ AOP ๊ตฌํ
-
Proxy๋ ํ๊ฒ์ ๊ฐ์ธ์ ํ๊ฒ์ ์์ฒญ์ ๋์ ๋ฐ์์ฃผ๋ Wrapping ์ค๋ธ์ ํธ
-
ํธ์ถ์์์ ํ๊ฒ์ ํธ์ถํ๋ฉด ํ๊ฒ์ด ์๋ ํ๊ฒ์ ๊ฐ์ธ๊ณ ์๋ Wrapping ์ค๋ฒ์ ํธ๊ฐ ํธ์ถ๋์ด์ ์ ์ฒ๋ฆฌ,ํ์ฒ๋ฆฌ ๋ฑ์ ์งํ
-
Dynamic Proxy
- ์ธํฐํ์ด์ค์๋ง ์ ์ฉ๊ฐ๋ฅ
- java ๋ฆฌํ๋ ์ ์ ์ด์ฉํด์ ๊ฐ์ฒด๋ฅผ ๋ง๋ ๋ค.
-
CGLIB
- ํด๋์ค์ ์ ์ฉ๊ฐ๋ฅ (final ํด๋์ค์๋ ์ ์ฉ ๋ถ๊ฐ)
- BCI๋ฅผ ํตํด ๋ฐ์ดํธ์ฝ๋๋ฅผ ์กฐ์ํด์ ํ๋ก์ ๊ฐ์ฒด๋ฅผ ๋ง๋ ๋ค.
- ์คํ๋ง ๋ถํธ๋ ๊ธฐ๋ณธ์ ์ผ๋ก cglib ํ๋ก์๋ฅผ ์ฌ์ฉํ๋๋ก ์ค์ ๋์ด ์๋ค.
- cglib๊ฐ ์กฐ๊ธ ๋ ๋น ๋ฅด๊ณ ์์ธ๋ฅผ ๋ฐ์์ํฌ ๊ฐ๋ฅ์ฑ์ด ๋ฎ๋ค.
-
PSA
Spring Data JPA๋ฅผ ์ฌ์ฉํ๋ฉด ๋ฐ์ดํฐ๋ฒ ์ด์ค ๋ฒค๋์ ์ข ์๋์ง ์๊ฒ ์ฝ๋๋ฅผ ์์ฑํ ์ ์๋ค. DB๋ฅผ ๊ต์ฒดํด๋ ์ฝ๋ ๋ณํ๋ ์๋ค.
์คํ๋ง์ ์๋น์ค ์ถ์ํ๋ ๋ฌด์์ธ์ง ์ค๋ช ํ์์ค
Spring์ด ์ ๊ณตํ๋ ๋ค์ํ ์ธํฐํ์ด์ค Service Abstraction ๋๋ถ์ ๊ธฐ์ ์ด ๋ณํ๋๋ผ๋ ์ฝ๋์ ๋ณ๊ฒฝ์ ๊ฑฐ์ ์๋ค.
- PSA(Portable Service Abstraction)
- ํ๊ฒฝ๊ณผ ๊ตฌํ ๊ธฐ์ ์ ๋ณ๊ฒฝ๊ณผ ๋ฌด๊ดํ๊ฒ ์ผ๊ด๋ ๋ฐฉ์์ผ๋ก ๊ธฐ์ ์ ๋ค๋ฃฐ ์ ์๊ฒ ์ง์
- ์ํฐํ๋ผ์ด์ฆ ์ ํ๋ฆฌ์ผ์ด์
์ ์ฌ์ฉ๋๋ ๋ค์ํ ๊ธฐ์ ์ ์๋น์ค ์ถ์ํ ์ ๊ณต
- ํธ๋์ญ์ ,์บ์,๋ฉ์ผ,๋ฉ์์ง
- POJO๊ธฐ๋ฐ ์์ ๊ฐ์ฒด(๋น์ฆ๋์ค ์ค๋ธ์ ํธ)๋ ์คํ๋ง์ด ์ ๊ณตํ๋ ์๋น์ค ์ถ์ํ(์บ์๋งค๋์ )๋ก ์บ์๋ฅผ ์ ์ด
- @Cacheable์ ๊ฐ์ผ ์ด๋ ธํ ์ด์ ์ ์ด์ฉํ์ฌ ๊ตฌํ์ฒด๋ ์ ๊ฒฝ ์ฐ์ง ์๊ณ ๊ธฐ์ ์ ๋ฐ๊พธ์ด ์์ํ ์ ์๋ค.
- ๊ตฌํ๊ธฐ์ ์ด ๋ณ๊ฒฝ๋๋๋ผ๋ ๋น์ฆ๋์ค ์ค๋ธ์ ํธ์๋ ์ํฅ ์ฃผ์ง ์๋๋ค.
์ํคํ ์ฒ
๊ฐ์ฒด์งํฅ๊ณผ ์ ์ฐจ์งํฅ์ ์ฐจ์ด๋ฅผ ์ค๋ช ํ์์ค
- ๊ฐ์ฒด์งํฅ ์ค๊ณ๊ธฐ๋ฒ์ ๊ฐ์ฒด๋ค๊ฐ์ ๋ฉ์์ง๋ฅผ ์ฃผ๊ณ ๋ฐ์ผ๋ฉด์ ํ๋ ฅ์ ํตํด ์ค๊ณ๋ฅผ ํ๋ค.
- ์ ์ฐจ์งํฅ ๊ธฐ๋ฒ์ ํ๋ก๊ทธ๋จ์ ์คํ ์ ์ฐจ์ ์ด์ ์ ๋ง์ถฐ์ ์ค๊ณํ๋ค.
SOLID ์์น์ ๋ํด ์๋๋๋ก ์ค๋ช ํ์์ค
- SRP(Single Responsibility Principle) : ํ ํด๋์ค๋ ๋จ ํ๊ฐ์ง ์ญํ ์ ๊ฐ์ ธ์ผ ํ๋ค.
- OCP(Open-Closed Principle) : ๋์ํ๊ณ ์๋ ์ฝ๋๋ฅผ ๋ณ๊ฒฝํ๋ ๊ฒ์ด ์๋๋ผ ์๋ก์ด ์ฝ๋๋ฅผ ๋ง๋ถ์ฌ์ ํ์ฅํ๋ค.
- LSP(Liskob Subsitution Principle) : ์์ ํ์ ์ ๋ถ๋ชจํ์ ์ผ๋ก ์นํ๊ฐ๋ฅ
- ISP(Interface Segregation Principle) : ํด๋ผ์ด์ธํธ๊ฐ ์์ ์ด ์ฌ์ฉํ์ง ์๋ ๋ฉ์๋์ ์์กดํ์ง ์์์ผ ํ๋ค.
- DIP(Dependency Inversion Principle) : ์ถ์ํ์ ์์กดํ์.
์บก์ํ์ ๋ํด ์ค๋ช ํ์์ค
- ์บก์ํ๋ ๋ณํ ์ ์๋ ์ด๋ค ๊ฒ์ด๋ผ๋ ๊ฐ์ถ๋ ๊ฒ์ด๋ค.
- ๋ด๋ถ ๊ตฌํ์ ๋ณ๊ฒฝ์ผ๋ก ์ธํด ์ธ๋ถ์ ๊ฐ์ฒด๊ฐ ์ํฅ์ ๋ฐ์ผ๋ฉด ์๋๋ค.
- ์ค๊ณ์์ ๋ณํ๋๊ฒ์ด ๋ฌด์์ธ์ง ๊ณ ๋ คํ๊ณ ๋ณํ๋ ๊ฐ๋ ์ ์บก์ํ ํด์ผํ๋ค.