Spring AOP
Spring AOP는 프록시다. 따라서 new 연산자를 통해 생성된 곳에는 AOP 적용이 안됨. 의존성 주입을 해서 해결해야한다.
실제로 이를 모르고 몇시간동안 왜 경로를 올바르게 설정했는데 의문을 품으며 몇시간을 삽질한적이 있다.
--- AOP 부분 ---
@Slf4j
@Aspect
@EnableAspectJAutoProxy(proxyTargetClass = false)
public class SimpleLogAop {
// 주요 기능에만 적용
// @Pointcut("kakao.getCI.aop.SimpleLogAop.cut()")
// @Pointcut("execution(* kakao.getCI.com.shinhan.security.imple.AopTest.test1(..))")
@Pointcut("execution(* kakao.getCI.com.shinhan.security.imple.*..*(..))")
public void cut() {}
// pointcut에 의해 필터링된 경로로 들어오는 경우 메서드 호출 전에 적용
@Before(value = "cut()")
public void beforeLog(JoinPoint joinPoint){
Method method = getMethod(joinPoint);
log.info("===== method {} start=====", method.getName());
System.out.println(method.getName());
Object[] args = joinPoint.getArgs();
if(args.length == 0)
{
log.info("no parameter");
return;
}
for (Object arg : args) {
log.info("parameter type = {}", args.getClass().getSimpleName());
log.info("parameter value = {}", arg);
}
}
// pointcut 통과후 메서드 리턴에
@AfterReturning(value = "cut()")
public void afterReturnLog(JoinPoint joinPoint){
Method method = getMethod(joinPoint);
log.info("===== method {} finished=====", method.getName());
}
public Method getMethod(JoinPoint joinPoint) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
return signature.getMethod();
}
}
--- Test 실패 case ---
@Test
void reg_init_test() throws SASimpleAuthException {
String reqJson = "{\"tag\":\"3E01\",\"tmppub\":\"30820122300D06092A864886F70D01010105000382010F003082010A0282010100997A4DE16E426664DB117B7EEBAD0F17BF1C317EEE8A4D1D441C60CD26598B03B3BCA3277BDCB0FDC26D3541575D14510103D7A127829A2CF2D2FAC6F61E1AD4D4685945C3AFF3F70B4261CBA77D07A60CB52217A25A1E7F412EBA83D44B4D5F5EC84D7B6AB438814A767637E2FBAFB73EE6442D85AF15A38DE59324ECB65FE58C864AA5B367DAD43C739C8B6562AE013406CF1985DD065FF5BB0D9C23CF118902ED7CB418718BA8B839340583A6BF9058AE04FDFD22AA2E265B5A83EBCB0397EB328D4A4FCC7F29AAAE528C3D18AE71475D3E574E3B53ABCD2747098EE2291F235C578788B72715768F2A6371C495693F543F15DDB2F518161BB7801307D0FD0203010001\",\"appid\":\"TEST_ALIAS\"}";
System.out.println("reqJson = " + reqJson);
// HttpSession session = new MockHttpSession();
// session.setAttribute(SAConst.TAG_CUSNO, CUSNO);
String serverResult = new SimpleAuthTask().registInit("3E01", reqJson, session);
}
AOP에 대해 정확한 이해가 없이 시도만 하다가 계속 실패하였다.
그러다 갓영한님의 수업을 듣던중 Spring AOP는 프록시 방식이라는 것을 알게되었고..
@Autowired를 적용시킨후 정상적으로 Pointcut이 적용되게 할 수 있었다.
@Autowired
SimpleAuthTask task;
@Test
void reg_init_test() throws SASimpleAuthException {
String reqJson = "{\"tag\":\"3E01\",\"tmppub\":\"30820122300D06092A864886F70D01010105000382010F003082010A0282010100997A4DE16E426664DB117B7EEBAD0F17BF1C317EEE8A4D1D441C60CD26598B03B3BCA3277BDCB0FDC26D3541575D14510103D7A127829A2CF2D2FAC6F61E1AD4D4685945C3AFF3F70B4261CBA77D07A60CB52217A25A1E7F412EBA83D44B4D5F5EC84D7B6AB438814A767637E2FBAFB73EE6442D85AF15A38DE59324ECB65FE58C864AA5B367DAD43C739C8B6562AE013406CF1985DD065FF5BB0D9C23CF118902ED7CB418718BA8B839340583A6BF9058AE04FDFD22AA2E265B5A83EBCB0397EB328D4A4FCC7F29AAAE528C3D18AE71475D3E574E3B53ABCD2747098EE2291F235C578788B72715768F2A6371C495693F543F15DDB2F518161BB7801307D0FD0203010001\",\"appid\":\"TEST_ALIAS\"}";
System.out.println("reqJson = " + reqJson);
// HttpSession session = new MockHttpSession();
// session.setAttribute(SAConst.TAG_CUSNO, CUSNO);
String serverResult = task.registInit("3E01", reqJson, session);
}
AOP 용어