..열심히 공부하세../MVC

[19] 스프링 Advice 예제 1

댄스댄스 2012. 7. 11. 14:19

 

------------------------------------------------------------[참조]
<aop:before> 메소드 실행 전에 적용
<aop:after-returning> 메소드가 정상 실행된 후에만 적용
<aop:after-throwing> 메소드가 예외를 발생시킬 때에만 적용(catch문과 비슷)
<aop:after> 메소드가 실행된 후 무조건 적용(finally문과 비슷)
<aop:around>모든 시점에 적용

Advice 설정에는 pointcut-ref속성 말고 pointcut 속성도 있는데 pointcut속성에는
<aop:pointcut>태그의 expression속성을 바로 설정해 줄 수 있다.

 

>>>>>>>>>>>>>>>>> Advice 예제 1 >>>>>>>>>>>>>>>>>>>


------------------------------------------------------------Service.java
package part1;

public interface Service {
    public void first();
    public void first(String str);
    public void second();
    public String third();
    public void fourth();
    public void fifth();
}
------------------------------------------------------------ServiceImp.java
package part1;

public class ServiceImp implements Service {

    public ServiceImp(){
        System.out.println("생성자");
    }
    @Override
    public void first() {
        System.out.println("first()");
    }

    @Override
    public void first(String str) {
        System.out.println(str+"first()");
    }

    @Override
    public void second() {
        System.out.println("second()");
    }

    @Override
    public String third() {
        System.out.println("third()");
        return "JAVA";
    }

    @Override
    public void fourth() {
        System.out.println("fourth()");
    }

    @Override
    public void fifth() {
        int num[]=new int[3];
        num[4]=10;
    }

}
------------------------------------------------------------AopCommon.java
package part1;

import org.aspectj.lang.ProceedingJoinPoint;

public class AopCommon {
    public void commMethod1(){
        System.out.println("선행 공통메소드()");
    }
    public void commMethod2(){
        System.out.println("후행 공통메소드()");
    }
    public void commMethod3(Object ret){
        System.out.println("commMethod3:"+ret.toString());
    }
    public void commMethod4(ProceedingJoinPoint joinpoint){
        System.out.println("target 실행전");
        try{
            joinpoint.proceed();
        }catch(Throwable e){
            e.printStackTrace();
        }
        System.out.println("target 실행후");
    }
    public void commMethod5(ArrayIndexOutOfBoundsException thr){
        System.out.println("commMethod5:"+thr.toString());
    }
}
------------------------------------------------------------aop.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
                           http://www.springframework.org/schema/aop
                           http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<!-- Aspect 빈 객체 선언 -->
<bean name="commAspect" class="part1.AopCommon"/>

<!-- Target 빈 객체 선언 -->
<bean name="svc" class="part1.ServiceImp"/>

<!-- advice 정의 -->
<aop:config>
  <aop:pointcut expression="execution(* part1.ServiceImp.first(..))"  id=one"/>
  <aop:pointcut expression="execution(* part1.ServiceImp.second(..))" id="two"/>
  <aop:pointcut expression="execution(* part1.ServiceImp.third(..))"  id="three"/>
  <aop:pointcut expression="execution(* part1.ServiceImp.fourth(..))" id="four"/>
  <aop:pointcut expression="execution(* part1.ServiceImp.fifth(..))"  id="five"/>
 
  <aop:aspect ref="commAspect">
    <aop:before method="commMethod1" pointcut-ref=one"/>
    <aop:after  method="commMethod2" pointcut-ref="two"/>
    <aop:after-returning method="commMethod3" pointcut-ref="three" returning="ret"/>
    <aop:around method="commMethod4" pointcut-ref="four"/>
    <aop:after-throwing method="commMethod5" pointcut-ref="five" throwing="thr"/>
  </aop:aspect>
</aop:config>

</beans>
------------------------------------------------------------ServiceMain.java
package part1;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class ServiceMain {
    public static void main(String[] args) {
        ApplicationContext context=new FileSystemXmlApplicationContext("src/part1/aop.xml");
        Service svc=(Service)context.getBean("svc");
        //svc.first("korea");
        //svc.first();
        //svc.second();
        //svc.third();
        //svc.fourth();
        try{
            svc.fifth();
        }catch(ArrayIndexOutOfBoundsException ex){}
    }
}