4. 3. Spring AOP((Spring Aspect Oriented Programming)
3-7. Schema-Based Spring AOP(<AOP> 네임스페이스 이용)
선언적인 스프링 AOP 설정을 간단하게 해 준다. <Beans> 태그 내에 aop 네임스페이스를 선언해야 한
다.
모든 스프링 aop 설정을 <aop:config></aop:config> 안에 넣어야 하는데 포인트컷, 어스펙트, 어드바
이저 등을 정의하고 다른 스프링 빈을 참조 가능 하다.
aop:config 태그안에서 aspect를 정의하는 데 aspect=advisor=advice + pointcut 이므로 충고와 포인트
컷을 aop:config안에서 정의해야 한다. 아래는 충고 클래스인 aBean을 외부에 선언하고 aop:config 태그
안에서 ref로 참조 한 경우이다.
<aop:config>
<aop:aspect id="myAspect" ref="aBean">
...
</aop:aspect>
</aop:config>
<bean id="aBean" class="...">
...
</bean>
aop:aspect 내에 pointcut을 정의할 수도 있다.
<aop:config>
<aop:aspect id="myAspect" ref=“aBean“>
<aop:pointcut id=“onjPointcut“ expression="execution(*
x.y.z.service.*.*(..))"/>
……
</aop:aspect>
</aop:config>
<bean id="aBean" class="...">
...
</bean>
5. 3. Spring AOP((Spring Aspect Oriented Programming)
3-7. Schema-Based Spring AOP(<AOP> 네임스페이스 이용)
pointcut을 정의하고 사전충고에서 포인트 컷을 참조하고 충고 메소드를 정의할 수도 있다.
<aop:config>
<aop:aspect id="myAspect" ref="aBean">
<aop:pointcut id=“onjPointcut“
expression=“execution(* x.y.z.service..*(..))"/>
<!– pointcut-ref 대신 pointcut 속성을 사용하여 직접 포인트컷 표현식을 써도 된다
<!—myLogic() 메소드가 사전 충고용 메소드임을 정의
<aop:before pointcut-ref="onjPointcut " method="myLogic"/>
</aop:aspect>
</aop:config>
<bean id="aBean" class="x.y.z.advice.MyAdvice">
...
</bean>
[MyAdvice.java]
// 충고 클래스인 MyAdvice.java 에서 myLogic이라는 메소드가 정의되어 있어야 한다.
package x.y.z.advice;
public class MyAdvice {
public void myLogic(Object service) { }
…
}
6. 3. Spring AOP((Spring Aspect Oriented Programming)
3-7. Schema-Based Spring AOP(<AOP> 네임스페이스 이용)
포인트컷 조합을 and, or , not을 사용할 수 있다.
<aop:config>
<aop:aspect id="myAspect" ref="aBean">
<aop:pointcut id=“onjPointcut“
expression=“execution(* x.y.z.service..*(..))"/>
<!– pointcut-ref 대신 pointcut 속성을 사용하여 직접 포인트컷 표현식을 써도 된다
<!—myLogic() 메소드가 사전 충고용 메소드임을 정의
<aop:before pointcut-ref="onjPointcut " method="myLogic"/>
</aop:aspect>
</aop:config>
<bean id="aBean" class="x.y.z.advice.MyAdvice">
...
</bean>
[MyAdvice.java]
// 충고 클래스인 MyAdvice.java 에서 myLogic이라는 메소드가 정의되어 있어야 한다.
package x.y.z.advice;
public class MyAdvice {
public void myLogic(Object service) { }
…
}
7. 3. Spring AOP((Spring Aspect Oriented Programming)
3-7. Schema-Based Spring AOP(<AOP> 네임스페이스 이용)
충고 작성 예문
// 포인트컷을 AspectJ Expression으로 직접 정의
<aop:before pointcut="execution(* x.y.z.dao.*.*(..))" method="insert"/>
// 포인트컷을 외부에 만들고 pointcut-ref로 참조
<aop:after-returning pointcut-ref="onjPointcut " method=“myLogic1"/>
<aop:after-throwing pointcut-ref="onjPointcut " method=“myLogic2”/>
<aop:after pointcut-ref="onjPointcut " method="myLogic3"/>
<aop:around pointcut-ref="onjPointcut " method="myLogic4"/>
<aop:around pointcut-ref="onjPointcut " method="myLogic4“ arg-names=“name, age”/>
Advisor 작성 예문
<aop:config>
<aop:pointcut id=“onjPointcut"
expression="execution(* x.y.z.service.*.*(..))"/>
<aop:advisor pointcut-ref="onjPointcut" advice-ref="tx-advice"/>
</aop:config>
// 스프링 AOP를 이용하여 트랜잭션도 하나의 충고로 주입 가능하다.
// Pointcut 클래스(x.y.z.service 패키지 아래의 모든 클래스)의 모든 메소드가
// 트랜잭션(충고)이 적용됨을 정의한다.
<tx:advice id="tx-advice">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
8. 3. Spring AOP((Spring Aspect Oriented Programming)
: 이전에 프로그래밍적인 AOP/ProxyFactoryBean을 이용하여 작성한 SmallMart 예제를 aop
Namespace를 이용한 방식으로 변경해 보자.
3-5. Schema-Based Spring AOP(<AOP> 네임스페이스 이용)
3-5-3. Schema-Based Spring AOP(<AOP> 네임스페이스 이용) – 예제
STS에서
1. File -> New -> Spring Starter Project
Name : demo-smallmart2
Type : MAVEN
Package : onj.hello.aop2
2. Core -> AOP 체크
[SmallMartInterface.java]
package onj.hello.aop2;
public interface SmallMartInterface {
public void getProducts(String productName) throws Exception;
}
[SmallMart.java]
package onj.hello.aop2;
public class SmallMart implements SmallMartInterface {
public void getProducts(String productName) throws Exception {
System.out.println("[Target Method]getProduct()..." +
productName);
throw new Exception("error..."); //주석으로 막고 실행해 보자.
}
}