[ Spring ] AOP ( Aspect Oriented Programming ) - excepObj를 통하여 반환된 예외 확인하기
2022. 4. 7. 15:01ㆍSpring
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에서 실행시키고 값을 확인 하면 끝!
'Spring' 카테고리의 다른 글
[ Spring ] AOP ( Aspect Oriented Programming ) - 핵심 관심 호출하기 ( 사용된 인자와 메서드명, 반환 값 확인 하기 ) (0) | 2022.04.07 |
---|---|
[ Spring ] AOP ( Aspect Oriented Programming ) - 관점 지향 프로그래밍 | 로그 작성하기 (0) | 2022.04.04 |
[ Spring ] IOC (Inversion of Control) - 디자인 패턴(Factory) 활용 (0) | 2022.04.01 |
[ Spring ] IOC (Inversion of Control) - 인터페이스 활용 (0) | 2022.04.01 |
[ Spring ] 스프링 프레임워크의 특징 (0) | 2022.03.29 |