[Spring] AOP(03) - PointCut
in Tech-Stack on Spring
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๋ฅผ ํตํด ์ค์ ํ ์ ์๋ค.
