본문 바로가기

spring/AOP

AOP 구조를 이해하는 예제3(어노테이션을 이용한 예제)

applicationContext.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:aop="http://www.springframework.org/schema/aop"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:util="http://www.springframework.org/schema/util"

xmlns:task="http://www.springframework.org/schema/task"

xsi:schemaLocation="

   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd

http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd

        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">          


<aop:aspectj-autoproxy/>

<bean class="com.demo3.MyAspect"/>

<bean id="demoService3" class="com.demo3.core.DemoServiceImpl"/>

</beans>


=========================================================================================================================================================

DemoService.java


package com.demo3.core;


public interface DemoService {

public void save(String value);

public void write();

}

=========================================================================================================================================================

DemoServiceImpl.java

package com.demo3.core;


public class DemoServiceImpl implements DemoService{

private String value;

@Override

public void save(String value) {

// int a=this.value.length(); //예외 발생 (현재 value는 null이기 때문에)

this.value=value;

}


@Override

public void write() {

System.out.println(value);

}

}

=========================================================================================================================================================
DemoAdvice.java

package com.demo3;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

/*
 * @Aspect 어노테이션
 * 설정파일에서 Advice 및 pointcut 등을 설정하지 않고 자동으로 advide를 적용
 */

@Aspect
public class MyAspect {
@Before("execution(public * com.demo3.core.*.*(..))") // 메소드 이름의 범위를 정의함(com.demo3.core 폴더안의 파일이름을 정의함)
public String before(JoinPoint point){
String method=point.getSignature().getName();
System.out.println("@Before : 메소드  --> " + method);
return method;
}
//메소드 실행 후 실행(예외가 발생하면 실행  않함)
@AfterReturning(pointcut="execution(public * com.demo3.core.*.*(..))",returning="ret" )
public void returning(JoinPoint point, Object ret){
String method=point.getSignature().getName();
System.out.println("@AfterReturning : 메소드 --> " + method + ", retrun : " +ret);
}
//예외가 발생된 경우에만 실행
@AfterThrowing(pointcut="execution(public * com.demo3.core.*.*(..))", throwing="e")
public void throwing(JoinPoint point, Throwable e){
String method=point.getSignature().getName();
System.out.println("@AfterThrowing : 메소드 -->" + method +", throws : " + e.getClass().getName());
}
@After("execution(public * com.demo3.core.*.*(..))")
public void after(JoinPoint point){
String method=point.getSignature().getName();
System.out.println("@After : 메소드 -->" + method);
}
}
=========================================================================================================================================================
ResultMain.java

package com.demo2;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ResultMain {
public static void main(String[] args){
ClassPathXmlApplicationContext ctx= new ClassPathXmlApplicationContext("com/demo2/applicationContext.xml");
DemoService service=(DemoService)ctx.getBean("demoService2");
service.setValue("자바 AOP");
System.out.println(service.getValue());
}
}
=========================================================================================================================================================

ResultMain을 실행시킨 결과


에러 없을 때 순서

@Before : 메소드  --> save

@After : 메소드 -->save

@AfterReturning : 메소드 --> save, retrun : null

@Before : 메소드  --> write

스프

@After : 메소드 -->write

@AfterReturning : 메소드 --> write, retrun : null



에러 발생시 실행 순서

@Before : 메소드  --> save

@After : 메소드 -->save

@AfterThrowing : 메소드 -->save, throws

에러가 나기전까지만 실행하고 AfterThrowing만 실행