Framework/Spring Framework

(23.01.17.)Spring ํ”„๋ ˆ์ž„์›Œํฌ : AOP(Aspect Oriented Programming) ๊ด€๊ณ„์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ

ํ”„๋กœ๊ทธ๋ž˜๋จธ ์˜ค์›” 2023. 1. 18.

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(32);
    }
}
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

 

 

 

์‹คํ–‰๊ฒฐ๊ณผ : 

 

 

 

 

๋Œ“๊ธ€