XML構成のSpring AOP AspectJの例
このチュートリアルでは、最後のリンク//spring3/spring-aop-aspectj-annotation-example/[Spring AOP AspectJアノテーション]をXMLベースの設定に変換する方法を示します。
注釈やJDK 1.4を使用しない人にとっては、AspectJをXMLベースで使用することができます。
最後のcustomerBoインターフェイスをもう一度見直してください。いくつかのメソッドがあり、後でXMLファイルのAspectJを使ってインターセプトする方法を学びます。
package com.mkyong.customer.bo;
public interface CustomerBo {
void addCustomer();
String addCustomerReturnValue();
void addCustomerThrowException() throws Exception;
void addCustomerAround(String name);
}
1. AspectJ <aop:before> = @Before
AspectJ @前例。
package com.mkyong.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class LoggingAspect {
@Before("execution(** com.mkyong.customer.bo.CustomerBo.addCustomer(..))")
public void logBefore(JoinPoint joinPoint) {
//...
}
}
<!-- Aspect -->
<bean id="logAspect" class="com.mkyong.aspect.LoggingAspect"/>
<aop:config>
<aop:aspect id="aspectLoggging" ref="logAspect" >
<!-- @Before -->
<aop:pointcut id="pointCutBefore"
expression="execution(** com.mkyong.customer.bo.CustomerBo.addCustomer(..))"/>
<aop:before method="logBefore" pointcut-ref="pointCutBefore"/>
</aop:aspect>
</aop:config>
2. AspectJ <aop:after> = @After
AspectJ @Afterの例です。
package com.mkyong.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.After;
@Aspect
public class LoggingAspect {
@After("execution(** com.mkyong.customer.bo.CustomerBo.addCustomer(..))")
public void logAfter(JoinPoint joinPoint) {
//...
}
}
-
<aop:after> ** を使用したXMLの同等の機能
<!-- Aspect -->
<bean id="logAspect" class="com.mkyong.aspect.LoggingAspect"/>
<aop:config>
<aop:aspect id="aspectLoggging" ref="logAspect" >
<!-- @After -->
<aop:pointcut id="pointCutAfter"
expression="execution(** com.mkyong.customer.bo.CustomerBo.addCustomer(..))"/>
<aop:after method="logAfter" pointcut-ref="pointCutAfter"/>
</aop:aspect>
</aop:config>
3. AspectJ <aop:after-returning> = @AfterReturning
AspectJ @AfterReturningの例です。
package com.mkyong.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.AfterReturning;
@Aspect
public class LoggingAspect {
@AfterReturning(
pointcut = "execution(** com.mkyong.customer.bo.CustomerBo.addCustomerReturnValue(..))",
returning= "result")
public void logAfterReturning(JoinPoint joinPoint, Object result) {
//...
}
}
-
<aop:after-returning> ** を使用したXMLの同等の機能
<!-- Aspect -->
<bean id="logAspect" class="com.mkyong.aspect.LoggingAspect"/>
<aop:config>
<aop:aspect id="aspectLoggging" ref="logAspect" >
<!-- @AfterReturning -->
<aop:pointcut id="pointCutAfterReturning"
expression="execution(** com.mkyong.customer.bo.CustomerBo.addCustomerReturnValue(..))"/>
<aop:after-returning method="logAfterReturning" returning="result"
pointcut-ref="pointCutAfterReturning"/>
</aop:aspect>
</aop:config>
4. AspectJ <aop:after-throwing> = @AfterReturning
AspectJ @AfterReturningの例です。
package com.mkyong.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.AfterThrowing;
@Aspect
public class LoggingAspect {
@AfterThrowing(
pointcut = "execution(** com.mkyong.customer.bo.CustomerBo.addCustomerThrowException(..))",
throwing= "error")
public void logAfterThrowing(JoinPoint joinPoint, Throwable error) {
//...
}
}
-
<aop:after-throwing> ** を使用したXMLの同等の機能
<!-- Aspect -->
<bean id="logAspect" class="com.mkyong.aspect.LoggingAspect"/>
<aop:config>
<aop:aspect id="aspectLoggging" ref="logAspect" >
<!-- @AfterThrowing -->
<aop:pointcut id="pointCutAfterThrowing"
expression="execution(** com.mkyong.customer.bo.CustomerBo.addCustomerThrowException(..))"/>
<aop:after-throwing method="logAfterThrowing" throwing="error"
pointcut-ref="pointCutAfterThrowing" />
</aop:aspect>
</aop:config>
5. AspectJ <aop:after-around> = @Around
AspectJ @Aroundの例です。
package com.mkyong.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Around;
@Aspect
public class LoggingAspect {
@Around("execution(** com.mkyong.customer.bo.CustomerBo.addCustomerAround(..))")
public void logAround(ProceedingJoinPoint joinPoint) throws Throwable {
//...
}
}
-
<aop:after-around> ** を使用したXMLの同等の機能
<!-- Aspect -->
<bean id="logAspect" class="com.mkyong.aspect.LoggingAspect"/>
<aop:config>
<aop:aspect id="aspectLoggging" ref="logAspect" >
<!-- @Around -->
<aop:pointcut id="pointCutAround"
expression="execution(** com.mkyong.customer.bo.CustomerBo.addCustomerAround(..))"/>
<aop:around method="logAround" pointcut-ref="pointCutAround" />
</aop:aspect>
</aop:config>
完全なXMLの例
完全なAspectJ XMLベースの設定ファイルを参照してください。
<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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<aop:aspectj-autoproxy/>
<bean id="customerBo" class="com.mkyong.customer.bo.impl.CustomerBoImpl"/>
<!-- Aspect -->
<bean id="logAspect" class="com.mkyong.aspect.LoggingAspect"/>
<aop:config>
<aop:aspect id="aspectLoggging" ref="logAspect">
<!-- @Before -->
<aop:pointcut id="pointCutBefore"
expression="execution(** com.mkyong.customer.bo.CustomerBo.addCustomer(..))"/>
<aop:before method="logBefore" pointcut-ref="pointCutBefore"/>
<!-- @After -->
<aop:pointcut id="pointCutAfter"
expression="execution(** com.mkyong.customer.bo.CustomerBo.addCustomer(..))"/>
<aop:after method="logAfter" pointcut-ref="pointCutAfter"/>
<!-- @AfterReturning -->
<aop:pointcut id="pointCutAfterReturning"
expression="execution(** com.mkyong.customer.bo.CustomerBo.addCustomerReturnValue(..))"/>
<aop:after-returning method="logAfterReturning"
returning="result" pointcut-ref="pointCutAfterReturning"/>
<!-- @AfterThrowing -->
<aop:pointcut id="pointCutAfterThrowing"
expression="execution(** com.mkyong.customer.bo.CustomerBo.addCustomerThrowException(..))"/>
<aop:after-throwing method="logAfterThrowing"
throwing="error" pointcut-ref="pointCutAfterThrowing"/>
<!-- @Around -->
<aop:pointcut id="pointCutAround"
expression="execution(** com.mkyong.customer.bo.CustomerBo.addCustomerAround(..))"/>
<aop:around method="logAround" pointcut-ref="pointCutAround"/>
</aop:aspect>
</aop:config>
</beans>
ソースコードをダウンロードする
ダウンロードする –
Spring3-AOP-AspectJ-XML-Example.zip
(8 KB)
参考文献
-
リンク://spring3/spring-aop-aspectj-annotation-example/[Spring AOP
AspectJ注釈の例]。
http://www.eclipse.org/aspectj/doc/released/progguide/index.html
[AspectJ
プログラミングガイド]。
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/aop.html
[Spring
AOP AspectJリファレンス]