https://bobaejin.tistory.com/269
스프링 AOP와 🐌 용어 정리 🐌 그리고 공통 관심사 호출해보기
스프링 AOP스프링이 뭐냐 ➡ "IoC와 AOP를 지원하는 경량의 프레임워크"IoC : 제어의 역행AOP : 관점 지향 프로그래밍경량의 : POJO "유지보수가 용이한 코드" IoC ▶ 낮은 결합도AOP ▶ 높은 응집도용어
bobaejin.tistory.com
ㄴ 여기서 정리한 개념중 throwing / around를 해보려한다
throwing
package com.example.biz.common;
public class AfterThrowingAdvice {
public void printLog() {
System.out.println("[로그] 예외발생시 출력되는 로그");
}
}
AfterThrowingAdvice.java 추가한다
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.example.biz" />
<bean class="com.example.biz.common.LogAdvice" id="la" />
<bean class="com.example.biz.common.PlusLogAdvice" id="pla" />
<bean class="com.example.biz.common.AfterThrowingAdvice" id="ata" />
<aop:config>
<aop:pointcut expression="execution(* com.example.biz..*Impl.*(..))" id="aPointcut" />
<aop:pointcut expression="execution(* com.example.biz..*Impl.get*(..))" id="bPointcut" />
<aop:aspect ref="la">
<aop:before method="printLog" pointcut-ref="aPointcut"/>
</aop:aspect>
<aop:aspect ref="pla">
<aop:before method="printLog" pointcut-ref="bPointcut"/>
</aop:aspect>
<aop:aspect ref="ata">
<aop:after-throwing method="printLog" pointcut-ref="aPointcut"/>
</aop:aspect>
</aop:config>
</beans>
pom.xml에도 AfterThrowingAdvice 설정을 추가한다
예외가 발생시 수행되는 Advice 해봤다.

이런식으로 출력이 됐다
around
package com.example.biz.common;
import org.aspectj.lang.ProceedingJoinPoint;
public class AroundAdvice {
public Object around(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("비즈니스 메서드 수행 전");
Object returnObj = pjp.proceed();
System.out.println("비즈니스 메서드 수행 후");
return returnObj;
}
}
AroundAdvice.java를 추가한다
around는 전,후를 다 다룰 수 있다
인터셉트처럼 중간에 공을 뺏어오는 느낌으로 구현된 아이이다.
그래서 현재 잘 수행되고 있는 조인포인트가 있고 == Object returnObj = pjp.proceed();
를 중단에 탈취하는 느낌이다
PJP(ProceedingJoinPoint)는 잘 실행되고 있는 포인트라는 뜻이다
<bean class="com.example.biz.common.AroundAdvice" id="aa" />
<aop:aspect ref="aa">
<aop:around method="around" pointcut-ref="bPointcut"/>
</aop:aspect>
applicationContext.xml에 위의 코드를 추가한다
'🍃 Spring' 카테고리의 다른 글
| AOP 바인드 변수 완전정리 (0) | 2026.01.26 |
|---|---|
| 애스팩트 설정 : after / returning (0) | 2026.01.24 |
| 스프링 AOP와 🐌 용어 정리 🐌 그리고 공통 관심사 호출해보기 (0) | 2026.01.23 |
| 로그인부터 Board 목록까지의 흐름과 관련된 질문과 답변 (0) | 2026.01.22 |
| Controller에 Service가 생기면서 시작되는 2-Layer 아키텍처 정리 (0) | 2026.01.19 |