[Spring] AOP(03) - PointCut


AOP - PointCut

JoinPoint๋ฅผ ์ง€์ •ํ•˜๋Š” ํ‘œํ˜„์‹

๋ฐ˜๋ณต๋˜๋Š” execution() ๋ฉ”์„œ๋“œ๋ฅผ PointCut์„ ํ†ตํ•ด ์ง€์ •ํ•˜์—ฌ ํŽธ๋ฆฌํ•˜๊ฒŒ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

PointCut ์ƒ์„ฑ ๋ฐฉ๋ฒ•

1. ์–ด๋…ธํ…Œ์ด์…˜ ์‚ฌ์šฉ

public class CommonPointCut {
    @Pointcut("execution(* usingComputer(..))")
    public void myPointCut() {}
}
  • ์•ž์„œ ์ž‘์„ฑํ•œ ์ฝ”๋“œ์—์„œ ๋ฐ˜๋ณตํ•ด์„œ ์‚ฌ์šฉ๋˜๋Š” "execution(* usingComputer(..))" ์„ myPointCut() ๋ฉ”์„œ๋“œ๋ฅผ ๋Œ€์‹ ํ•˜์—ฌ ์‚ฌ์šฉํ•œ๋‹ค.


  • ๊ธฐ์กด ์ฝ”๋“œ
      @Before("execution(public int aop02.*.usingComputer(String))")
      public void bootingAndLogin(JoinPoint jp) {
          String pwd = (String) jp.getArgs()[0];
          System.out.println("์ปดํ“จํ„ฐ ๋ถ€ํŒ… ์‹œ์ž‘. " + pwd + " ์ž…๋ ฅ ๋กœ๊ทธ์ธ");
      }
    
  • Pointcut์„ ํ™œ์šฉํ•œ ์ฝ”๋“œ
      @Before("CommonPointCut.myPointCut()")
      public void bootingAndLogin(JoinPoint jp) {
          String pwd = (String) jp.getArgs()[0];
          System.out.println("์ปดํ“จํ„ฐ ๋ถ€ํŒ… ์‹œ์ž‘. " + pwd + " ์ž…๋ ฅ ๋กœ๊ทธ์ธ");
      }
    


2. xml ์‚ฌ์šฉ

<aop:config>
    <aop:pointcut expression="execution(* usingComputer(..))" id="myPoinCut" />
    <aop:aspect ref="myAspect">
        <aop:before method="bootingAndLogin" pointcut-ref="myPoinCut" />
    </aop:aspect>
</aop:config>
  • myPointCut์ด๋ผ๋Š” id๋กœ PointCut์„ ์ƒ์„ฑํ•œ๋‹ค.
  • PointCut์„ ํ™œ์šฉํ•˜๋Š” aspect๋Š” pointcut-ref๋ฅผ ํ†ตํ•ด ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.





ยฉ 2022. by Yejin Ha

Powered by JihyunRyu