반응형
package aopEx02;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class Aop {
@Before("within(패키지명.클래스)")
public void beforeAdvice() {
System.out.println("핵심 로직 실행 전에 호출되는 공통 모듈");
}
@After("within(패키지명.클래스)")
public void afterAdvice() {
System.out.println("핵심 로직 실행 후에 호출되는 공통 모듈");
}
@AfterThrowing("within(패키지명.클래스)")
public void afterThrowingAdvice() {
System.out.println("핵심 로직이 에러가 발생하여 종료 되었습니다");
}
@AfterReturning("within(패키지명.클래스)")
public void afterReturningAdvice() {
System.out.println("핵심로직이 정상 종료되었습니다");
}
@Around("within(패키지명.클래스)")
public Object aroundAdvice(ProceedingJoinPoint joinpoint) throws Throwable {
System.out.println("핵심로직 실행전");
try {
System.out.println("핵심로직 실행전2");
Object obj = joinpoint.proceed(); //핵심로직 실행
return obj;
}finally {
System.out.println("핵심로직 실행후");
}
}
}
<?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: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-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
<context:component-scan base-package="aopEx02" />
<aop:aspectj-autoproxy /> <!-- aop 어노테이션을 실행함 -->
</beans>
반응형
'Spring' 카테고리의 다른 글
[Spring] @Controller (0) | 2024.11.25 |
---|---|
[Spring] 스프링 MVC 프로젝트 (0) | 2024.11.24 |
[Spring] joinpoint (0) | 2024.11.22 |
[Spring] AOP (0) | 2024.11.21 |
[Spring] 어노테이션(@) (0) | 2024.11.20 |