>>>>>>>>>>>>>>>> Advice 예제 2  >>>>>>>>>>>>>>>>>>

 

------------------------------------------------------------MannerAOP.java
package secondSpringAOP;

public class MannerAOP {
    public void beforeSaying(){
        System.out.println("당신의 이름은?");
    }
    public void afterreturnSaying(){
        System.out.println("정말 멋진 분이군요!!");
    }
    public void afterSaying(){
        System.out.println("사랑합니다:)");
    }
}
------------------------------------------------------------Human.java
package secondSpringAOP;

public interface Human {
    public String sayName(String name);
}
------------------------------------------------------------HongGilDong.java
package secondSpringAOP;

public class HongGilDong implements Human {
    @Override
    public String sayName(String name) {
        System.out.println(name+" 이라고 합니다.");
        return "난~~~동해번쩍 서해번쩍"+name+"이다";
    }
}
------------------------------------------------------------LeeSunShin.java
package secondSpringAOP;

public class LeeSunShin implements Human {
    @Override
    public String sayName(String name) {
        System.out.println(name+" 이라고 합니다.");
        return "난~~~민족의 영웅"+name+"이다";
    }
}
------------------------------------------------------------YuGwanSoon.java
package secondSpringAOP;

public class YuGwanSoon implements Human {
    @Override
    public String sayName(String name) {
        System.out.println(name+" 이라고 합니다");
        return "난 ~~~독립투사"+name+"이다";
    }
}
------------------------------------------------------------aopAppContext.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">
  
      <bean id="hgd" class="secondSpringAOP.HongGilDong"/>                         
      <bean id="lss" class="secondSpringAOP.LeeSunShin"/>                         
      <bean id="ygs" class="secondSpringAOP.YuGwanSoon"/>
      <bean id="manner" class="secondSpringAOP.MannerAOP"/>
  
    <aop:config>
         <aop:aspect ref="manner">
             <aop:pointcut id="greeting"
                                  expression="execution(public * secondSpringAOP.Human.sayName(..))"/>
             <aop:before pointcut-ref="greeting" method="beforeSaying"/>
             <aop:after-returning pointcut-ref="greeting" method="afterreturnSaying"/>
             <aop:after pointcut-ref="greeting" method="afterSaying"/>
        </aop:aspect>
    </aop:config>                           
</beans>
------------------------------------------------------------TestSecondAOP.java
package secondSpringAOP;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSecondAOP {

    public static void main(String[] args) {
        BeanFactory bf=new ClassPathXmlApplicationContext("secondSpringAOP/aopAppContext.xml");
       
        Human human=(Human)bf.getBean("hgd");
        System.out.println(human.sayName("홍길동"));
        System.out.println("");
       
        human=(Human)bf.getBean("lss");
        System.out.println(human.sayName("이순신"));
        System.out.println("");
       
        human=(Human)bf.getBean("ygs");
        System.out.println(human.sayName("유관순"));
        System.out.println("");
       
    }
}
------------------------------------------------------------결과
당신의 이름은?
홍길동 이라고 합니다.
정말 멋진 분이군요!!
사랑합니다:)
난~~~동해번쩍 서해번쩍홍길동이다

 

당신의 이름은?
이순신 이라고 합니다.
정말 멋진 분이군요!!
사랑합니다:)
난~~~민족의 영웅이순신이다

 

당신의 이름은?
유관순 이라고 합니다
정말 멋진 분이군요!!
사랑합니다:)
난 ~~~독립투사유관순이다

+ Recent posts