develog

Spring AOP 본문

Dev/Java

Spring AOP

냐옴 2012. 11. 24. 23:40

beans.xml

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

<?xml version="1.0" encoding="UTF-8" ?>

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd

           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd

           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">


<bean id="loggingAspect" class="test.aop.LoggingAspect" />

<aop:config>

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

<aop:aspect ref="loggingAspect">

<aop:before pointcut-ref="loggingPointcut" method="before" />

<aop:after-returning pointcut-ref="loggingPointcut" method="afterReturning" returning="ret" />

<aop:after-throwing pointcut-ref="loggingPointcut" method="afterThrowing" throwing="ex" />

<aop:after pointcut-ref="loggingPointcut" method="after" />

<aop:around pointcut-ref="loggingPointcut" method="around" />

</aop:aspect>

</aop:config>


</beans>




LoggingAspect.java

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

public class LoggingAspect {

private static Logger logger = Logger.getLogger(LoggingAspect.class);

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

}

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

}

public void before(JoinPoint joinPoint) {

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

String args = getArgs(joinPoint);

if (args != null && !"".equals(args)) {

logger.info("[LoggingAdvice] " + "args : " + args);

}

}

public void afterReturning(JoinPoint joinPoint, Object ret) {

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

logger.info("[LoggingAdvice] " + "ret : " + ret);

}

public void afterThrowing(JoinPoint joinPoint, Throwable ex) {

logger.info("[LoggingAdvice] >> " + getFullName(joinPoint) + " (afterThrowing)");

logger.error("[LoggingAdvice] " + "ex : " + ex, ex);

}

public void after(JoinPoint joinPoint) {

logger.info("[LoggingAdvice] >> " + getFullName(joinPoint) + " (after)");

}

public Object around(ProceedingJoinPoint joinPoint) throws Throwable {

logger.info("[LoggingAdvice] >> " + getFullName(joinPoint) + " (around)");

Object ret = null;

System.out.println("[LoggingAdvice] " + "실행전");

try {

ret = joinPoint.proceed();

System.out.println("[LoggingAdvice] " + "실행후");

System.out.println("ret : " + ret);

} catch (Throwable e) {

System.out.println("[LoggingAdvice] " + "예외발생 : " + e);

throw e;

}

System.out.println("[LoggingAdvice] " + "실행종료");

return ret;

}

}


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

SQL Developer 에러 조치  (0) 2012.11.27
try catch finally - Flow test  (0) 2012.11.25
[java] JSP  (0) 2012.11.14
[java] JSTL  (0) 2012.11.14
Primitive Variable  (0) 2012.11.12
Comments