2013년 7월 27일 토요일

[오라클자바개발자실무교육,오엔제이프로그래밍실무교육센터, Spring Framework3.2]Spring AOP(Spring 선언적 AOP, aop 네임스페이스)

Spring AOP(Spring 선언적 AOP, aop 네임스페이스)


오라클자바커뮤니티에서 설립한 오엔제이프로그래밍 실무교육센터
(오라클SQL, 튜닝, 힌트,자바프레임워크, 안드로이드, 아이폰, 닷넷  실무전문 강의)


  

선언적인 스프링 AOP 설정을 간단하게 해 준다.
<Beans> 태그내에 asp 네임스페이스를 선언해야 한다.
모든 스프링의 aop 설정을 <aop:config></aop:config> 안에 넣어야 하는데 포인트컷, 애스펙트,
어드바이저 등을 정의하고 다른 스프링 빈을 참조 가능 하다.
[MyDependency.java]
package onj.edu.aop9;
//충고를 받을 빈, MyBean의 setter로 주입된다.
public class MyDependency {
 public void hello(int intValue) {
  System.out.println("hello... OnJ" + intValue);
 }
 public void goodbye() {
  System.out.println("goodbye... OnJ");
 }
}

[MyBean.java]
package onj.edu.aop9;
//의존객체(MyDependency)를 주입받고 run() 메인에서 호출당함
public class MyBean {
 private MyDependency dependency;
 public void run() {
  dependency.hello(5919);
  dependency.hello(4790);
  dependency.goodbye();
 }
 public void setDependency(MyDependency dependency) {
  this.dependency = dependency;
 }
}

[MyAdvice.java]

package onj.edu.aop9;
import org.aspectj.lang.JoinPoint;
//타겟이 되는 MyDendency의 hello(), goodbye()에 적용될 충고
public class MyAdvice {
 public void simpleBeforeAdvice(JoinPoint joinPoint) {
  System.out.println("충고실행 ::: " + joinPoint.getSignature().getDeclaringTypeName() + "," + joinPoint.getSignature().getName());
 }
}
[aop-context9.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:util="http://www.springframework.org/schema/util"
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/contexthttp://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“>
<aop:config>
<aop:pointcut id="onjpointcut" __EXPRESSION__="execution(* onj.edu.aop9..hello*(int))"/>
<aop:aspect ref="advice">
<aop:before pointcut-ref="onjpointcut" method="simpleBeforeAdvice"/>
</aop:aspect>
</aop:config>
<bean id="advice" class="onj.edu.aop9.MyAdvice"/>
<!--  실제 충고를 받을 빈, 이빈이 프록시가 되어 충고가 적용됨 -->
<bean id="myDependency" class="onj.edu.aop9.MyDependency"/>
<!-- MyBean의 세터를 통해 중고가 적용될 프록시빈이 주입 -->
<bean id="myBean" class="onj.edu.aop9.MyBean">
<property name="dependency" ref="myDependency"/>
</bean>
</beans>

[AopNamesapceExam.java]
package onj.edu.aop9;
import org.springframework.context.support.GenericXmlApplicationContext;
public class AopNamespaceExam {
 public static void main(String[] args) {
  GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
  ctx.load("app-context9.xml");
  ctx.refresh();
  MyBean bean = (MyBean)ctx.getBean("myBean");
  System.out.println("---- bean ---");
  bean.run();
  ctx.close();
 }
}
[결과]
---- bean ---
충고실행 ::: onj.edu.aop9.MyDependency,hello
hello... OnJ5919
충고실행 ::: onj.edu.aop9.MyDependency,hello
hello... OnJ4790

댓글 없음:

댓글 쓰기