Spring
[ Spring ] AOP ( Aspect Oriented Programming ) - excepObj를 통하여 반환된 예외 확인하기
dauneee
2022. 4. 7. 15:01
AfterThrowingAdvice |
Target 메서드가 수행도중 예외가 발생했을 때 되면 실행되는 Advice를 의미한다. |
1. Exception excepObj를 추가한다.
package com.test.app.common;
import org.aspectj.lang.JoinPoint;
public class AfterThrowingAdvice { // ▼
public void ataLog(JoinPoint jp, Exception excepObj) {
// 반환받을 예외 객체
String methodName = jp.getSignature().getName();
System.out.println("호출된 핵심관심: " + methodName);
}
}
2. 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: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.test.app"/>
<bean id="log" class="com.test.app.common.LogAdvice"/><!-- 사용될 클래스 new처리 -->
<aop:config>
<aop:pointcut expression="execution(* com.test.app..*Impl.*(..))" id="aPointCut"/>
<aop:aspect ref="log">
<aop:after-throwing method="printlog1" pointcut-ref="aPointCut" throwing="excepObj"/><!-- excepObj -->
</aop:aspect>
</aop:config>
</beans>
3. BoardServiceImpl에서 일부러 예외를 발생시키는 코드를 작성해준다.
public class BoardServiceImpl implements BoardService {
private BoardDAO boardDAO;
@Override
public void insertBoard(BoardVO vo) {
if(vo.getBid()==0) {
throw new IllegalArgumentException("내가 만든 예외!");
}
boardDAO.insertBoard(vo);
}
4. client에서 실행시키고 값을 확인 하면 끝!