<aop:aspectj-autoproxy>
AOP๋ฅผ ์ด๋ ธํ ์ด์ ์ผ๋ก ์ค์ ํ๋ ค๋ฉด, ๊ฐ์ฅ ๋จผ์ ์คํ๋ง ์ค์ ํ์ผ์ <aop:aspectj-autoproxy> ์๋ฆฌ๋จผํธ๋ฅผ ์ ์ธํด์ผ ํ๋ค. ์ด๊ฒ๋ง ์ค์ ํ๋ฉด ์คํ๋ง ์ปจํ ์ด๋๋ AOP ๊ด๋ จ ์ด๋ ธํ ์ด์ ๋ค์ ์ธ์ํ๊ณ ์ฉ๋์ ๋ง๊ฒ ์ฒ๋ฆฌํด์ค๋ค.
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
<context:component-scan base-package="com.springbook.biz" />
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
@Pointcut
package com.springbook.biz.common;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class PointcutCommon {
@Pointcut("execution(* com.springbook.biz..*Impl.*(..))")
public void allPointcut() {
}
@Pointcut("execution(* com.springbook.biz..*Impl.get*(..))")
public void getPointcut() {
}
}
ํฌ์ธํธ์ปท์ ์ ์ธํ๋ ์ด๋
ธํ
์ด์
์ด๋ค.
์ฌ๋ฌ ๊ฐ์ ํฌ์ธํธ์ปท์ ์ ์ธํ ์ ์๋๋ฐ, ์ฌ๋ฌ ํฌ์ธํธ์ปท์ ์๋ณํ๊ธฐ ์ํด ์ฐธ์กฐ ๋ฉ์๋๋ฅผ ์ด์ฉํ๋ค.
์ฐธ์กฐ ๋ฉ์๋๋ ๊ตฌํ ๋ก์ง์ด ์๋ ๋ฉ์๋์ด๋ค.
์ด๋ค ๊ธฐ๋ฅ์ฒ๋ฆฌ๋ฅผ ๋ชฉ์ ์ผ๋ก ํ์ง ์๊ณ ๋จ์ํ ํฌ์ธํธ์ปท์ ์๋ณํ๋ ์ด๋ฆ์ผ๋ก๋ง ์ฌ์ฉ๋๋ค.
ํฌ์ธํธ์ปท์ ์ธ๋ถ์ ๋ ๋ฆฝ๋ ํด๋์ค์ ๋ฐ๋ก ์ค์ ํ์ฌ ๋ฐ๋ณต ์ ์ธํ๋ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ์ ์๋ค.
์ด๋๋ฐ์ด์ค์์ ํฌ์ธํธ์ปท์ ์ฐธ์กฐํ๋ ค๋ฉด "PointcutCommon.allPointcut()"์ ๊ฐ์ด ํด๋์ค์ด๋ฆ๊ณผ ์ฐธ์กฐ ๋ฉ์๋ ์ด๋ฆ ์ฐ๋ฉด ๋๋ค.
@Service
์ด๋๋ฐ์ด์ค ํด๋์ค๋ ๋ฐ๋์ @Service ์ด๋ ธํ ์ด์ ์ ์ฌ์ฉํ์ฌ ์ปดํฌ๋ํธ๊ฐ ๊ฒ์๋ ์ ์๋๋ก ํด์ผ ํ๋ค.
@Aspect
Aspect๋ Pointcut๊ณผ Advice์ ๊ฒฐํฉ์ด๋ค.
๋ฐ๋ผ์ @Aspect๊ฐ ์ค์ ๋ ์ ์คํฉํธ ๊ฐ์ฒด์๋ ๋ฐ๋์ ํฌ์ธํธ์ปท๊ณผ ์ด๋๋ฐ์ด์ค๋ฅผ ๊ฒฐํฉํ๋ ์ค์ ์ด ์์ด์ผ ํ๋ค.
์ด๋๋ฐ์ด์ค ์ค์
@Before
allPointCut()์ผ๋ก ์ง์ ํ ๋ฉ์๋๊ฐ ํธ์ถ๋ ๋, beforeLog() ๋ฉ์๋๊ฐ Before ํํ๋ก ๋์ํ๋๋ก ์ค์ ํ๋ค.
package com.springbook.biz.common;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Service;
@Service
@Aspect
public class BeforeAdvice {
@Before("PointcutCommon.allPointcut()")
public void beforeLog(JoinPoint jp){
String method = jp.getSignature().getName();
Object[] args = jp.getArgs();
System.out.println("[์ฌ์ ์ฒ๋ฆฌ] " + method +
"() ๋ฉ์๋ ARGS ์ ๋ณด : " + args[0].toString());
}
}
@AfterReturning
After Returning ์ด๋๋ฐ์ด์ค๋ ๋น์ฆ๋์ค ๋ฉ์๋ ์ํ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ์๋ด๊ธฐ ์ํด ๋ฐ์ธ๋ ๋ณ์๋ฅผ ์ง์ ํ๋ค.
@AfterReturning์ returning ์์ฑ์ ์ด์ฉํ์ฌ ๋ฐ์ธ๋ ๋ณ์๋ฅผ ์ง์ ํ๊ณ ์๋ค.
package com.springbook.biz.common;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Service;
import com.springbook.biz.user.UserVO;
@Service
@Aspect
public class AfterReturningAdvice {
@AfterReturning(pointcut="PointcutCommon.getPointcut()", returning="returnObj")
public void afterLog(JoinPoint jp, Object returnObj) {
String method = jp.getSignature().getName();
if (returnObj instanceof UserVO) {
UserVO user = (UserVO) returnObj;
if (user.getRole().equals("Admin")) {
System.out.println(user.getName() + " ๋ก๊ทธ์ธ(Admin)");
}
}
System.out.println("[์ฌํ ์ฒ๋ฆฌ] " + method + "() ๋ฉ์๋ ๋ฆฌํด๊ฐ : " + returnObj.toString());
}
}
@AfterThrowing
@AfterThrowing๋ throwing ์์ฑ์ ์ฌ์ฉํ์ฌ ๋ฐ์ธ๋ ๋ณ์๋ฅผ ์ง์ ํ๊ณ ์๋ค.
package com.springbook.biz.common;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Service;
@Service
@Aspect
public class AfterThrowingAdvice {
@AfterThrowing(pointcut="PointcutCommon.allPointcut()", throwing="exceptObj")
public void exceptionLog(JoinPoint jp, Exception exceptObj) {
String method = jp.getSignature().getName();
// System.out.println("[์์ธ ์ฒ๋ฆฌ] " + method + "() ๋ฉ์๋ ์ํ ์ค ๋ฐ์๋ ์์ธ ๋ฉ์์ง : "
// + exceptObj.getMessage());
System.out.println(method + "() ๋ฉ์๋ ์ํ ์ค ์์ธ ๋ฐ์!");
if (exceptObj instanceof IllegalArgumentException) {
System.out.println("๋ถ์ ํฉํ ๊ฐ์ด ์
๋ ฅ๋์์ต๋๋ค.");
} else if (exceptObj instanceof NumberFormatException) {
System.out.println("์ซ์ ํ์์ ๊ฐ์ด ์๋๋๋ค.");
} else if (exceptObj instanceof Exception) {
System.out.println("๋ฌธ์ ๊ฐ ๋ฐ์ํ์ต๋๋ค.");
}
}
}
@After
package com.springbook.biz.common;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Service;
@Service
@Aspect
public class AfterAdvice {
@After("PointcutCommon.allPointcut()")
public void finallyLog() {
System.out.println("[์ฌํ ์ฒ๋ฆฌ] ๋น์ฆ๋์ค ๋ก์ง ์ํ ํ ๋ฌด์กฐ๊ฑด ๋์");
}
}
@Around
Around ์ด๋๋ฐ์ด์ค๋ง ProceedingJoinPoint ๊ฐ์ฒด๋ฅผ ๋งค๊ฐ๋ณ์๋ก ๋ฐ์ proceed() ๋ฉ์๋๋ฅผ ์คํํ๋ค.
package com.springbook.biz.common;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Service;
import org.springframework.util.StopWatch;
@Service
@Aspect
public class AroundAdvice {
@Around("PointcutCommon.allPointcut()")
public Object aroundLog(ProceedingJoinPoint pjp) throws Throwable {
String method = pjp.getSignature().getName();
StopWatch stopWatch = new StopWatch();
stopWatch.start();
Object obj = pjp.proceed();
stopWatch.stop();
System.out.println(method + "() ๋ฉ์๋ ์ํ์ ๊ฑธ๋ฆฐ ์๊ฐ : " + stopWatch.getTotalTimeMillis() + "(ms)์ด");
return obj;
}
}
'Spring > Spring Quick Start' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Model 1 ์ํคํ ์ณ๋ก ๊ฒ์ํ ๊ฐ๋ฐ (JSP์ JavaBeans๋ง์ ์ฌ์ฉ) (0) | 2021.01.07 |
---|---|
ํธ๋์ญ์ (Transaction) ์ ์ฉ (0) | 2021.01.07 |
ํ๋กํผํฐ ํ์ผ์ ์ด์ฉํ Spring JDBC (0) | 2021.01.07 |
JointPoint ๋ฉ์๋ (0) | 2021.01.07 |
Advice ๋์ ์์ (0) | 2021.01.07 |
ํฌ์ธํธ์ปท ํํ์ (0) | 2021.01.07 |