๋ฐ์ํ
01. ๐Spring Bean Configuration File
[07-5_aopAnnotation.xml]
- bean , context , aop ๊ธฐ๋ฅ๊น์ง ๋ชจ๋ ์ด์ฉํ๋ฏ๋ก 3๊ฐ ๋ค ์ค์ ํด์ ์์ฑํด์ผํจ!!
<?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:aop="<http://www.springframework.org/schema/aop>"
xmlns:context="<http://www.springframework.org/schema/context>"
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.xsd>
<http://www.springframework.org/schema/aop> <http://www.springframework.org/schema/aop/spring-aop.xsd>">
<!-- ์ด์ ํด๋์ค์ @ ์ด๋
ธํ
์ด์
์ ์ฌ์ฉํด์ ์คํ๋ง ๋น์ผ๋ก ๋ฑ๋ก ๊ฐ๋ฅํด์ง! -->
<context:component-scan base-package="xyz.itwill07.aop"/>
<!-- aspectj-autoproxy : Spring Annotation๋ฅผ ์ด์ฉํ์ฌ AOP ๊ธฐ๋ฅ์ ์ ๊ณตํ๊ธฐ ์ํ ์๋ฆฌ๋จผํธ -->
<!-- => AOP ๊ด๋ จ Annotation์ ์ฌ์ฉํ์ฌ ํต์ฌ๊ด์ฌ์ฝ๋์ ํก๋จ๊ด์ฌ์ฝ๋๋ฅผ ์ฝ์
ํ์ฌ ์คํ๋๋๋ก ์ค์ -->
<aop:aspectj-autoproxy/>
</beans>
02. [ํต์ฌ๊ด์ฌ๋ชจ๋]
AopAnnotationBean.java
package xyz.itwill07.aop;
import org.springframework.stereotype.Component;
//Component ์ด๋
ธํ
์ด์
์ ์ํด Spring Bean์ผ๋ก ๋ฑ๋ก๋จ
@Component
public class AopAnnotationBean {
public void display1() {
System.out.println("### AopAnnotationBean ํด๋์ค์ display1() ๋ฉ์๋ ํธ์ถ ###");
}
public void display2() {
System.out.println("### AopAnnotationBean ํด๋์ค์ display2() ๋ฉ์๋ ํธ์ถ ###");
}
public void display3() {
System.out.println("### AopAnnotationBean ํด๋์ค์ display3() ๋ฉ์๋ ํธ์ถ ###");
throw new RuntimeException(); //after-throwing ์ด๋
ธํ
์ด์
์ ์ด์ฉํ๊ธฐ ์ํด ์ธ์์ ์์ธ๋ฐ์
}
}
03. [ํก๋จ๊ด์ฌ๋ชจ๋] : Advisorํด๋์ค
AopAnnotationAdvice.java
package xyz.itwill07.aop;
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.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
//Advisor ํด๋์ค : JoinPoint ๊น์ง ๋ชจ๋ ์ง์ ๋ ํด๋์ค
@Component
//@Aspect : ํต์ฌ๊ด์ฌ์ฝ๋์ ํก๋จ๊ด์ฌ์ฝ๋๋ฅผ ์ฝ์
ํ์ฌ ์คํํ๊ธฐ ์ํ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ ์ด๋
ธํ
์ด์
//=> Spring Bean Configuration File์ aspect ์๋ฆฌ๋จผํธ์ ์ ์ฌํ ๊ธฐ๋ฅ ์ ๊ณต
//=> Spring Bean Configuration File์ aspectj-autoproxy ์๋ฆฌ๋จผํธ ์ถ๊ฐํด์ผ ์ฌ์ฉ ๊ฐ๋ฅ
@Aspect
public class AopAnnotationAdvice {
public static final Logger logger=LoggerFactory.getLogger(AopAnnotationBean.class);
//1.
//@Pointcut : ํ๊ฒ๋ฉ์๋๋ฅผ ์ง์ ํ๊ธฐ ์ํ ์ด๋
ธํ
์ด์
//=> ๋ฉ์๋ ํธ์ถ์ ์ด์ฉํ์ฌ ํ๊ฒ๋ฉ์๋๋ฅผ ์ง์ ํ PointCut ํํ์์ ์ ๊ณตํ๊ธฐ ์ํด ์ฌ์ฉ
// => Spring Bean Configuration File์ pointcut ์๋ฆฌ๋จผํธ์ ์ ์ฌํ ๊ธฐ๋ฅ ์ ๊ณต
//value ์์ฑ : ํ๊ฒ๋ฉ์๋๋ฅผ ์ง์ ํ๊ธฐ ์ํ PointCut ํํ์์ ์์ฑ๊ฐ์ผ๋ก ์ค์
// => ๋ค๋ฅธ ์์ฑ์ด ์๋ ๊ฒฝ์ฐ ์์ฑ๊ฐ๋ง ์ค์ ๊ฐ๋ฅ
@Pointcut("within(xyz.itwill07.aop.AopAnnotationBean)")
public void aopPointCut() {}
//2.
//Before Advice ๋ฉ์๋
//@Before : ํต์ฌ๊ด์ฌ์ฝ๋ ์คํ ์ ์ ํก๋จ๊ด์ฌ์ฝ๋๋ฅผ ์คํํ๋ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ ์ด๋
ธํ
์ด์
// => Spring Bean Configuration File์ before ์๋ฆฌ๋จผํธ์ ์ ์ฌํ ๊ธฐ๋ฅ ์ ๊ณต
//value ์์ฑ : ํ๊ฒ๋ฉ์๋๋ฅผ ์ง์ ํ๊ธฐ ์ํ PointCut ํํ์์ ์์ฑ๊ฐ์ผ๋ก ์ค์
// => ๋ค๋ฅธ ์์ฑ์ด ์๋ ๊ฒฝ์ฐ ์์ฑ๊ฐ๋ง ์ค์ ๊ฐ๋ฅ
//@Before("within(xyz.itwill07.aop.AopAnnotationBean)")
//value ์์ฑ๊ฐ์ผ๋ก @Pointcut ์ด๋
ธํ
์ด์
์ ์ฌ์ฉํ ๋ฉ์๋๋ฅผ ํธ์ถํ๋ฉด ๋ฑ๋ก๋ PointCut ํํ์์ ์ ๊ณต๋ฐ์ ์ฌ์ฉ ๊ฐ๋ฅ
@Before("aopPointCut()")
public void beforeLog() {
logger.info("[before]ํต์ฌ๊ด์ฌ์ฝ๋ ์คํ ์ ์ฝ์
๋์ด ์คํ๋ ํก๋จ๊ด์ฌ์ฝ๋");
}
//3.
//After Advice ๋ฉ์๋
//@After : ํต์ฌ๊ด์ฌ์ฝ๋ ์คํ ํ์ ๋ฌด์กฐ๊ฑด ํก๋จ๊ด์ฌ์ฝ๋๋ฅผ ์คํํ๋ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ ์ด๋
ธํ
์ด์
// => Spring Bean Configuration File์ after ์๋ฆฌ๋จผํธ์ ์ ์ฌํ ๊ธฐ๋ฅ ์ ๊ณต
//@After(value = "within(xyz.itwill07.aop.AopAnnotationBean)")
@After("aopPointCut()")
public void afterLog() {
logger.info("[after]ํต์ฌ๊ด์ฌ์ฝ๋ ์คํ ํ ๋ฌด์กฐ๊ฑด ์ฝ์
๋์ด ์คํ๋ ํก๋จ๊ด์ฌ์ฝ๋");
}
//4.
//After Returning Advice ๋ฉ์๋
//@AfterReturning : ํต์ฌ๊ด์ฌ์ฝ๋๊ฐ ์ ์์ ์ผ๋ก ์คํ๋ ํ ํก๋จ๊ด์ฌ์ฝ๋๋ฅผ ์คํํ๋ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ ์ด๋
ธํ
์ด์
// => Spring Bean Configuration File์ after-returning ์๋ฆฌ๋จผํธ์ ์ ์ฌํ ๊ธฐ๋ฅ ์ ๊ณต
//@AfterReturning("aopPointCut()")
//returning ์์ฑ : ํ๊ฒ๋ฉ์๋์ ๋ฐํ๊ฐ์ ์ ์ฅํ๊ธฐ ์ํ ๋งค๊ฐ๋ณ์์ ์ด๋ฆ์ ์์ฑ๊ฐ์ผ๋ก ์ค์
@AfterReturning(value="aopPointCut()", returning="object")
public void afterReturningLog(Object object) {
logger.info("[after-returning]ํต์ฌ๊ด์ฌ์ฝ๋๊ฐ ์ ์์ ์ผ๋ก ์คํ๋ ํ ์ฝ์
๋์ด ์คํ๋ ํก๋จ๊ด์ฌ์ฝ๋");
}
//5.
//After Throwing Advice ๋ฉ์๋
//@AfterThrowing : ํต์ฌ๊ด์ฌ์ฝ๋ ์คํ์ ์์ธ๊ฐ ๋ฐ์๋ ๊ฒฝ์ฐ ํก๋จ๊ด์ฌ์ฝ๋๋ฅผ ์คํํ๋ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ ์ด๋
ธํ
์ด์
// => Spring Bean Configuration File์ after-throwing ์๋ฆฌ๋จผํธ์ ์ ์ฌํ ๊ธฐ๋ฅ ์ ๊ณต
//throwing ์์ฑ : ํ๊ฒ๋ฉ์๋์์ ๋ฐ์๋ ์์ธ๋ฅผ ์ ์ฅํ๊ธฐ ์ํ ๋งค๊ฐ๋ณ์์ ์ด๋ฆ์ ์์ฑ๊ฐ์ผ๋ก ์ค์
@AfterThrowing(value="aopPointCut()", throwing ="exception" )
public void afterThrowingLog(Exception exception) {
logger.info("[after-throwing]ํต์ฌ๊ด์ฌ์ฝ๋ ์คํ์ ์์ธ๊ฐ ๋ฐ์๋๋ฉด ์ฝ์
๋์ด ์คํ๋ ํก๋จ๊ด์ฌ์ฝ๋");
}
//6.
//Around Advice ๋ฉ์๋
//@Around : ํต์ฌ๊ด์ฌ์ฝ๋ ์คํ ์ ๊ณผ ํ์ ํก๋จ๊ด์ฌ์ฝ๋๋ฅผ ์คํํ๋ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ ์ด๋
ธํ
์ด์
// => Spring Bean Configuration File์ around ์๋ฆฌ๋จผํธ์ ์ ์ฌํ ๊ธฐ๋ฅ ์ ๊ณต
@Around("aopPointCut()")
public Object aroundLog(ProceedingJoinPoint joinPoint) throws Throwable {
logger.info("[around]ํต์ฌ๊ด์ฌ์ฝ๋ ์คํ ์ ์ ์ฝ์
๋์ด ์คํ๋ ํก๋จ๊ด์ฌ์ฝ๋");
Object object=joinPoint.proceed();
logger.info("[around]ํต์ฌ๊ด์ฌ์ฝ๋ ์คํ ํ์ ์ฝ์
๋์ด ์คํ๋ ํก๋จ๊ด์ฌ์ฝ๋");
return object;
}
}
04. ์คํํ๋ก๊ทธ๋จ
AopAnnotationApp.java
package xyz.itwill07.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AopAnnotationApp {
public static void main(String[] args) throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext("07-5_aopAnnotation.xml");
AopAnnotationBean bean = context.getBean("aopAnnotationBean",AopAnnotationBean.class);
System.out.println("======================================================");
bean.display1();
System.out.println("======================================================");
bean.display2();
System.out.println("======================================================");
bean.display3();
System.out.println("======================================================");
((ClassPathXmlApplicationContext)context).close();
}
}
๋ฐ์ํ