AOP(Aspect Oriented Programming)
- ๊ด์ ์งํฅ ํ๋ก๊ทธ๋๋ฐ
- Core Concerns : ๋ฉ์ธ ๋ก์ง
- Cross-Cutting Concerns : ๋ถ๊ฐ์ ์ธ ๋ก์ง(๋ก๊ทธ์ธ ๊ฒ์ฌ, ๋ณด์, ๋ก๊น ….)
- ๋ฐ๋ณต๋๋ ๋ถ๊ฐ ๋ก์ง์ ์ฌ์ฌ์ฉํ ์ ์๋ ๋ฐฉ๋ฒ(๋ถ๊ฐ๋ก์ง ์ค๋ณต๋ฐฉ์ง)
- OOP(Object Oriented Progrramming) ๋ฐฉ์์ผ๋ก๋ ์๋จ
- AOP ๋ฐฉ์์ ์ฌ์ฉํ์ฌ OOP๋ฅผ ๋ณด์ํ ์ ์์
- Adivce : ๋ถ๊ฐ์ ์ธ ๋ก์ง์ ๋ฉ์๋๋ก ์ ์ธํ ๊ฒ
- Pointcut : ๋ถ๊ฐ๋ก์ง์ธ ์คํ๋ ๋ฉ์ธ ๋ก์ง์ ์์ ํ ๊ฒฝ๋ก ํํ
- Aspect : ๋ถ๊ฐ๋ก์ง์ ๋ฉ์๋๋ก ์ ์ธํ ํด๋์ค
- JoinPoint : ๋ถ๊ฐ๋ก์ง์ ์ด์ด์ ์คํ๋ ์ฃผ ๋ก์ง์ ๋ฉ์๋ ์ ๋ณด
- @Before, @After, @Around, @AfterReturning, @AfterThrowing : Adviceํ์
- @Aspect : ๋ถ๊ฐ๋ก์ง์ ์ ์ธํ ํด๋์ค
- @Pointcut : ์ด๋ค์ง์ ์์ ๋ถ๊ฐ๋ก์ง ๋ฉ์๋๊ฐ ๋์๊ฐ๊ฒ ํ ๊ฒ์ธ๊ฐ
์ด ๋ฐ๋ณต๋๋, ๋ถ๊ฐ์ ์ธ ๋ก์ง์ ํด๋์ค๋ก ์ ์ธํด์ ์ฌ์ฌ์ฉ. ์ค๋ณต์ ํผํ ์ ์๋ค.
(+ ๋ฉ์ธ/๋ถ๊ฐ ๋ก์ง ๋ถ๋ฆฌ๋ก ๊ด๋ฆฌ๋ ํธํด์ง)
โ
์ฌ๋ฌ ๋ฉ์๋์ ์ค๋ณต์ ์ผ๋ก ๋ค์ด๊ฐ๋ ๋ก์ง๋ค์ ๋ฐ๋ก ๋นผ์ ๋ฉ์๋๋ฅผ ํธ์ถํ๋ ๋ฐฉ๋ฒ์ ๋ถ๊ฐ๋ก์ง์ผ๋ก ๋ฏธ๋ฆฌ ์ ์ธํด๋ฌ์ ํธํ๊ฒ ํธ์ถํ ์ ์๋ค.
๋ถ๊ฐ๋ก์ง ํ์ฉ์ ์
๋น ๋ฐ์ดํฐ๋ฅผ ์์ฑํ๊ธฐ ์ํด
logging (์ด์ฉ์๊ฐ ํด๋ฆญํ๋ ๋ชจ๋ ์์ฒญ, ์ ์ํ ์๊ฐ ๋ฑ์ ์ ์ฅ)
๋จผ์ dependency๋ฅผ ์ค์ ํด์ค์ผํ๋ค.
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<scope>compile</scope>
</dependency>
AOP ํ์ฉ ๋ฐฉ์
MathService.java
1
2
3
4
5
6
7
8
|
import org.springframework.stereotype.Service;
@Service
public class MathService {
public int add(int a, int b ) {
return a+b;
}
}
|
cs |
AopTestController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/aoptest")
public class AopTestController {
@Autowired
private MathService svc;
@GetMapping("/add")
@ResponseBody
public int result() {
return svc.add(3, 2);
}
}
|
cs |
MathAOP.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.Configuration;
import lombok.extern.slf4j.Slf4j;
@Aspect
@Configuration
@Slf4j
public class MathAOP { //MathService๊ฐ ์คํ์ ์ ํ๋ฉด์ ํ์
@Pointcut("execution(* com.ezen.spring.web.aop.MathService.*(..))")
private void mathfunc() {
}
@Before("mathfunc()")
public void beforeLog(JoinPoint jp) {
String methodName = jp.getSignature().getName();
log.info(methodName+"ํธ์ถ๋จ");
}
}
|
cs |
์คํ๊ฒฐ๊ณผ :
๋๊ธ