develog

Spring AOP Aspect 설정 본문

Dev/Spring

Spring AOP Aspect 설정

냐옴 2013. 4. 22. 13:40

<bean id="LogAspect" class="com.my.LogAspect" />

<aop:config proxy-target-class="true">

    <aop:aspect ref="LogAspect">

        <aop:pointcut id="exceptionThrowing" expression="execution(* *..*Service.*(..))" />

        <aop:after-throwing pointcut-ref="exceptionThrowing" method="exceptionThrowing" throwing="ex" arg-names="ex" />

    </aop:aspect>

</aop:config>


-----------------------------------------------------------------------------------------------------------------


package com.my;


import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

import org.aspectj.lang.JoinPoint;

import org.aspectj.lang.Signature;



public class LogAspect {


private final Log logger = LogFactory.getLog(getClass());


public void before(JoinPoint joinPoint) {

if (logger.isInfoEnabled()) {

logger.info("[Aspect] before() => " + getFullName(joinPoint));

logger.info("[Aspect] args : " + getArgs(joinPoint));

}

}

public void afterReturning(JoinPoint joinPoint, Object ret) {

if (logger.isInfoEnabled()) {

logger.info("[Aspect] afterReturning() => " + getFullName(joinPoint));

logger.info("[Aspect] ret => " + ret);

}

}

public void afterThrowing(JoinPoint joinPoint, Exception ex) {

if (logger.isErrorEnabled()) {

logger.error("[Aspect] afterThrowing() => " + getFullName(joinPoint));

logger.error("[Aspect] ex => " + ex, ex);

}

}

public void exceptionThrowing(JoinPoint joinPoint, Exception ex) {

if (logger.isErrorEnabled()) {

logger.error("[Aspect] exceptionThrowing() => " + getFullName(joinPoint));

logger.error("[Aspect] ex => " + ex, ex);

}

}

private String getArgs(JoinPoint joinPoint) {

StringBuffer sb = new StringBuffer();

Object[] args = joinPoint.getArgs();

for (int i = 0; i < args.length; i++) {

sb.append("\n" + i + ". " + args[i]);

}

return sb.toString();

}

private String getFullName(JoinPoint joinPoint) {

Signature signature = joinPoint.getSignature();

StringBuffer sb = new StringBuffer();

sb.append(signature.getDeclaringTypeName());

sb.append(".");

sb.append(signature.getName());

return sb.toString();

}

 }


'Dev > Spring' 카테고리의 다른 글

Spring AOP Pointcut expression  (0) 2013.12.11
Spring AOP 설정  (0) 2013.12.11
Spring AOP LoggingAdvice  (0) 2013.11.29
REQUIRED, REQUIRES_NEW  (0) 2013.07.18
spring jar download  (0) 2013.07.09
Comments