ํต์ฌ ๊ธฐ๋ฅ๊ณผ ๋ถ๊ฐ ๊ธฐ๋ฅ
์ ํ๋ฆฌ์ผ์ด์ ๋ก์ง์ ํฌ๊ฒ ํต์ฌ ๊ธฐ๋ฅ๊ณผ ๋ถ๊ฐ ๊ธฐ๋ฅ์ผ๋ก ๋๋ ์ ์๋ค
ํต์ฌ ๊ธฐ๋ฅ
: ํด๋น ๊ฐ์ฒด๊ฐ ์ ๊ณตํ๋ ๊ณ ์ ์ ๊ธฐ๋ฅ- ์๋ฅผ ๋ค์ด BookPrinter์ ํต์ฌ ๊ธฐ๋ฅ์ Print ๋ก์ง
๋ถ๊ฐ ๊ธฐ๋ฅ
: ํต์ฌ ๊ธฐ๋ฅ์ ๋ณด์กฐํ๊ธฐ ์ํด ์ ๊ณต๋๋ ๊ธฐ๋ฅ- ์๋ฅผ ๋ค์ด ๋ก๊ทธ ์ถ์ ๋ก์ง, ์คํ ์๊ฐ ์ถ๋ ฅ ๋ฑ
๋ณดํต ๋ถ๊ฐ ๊ธฐ๋ฅ์ ์ฌ๋ฌ ํด๋์ค์ ๊ฑธ์ณ์ ํจ๊ป ์ฌ์ฉ๋๋ค.
- ์๋ฅผ ๋ค์ด ๋ชจ๋ ์ ํ๋ฆฌ์ผ์ด์
ํธ์ถ์ ๋ก๊น
ํด์ผ ํ๋ ์๊ตฌ์ฌํญ์ ์๊ฐํด๋ณด์
- ๋ก๊ทธ ์ถ์ ๋ก์ง ๊ฐ์ ๋ถ๊ฐ ๊ธฐ๋ฅ์ ํก๋จ ๊ด์ฌ์ฌ(cross-cutting concerns)๊ฐ ๋๋ค.
- ์ฝ๊ฒ ๋งํด ํ๋์ ๋ถ๊ฐ ๊ธฐ๋ฅ์ด ์ฌ๋ฌ ๊ณณ์ ๋์ผํ๊ฒ ์ฌ์ฉ๋๋ค๋ ๋ป์ด๋ค.
ํ์ง๋ง ๋ถ๊ฐ ๊ธฐ๋ฅ์ ์ ์ฉํ ๋ ์๊ธฐ๋ ๋ฌธ์ ์ ์ด ์๋ค
- ๋ถ๊ฐ ๊ธฐ๋ฅ์ ์ ์ฉํ ๋ ์์ฃผ ๋ง์ ๋ฐ๋ณต์ด ํ์ํ๋ค.
- ๋ถ๊ฐ ๊ธฐ๋ฅ์ด ์ฌ๋ฌ ๊ณณ์ ํผ์ ธ์ ์ค๋ณต ์ฝ๋๋ฅผ ๋ง๋ค์ด๋ธ๋ค.
- ๋ถ๊ฐ ๊ธฐ๋ฅ์ ๋ณ๊ฒฝํ ๋ ์ค๋ณต ๋๋ฌธ์ ๋ง์ ์์ ์ด ํ์ํ๋ค
- ๋ถ๊ฐ ๊ธฐ๋ฅ์ ์ ์ฉ ๋์์ ๋ณ๊ฒฝํ ๋ ๋ง์ ์์ ์ด ํ์ํ๋ค.
๋ถ๊ฐ ๊ธฐ๋ฅ ๋์ ์ ๋ฌธ์ ์ ์ ํด๊ฒฐํ๋ ๋ฐฉ๋ฒ
ํต์ฌ ๊ธฐ๋ฅ๊ณผ ๋ถ๊ฐ ๊ธฐ๋ฅ์ ๋ถ๋ฆฌ
- ๋ถ๊ฐ ๊ธฐ๋ฅ์ ํต์ฌ ๊ธฐ๋ฅ์์ ๋ถ๋ฆฌํ๊ณ ํ ๊ณณ์์ ๊ด๋ฆฌํ๋๋ก ํ๋ค.
- ๋ถ๊ฐ ๊ธฐ๋ฅ์ ์ด๋์ ์ ์ฉํ ์ง ์ ํํ๋ ๊ธฐ๋ฅ๋ ๋ง๋ค์๋ค
- ์ด๋ฌํ ๊ธฐ๋ฅ์ ํฉํด์ ํ๋์ ๋ชจ๋๋ก ๋ง๋ค์๋๋ค. ๋ฐ๋ก ๊ทธ๊ฒ aspect ์ ์คํํธ์ด๋ค.
- ๋ค์ ๋งํด, ๋ถ๊ฐ ๊ธฐ๋ฅ๊ณผ ํด๋น ๋ถ๊ฐ ๊ธฐ๋ฅ์ ์ด๋์ ์ ์ฉํ ์ง ์ ์ ํ ๊ฒ
๊ด์ ์งํฅ ํ๋ก๊ทธ๋๋ฐ AOP
์ ์คํฉํธ๋ ์ฐ๋ฆฌ๋ง๋ก ํด์ํ๋ฉด ๊ด์ ์ด๋ผ๊ณ ํ๋ค. ์ด๋ฆ ๊ทธ๋๋ก ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ฐ๋ผ๋ณด๋ ๊ด์ ์ ํ๋ํ๋์ ๊ธฐ๋ฅ์์ ํก๋จ ๊ด์ฌ์ฌ ๊ด์ ์ผ๋ก ๋ฌ๋ฆฌ ๋ณด๋ ๊ฒ์ด๋ค. ์ด๋ ๊ฒ ์ ์คํํธ๋ฅผ ์ฌ์ฉํ ํ๋ก๊ทธ๋๋ฐ ๋ฐฉ์์ ๊ด์ ์งํฅ ํ๋ก๊ทธ๋๋ฐ์ด๋ผ๊ณ ํ๋ค.
์ฐธ๊ณ ๋ก, AOP๋ OOP๋ฅผ ๋์ฒดํ๊ธฐ ์ํ ๊ฒ์ด ์๋๋ผ ํก๋จ ๊ด์ฌ์ฌ๋ฅผ ๊น๋ํ๊ฒ ์ฒ๋ฆฌํ๊ธฐ ์ด๋ ค์ด OOP์ ๋ถ์กฑํ ๋ถ๋ถ์ ๋ณด์กฐํ๋ ๋ชฉ์ ์ผ๋ก ๊ฐ๋ฐ๋์๋ค.
์ฅ์
- ๋ถ๊ฐ ๊ธฐ๋ฅ๊ณผ ํต์ฌ ๊ธฐ๋ฅ์ ๋ถ๋ฆฌํ์ฌ ํต์ฌ ๋ก์ง์ ๊น๋ํ๊ฒ ์ ์งํ ์ ์๋ค
- ๋ถ๊ฐ ๊ธฐ๋ฅ ๋ณ๊ฒฝ์ด ํ์ ํ ๋ ๋ค๋ฅธ ๋ถ๋ถ์ ๋ณ๊ฒฝ ์์ด ๋ถ๊ฐ ๊ธฐ๋ฅ๋ง ๋ณ๊ฒฝํ๋ฉด ๋๋ค.
AOP ์ ์ฉ ๋ฐฉ์
1. ์ปดํ์ผ ์์
.java
์์ค ์ฝ๋๋ฅผ ์ปดํ์ผ๋ฌ๋ฅผ ์ฌ์ฉํด .class
๋ฅผ ๋ง๋๋ ์์ ์ ๋ถ๊ฐ ๊ธฐ๋ฅ ๋ก์ง์ ์ถ๊ฐํ ์ ์๋ค. ๋ถ๊ฐ ๊ธฐ๋ฅ ์ฝ๋๊ฐ ํต์ฌ ๊ธฐ๋ฅ์ด ์๋ ์ปดํ์ผ๋ ์ฝ๋ ์ฃผ๋ณ์ ์ค์ ๋ก ๋ถ์ด ๋ฒ๋ฆฐ๋ค๊ณ ์๊ฐํ๋ฉด ๋๋ค. ์ด์ฒ๋ผ ์๋ณธ ๋ก์ง์ ๋ถ๊ฐ ๊ธฐ๋ฅ ๋ก์ง์ด ์ถ๊ฐ๋๋ ๊ฒ์ weaving ์๋น์ด๋ผ๊ณ ํ๋ค.
๋จ์ : ์ปดํ์ผ ์์ ์ ๋ถ๊ฐ ๊ธฐ๋ฅ์ ์ ์ฉํ๋ ค๋ฉด ํน๋ณํ ์ปดํ์ผ๋ฌ๋ ํ์ํ๊ณ ๋ณต์กํ๋ค.
2. ํด๋์ค ๋ก๋ฉ ์์
์๋ฐ๋ฅผ ์คํํ๋ฉด ์๋ฐ ์ธ์ด๋ .class
ํ์ผ์ JVM ๋ด๋ถ ํด๋์ค ๋ก๋์ ๋ณด๊ดํ๋ค. ์ด๋ ์ค๊ฐ์์ .class
ํ์ผ์ ์กฐ์ํ ๋ค์ JVM์ ์ฌ๋ฆด ์ ์๋ค(์๋ฐ ์ธ์ด๋ JVM์ .class
ํ์ผ์ ์ ์ฅํ๊ธฐ ์ ์ ์กฐ์ํ ์ ์๋ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ค(java instrumentation) ์ฐธ๊ณ ๋ก ์ ๋ง์ ๋ชจ๋ํฐ๋ง ํด๋ค์ด ์ด ๋ฐฉ์์ ์ฌ์ฉํ๋ค. ์ด ์์ ์ ์ ์คํํธ๋ฅผ ์ ์ฉํ๋ ๊ฒ์ ๋ก๋ ํ์ ์๋น์ด๋ผ๊ณ ํ๋ค
๋จ์ : ๋ก๋ ํ์ ์๋น์ ์๋ฐ๋ฅผ ์คํํ ๋ ํน๋ณํ ์ต์ ( java -javaagent )์ ํตํด ํด๋์ค ๋ก๋ ์กฐ์๊ธฐ๋ฅผ ์ง์ ํด์ผ ํ๋๋ฐ, ์ด ๋ถ๋ถ์ด ๋ฒ๊ฑฐ๋กญ๊ณ ์ด์ํ๊ธฐ ์ด๋ ต๋ค
3. ๋ฐํ์ ์์ (ํ๋ก์)
๋ฐํ์ ์์ ์ ์ปดํ์ผ๋ ๋ค ๋๋๊ณ , ํด๋์ค ๋ก๋์ ํด๋์ค๋ ๋ค ์ฌ๋ผ๊ฐ์ ์ด๋ฏธ ์๋ฐ๊ฐ ์คํ๋๊ณ ๋ ๋ค์์ ๋งํ๋ค. ์๋ฐ์ main ๋ฉ์๋๊ฐ ์ด๋ฏธ ์คํ๋ ๋ค์์ด๋ค. ๋ฐ๋ผ์ ์๋ฐ ์ธ์ด๊ฐ ์ ๊ณตํ๋ ๋ฒ์ ์์์ ๋ถ๊ฐ ๊ธฐ๋ฅ์ ์ ์ฉํด์ผ ํ๋ค. ์ด ๋ฐฉ์์ ์ค์ ๋์ ์ฝ๋๋ ๊ทธ๋๋ก ์ ์ง๋๊ณ ํ๋ก์๋ฅผ ํตํ ๋ถ๊ฐ ๊ธฐ๋ฅ์ด ์ ์ฉ๋๋ ๋ฐฉ์์ด๋ค. ์คํ๋ง AOP๋ ์ด ๋ฐฉ์์ ์ฌ์ฉํ๋ค.
- ํ๋ก์ ๋ฐฉ์์ ์ฌ์ฉํ๋ ์คํ๋ง AOP๋ ๋ฉ์๋ ์คํ ์ง์ ์๋ง AOP๋ฅผ ์ ์ฉํ ์ ์๋ค
- ํ๋ก์๋ ๋ฉ์๋ ์ค๋ฒ๋ผ์ด๋ฉ ๊ฐ๋ ์ผ๋ก ๋์ํ๋ค. ๋ฐ๋ผ์ ์์ฑ์๋ static ๋ฉ์๋, ํ๋ ๊ฐ ์ ๊ทผ์๋ ํ๋ก์ ๊ฐ๋ ์ด ์ ์ฉ๋ ์ ์๋ค.
- ํ๋ก์๋ฅผ ์ฌ์ฉํ๋ ์คํ๋ง AOP์ ์กฐ์ธ ํฌ์ธํธ๋ ๋ฉ์๋ ์คํ์ผ๋ก ์ ํ๋๋ค.
- ํ๋ก์ ๋ฐฉ์์ ์ฌ์ฉํ๋ ์คํ๋ง AOP๋ ์คํ๋ง ์ปจํ ์ด๋๊ฐ ๊ด๋ฆฌํ ์ ์๋ ์คํ๋ง ๋น์๋ง AOP๋ฅผ ์ ์ฉํ ์ ์๋ค
์ฐธ๊ณ : ์กฐ์ธ ํฌ์ธํธ(join point)๋ AOP๋ฅผ ์ ์ฉํ ์ ์๋ ์ง์ ์ ๋งํ๋ค. ์กฐ์ธ ํฌ์ธํธ์๋ ์์ฑ์, ํ๋ ๊ฐ ์ ๊ทผ, static ๋ฉ์๋ ์ ๊ทผ, ๋ฉ์๋ ์คํ ๋ฑ ๋ค์ํ ์์น๊ฐ ์กด์ฌํ๋ค.
๋ด๋ถ ํธ์ถ์ ๋ฌธ์ ์
์คํ๋ง์ ํ๋ก์ ๋ฐฉ์์ AOP๋ฅผ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์, AOP๋ฅผ ์ ์ฉํ๋ ค๋ฉด ํญ์ ํ๋ก์๋ฅผ ํตํด์ ๋์ ๊ฐ์ฒด๋ฅผ ํธ์ถํด์ผ ํ๋ค. ์ด๋ฐ ๊ณผ์ ์ ๊ฑฐ์ณ์ผ ํ๋ก์์์ ์ด๋๋ฐ์ด์ค๋ฅผ ํธ์ถํ๊ณ ์ดํ์ ๋์ ๊ฐ์ฒด๋ฅผ ํธ์ถํ๋ค. ๋ง์ฝ ํ๋ก์๋ฅผ ๊ฑฐ์น์ง ์๋๋ค๋ฉด AOP๋ ์ ์ฉ๋์ง ์๋๋ค.
AOP๋ฅผ ์ ์ฉํ๋ฉด ์คํ๋ง์ ๋์ ๊ฐ์ฒด ๋์ ํ๋ก์๋ฅผ ์คํ๋ง ๋น์ผ๋ก ๋ฑ๋กํ๋ค. ๋ฐ๋ผ์ ์คํ๋ง์ ์์กด๊ด๊ณ ์ฃผ์
์์ ํญ์ ํ๋ก์ ๊ฐ์ฒด๋ฅผ ์ฃผ์
ํ๋ค.
ํ์ง๋ง ๋์ ๊ฐ์ฒด์ ๋ด๋ถ์์ ๋ฉ์๋ ํธ์ถ์ด ๋ฐ์ํ ๋ ํ๋ก์๋ฅผ ๊ฑฐ์น์ง ์๊ณ ๋์ ๊ฐ์ฒด๋ฅผ ์ง์ ํธ์ถํ๋ ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ค.
๋ด๋ถ ํธ์ถ ๋ฌธ์ ํด๊ฒฐ ๋ฐฉ๋ฒ
1. ์๊ธฐ ์์ ์ฃผ์
/**
* ์ฐธ๊ณ : ์์ฑ์ ์ฃผ์
์ ์ํ ์ฌ์ดํด์ ๋ง๋ค๊ธฐ ๋๋ฌธ์ ์คํจํ๋ค.
*/
@Slf4j
@Component
public class CallServiceV1 {
private CallServiceV1 callServiceV1;
@Autowired
public void setCallServiceV1(CallServiceV1 callServiceV1) {
this.callServiceV1 = callServiceV1;
}
public void external() {
log.info("call external");
callServiceV1.internal(); //์ธ๋ถ ๋ฉ์๋ ํธ์ถ
}
public void internal() {
log.info("call internal");
}
}
์๊ธฐ ์์ ์ ์์ ์(setter)๋ฅผ ํตํด ์ฃผ์ ๋ฐ๋ ๋ฐฉ๋ฒ์ด๋ค. ์คํ๋ง์์ AOP๊ฐ ์ ์ฉ๋ ๋์์ ์์กด๊ด๊ณ ์ฃผ์ ๋ฐ์ผ๋ฉด ์ฃผ์ ๋ฐ์ ๋์์ ์ค์ ์์ ์ด ์๋๋ผ ํ๋ก์ ๊ฐ์ฒด์ด๋ค.
์ฃผ์ : ์คํ๋ง ๋ถํธ 2.6๋ถํฐ๋ ์ํ ์ฐธ์กฐ๋ฅผ ๊ธฐ๋ณธ์ ์ผ๋ก ๊ธ์งํ๋๋ก ์ ์ฑ ์ด ๋ณ๊ฒฝ ๋์ด ์์ ์ฝ๋๋ ๋ณ๋์ ์ค์ ์ ์ถ๊ฐํ์ง ์์ผ๋ฉด ์ ์ ์คํ๋์ง ์๋๋ค.
2. ์ง์ฐ ์กฐํ
ObjectProvider
๋ฅผ ์ฌ์ฉํด์ ๊ฐ์ฒด๋ฅผ ์คํ๋ง ์ปจํ
์ด๋์์ ์กฐํํ๋ ๊ฒ์ ์คํ๋ง ๋น ์์ฑ ์์ ์ด ์๋๋ผ ์ค์ ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ๋ ์์ ์ผ๋ก ์ง์ฐํ ์ ์๋ค.
3. ๊ตฌ์กฐ ๋ณ๊ฒฝ
/**
* ๊ตฌ์กฐ๋ฅผ ๋ณ๊ฒฝ(๋ถ๋ฆฌ)
*/
@Slf4j
@Component
@RequiredArgsConstructor
public class CallServiceV3 {
private final InternalService internalService;
public void external() {
log.info("call external");
internalService.internal(); //์ธ๋ถ ๋ฉ์๋ ํธ์ถ
}
}
@Slf4j
@Component
public class InternalService {
public void internal() {
log.info("call internal");
}
}
๊ตฌ์กฐ๋ฅผ ๋ณ๊ฒฝํ์ฌ ๋ด๋ถ ํธ์ถ ์์ฒด๋ฅผ ์ญ์ ํ๋ค.
์ถ์ฒ
https://inf.run/fnKP