AOP를 구현하는 3가지 방법


1) POJO Class를 이용한 AOP구현 (전통적인 방식)

2) 스프링 API를 이용한 AOP구현 (<aop> 엘리먼트 이용)

3) 어노테이션을 이용한 AOP 구현 (@Aspect으로 직접 구현)



AOP 관련 용어 정리


1. 대상 객체(Target Class)

2. PointCut

3. Advice

4. Advisor: PointCut + Advice




1) 메시지빈 인터페이스

package spring.aop.ch02;


public interface MessageBean {

public void sayHello();

}



2) 메시지빈 클래스

package spring.aop.ch02;


public class MessageBeanImpl implements MessageBean{

private String name;

private String greeting;

public void setName(String name){

this.name = name;

}

public void setGreeting(String greeting){

this.greeting = greeting;

}

@Override

public void sayHello() {


System.out.println(greeting+", "+name+"!");

}

}





3) 공통 관심사항이 적용되는 클래스. 메서드를 호출하는 시점(Before)의 인터페이스를 구현

package spring.aop.ch02;


import java.lang.reflect.Method;


import org.springframework.aop.MethodBeforeAdvice;


public class BeforeAdvice implements MethodBeforeAdvice{


@Override

public void before(Method method, Object[] args, Object target) throws Throwable {


System.out.println("BeforeAdvice.before() 메서드 실행");

}

}




4) 공통 관심사항이 적용되는 클래스. 끝나고 난 뒤 호출해주는 AfterReturningAdvice 인터페이스 구현

package spring.aop.ch02;

import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;



public class AfterAdvice implements AfterReturningAdvice  {


@Override

public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {

System.out.println("AfterReturningAdvice.afterReturning() 메서드 실행");

}

}






3) 실행 클래스

package spring.aop.ch02;


import org.springframework.context.support.AbstractApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;


public class MainAOP {


public static void main(String[] args) {

AbstractApplicationContext context = 

new ClassPathXmlApplicationContext("applicationContext2.xml");

context.registerShutdownHook();


MessageBean msgBean = 

context.getBean("proxy", MessageBean.class);

msgBean.sayHello();


context.close();

}

}




4) 설정파일


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

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


<bean id="msgBean" class="spring.aop.ch02.MessageBeanImpl" p:greeting="안녕~~~" p:name="홍길동"/>

<bean id="beforeAdvice" class="spring.aop.ch02.BeforeAdvice" />

<bean id="afterAdvice" class="spring.aop.ch02.AfterAdvice" />

<bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">

<property name="target" ref="msgBean"/>

<property name="interceptorNames">

<list>

<value>beforeAdvisor</value>

<value>afterAdvisor</value>

</list>

</property>

</bean>

<bean id="beforeAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor"

p:advice-ref="beforeAdvice" p:pointcut-ref="pointcut" />

<bean id="afterAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor"

p:advice-ref="afterAdvice" p:pointcut-ref="pointcut" />


<bean id="pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut" p:pattern=".*sayHello.*"/>


</beans>



BeforeAdvice.before() 메서드 실행
안녕~~~, 홍길동!
AfterReturningAdvice.afterReturning() 메서드 실행



Posted by netyhobby
,