본문 바로가기

spring/AOP

AOP 구조를 이해하는 예제2(MethodInterceptor)

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">          


<bean id="demoService2" class="com.demo2.DemoServiceImpl"/>


<!-- Advice -->

<bean id="myAdvice" class="com.demo2.DemoAdvice"/>


<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean>


<!-- advisor -->

<bean id="myAdvisor" class="org.springframework.aop.aspectj.AspectJExpressionPointcutAdvisor">

<property name="advice" ref="myAdvice"/>

<property name="expression" value="execution(public * *(..))"/><!-- return 타입 *, 메소드이름 *, 인자뭐가 와도 상관없음. -->

</bean>

</beans>


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

DemoService.java


package com.demo2;


public interface DemoService {

public void setValue(String value);

public String getValue();

}

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

DemoServiceImpl.java

package com.demo2;


public class DemoServiceImpl implements DemoService{

private String value;

@Override

public void setValue(String value) {

this.value=value;

}


@Override

public String getValue() {

return value;

}

}


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

DemoAdvice.java

package com.demo2;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class DemoAdvice implements MethodInterceptor{

@Override
public Object invoke(MethodInvocation inv) throws Throwable {
// 대상 메소드 실행전
System.out.println(inv.getMethod().getName()+" 메소드 실행 전....");
// 대상 메소드 실행
Object returnValue=inv.proceed();
//대상 메소드 실행 후
System.out.println(inv.getMethod().getName()+" 메소드 실행 후.... 리턴 값 : "+returnValue);
return returnValue;
}
}

=========================================================================================================================================================
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을 실행시킨 결과


setValue 메소드 실행 전....

setValue 메소드 실행 후.... 리턴 값 : null

getValue 메소드 실행 전....

getValue 메소드 실행 후.... 리턴 값 : 자바 AOP

자바 AOP


// 설명

setValue메소드를 실행하기 직전 MethodInterceptor가 implement된 DemoAdvice.java가 한번 실행된다. 그래서 첫번째 줄의 결과가 찍혔다.

setValue메소드를 실행하고 난후 MethodInterceptor가 implement된 DemoAdvice.java가 다시 한번 실행된다. 그래서 두번째 줄의 결과가 찍혔다.

getValue 메소드를 실행하기 직전 MethodInterceptor가 implement된 DemoAdvice.java가 한번 실행된다. 그래서 세번째 줄의 결과가 찍혔다.

getValue 메소드를 실행하고 난후 MethodInterceptor가 implement된 DemoAdvice.java가 다시 한번 실행된다. 그래서 네번째 줄의 결과가 찍혔다.

System.out.println(service.getValue());의 실행결과가 찍혔다.