在本教程中,我们将向你展示如何转换上章节中 Spring AOP+AspectJ 注解转成基于XML的配置。
对于那些不喜欢注释,使用JDK1.4,则可以基于XML,而不使用 AspectJ。
再次回顾上个 customerBo 接口中的几个方法,以后你将学会如何在 XML文件实现 AspectJ 拦截
package com.gp6.aop.aspectJ.annotation;
public interface CustomerBo {
void addCustomer();
String addCustomerReturnValue();
void addCustomerThrowException() throws Exception;
void addCustomerAround(String name);
}
公用类
public interface CustomerBo {
void addCustomer();
String addCustomerReturnValue();
void addCustomerThrowException() throws Exception;
void addCustomerAround(String name);
}
public class CustomerBoImpl implements CustomerBo {
public void addCustomer(){
System.out.println("addCustomer() is running ");
}
public String addCustomerReturnValue(){
System.out.println("addCustomerReturnValue() is running ");
return "abc";
}
public void addCustomerThrowException() throws Exception {
System.out.println("addCustomerThrowException() is running ");
throw new Exception("Generic Error");
}
public void addCustomerAround(String name){
System.out.println("addCustomerAround() is running, args : " + name);
}
}
1. AspectJ <aop:before> = @Before
AspectJ @Before 示例.
package com.gp6.aop.aspectJ.xml.before;
import java.util.Arrays;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class LoggingAspect {
public void logBefore(JoinPoint joinPoint) {
System.out.println("logBefore() is running!================START");
System.out.println("hijacked : " + joinPoint.getSignature().getName());
System.out.println("logBefore() is running!================END");
}
}
<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 ">
<!-- 在 Spring 配置文件,把“<aop:aspectj-autoproxy />”,并定义Aspect(拦截)和普通的bean -->
<aop:aspectj-autoproxy />
<bean id="customerBo" class="com.gp6.aop.aspectJ.xml.before.CustomerBoImpl" />
<!-- Aspect -->
<bean id="logAspect" class="com.gp6.aop.aspectJ.xml.before.LoggingAspect" />
<aop:config>
<aop:aspect id="aspectLoggging" ref="logAspect" >
<!-- @Before -->
<aop:pointcut id="pointCutBefore" expression="execution(* com.gp6.aop.aspectJ.xml.before.CustomerBo.addCustomer(..))" />
<aop:before method="logBefore" pointcut-ref="pointCutBefore" />
</aop:aspect>
</aop:config>
</beans>
package com.gp6.aop.aspectJ.xml.before;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) throws Exception {
ApplicationContext appContext = new ClassPathXmlApplicationContext("com/gp6/aop/aspectJ/xml/before/applicationContext.xml");
CustomerBo customer = (CustomerBo) appContext.getBean("customerBo");
customer.addCustomer();
//customer.addCustomerReturnValue();
//customer.addCustomerThrowException();
//customer.addCustomerAround("gp6");
}
}
运行
logBefore() is running!================START
hijacked : addCustomer
logBefore() is running!================END
addCustomer() is running
2. AspectJ <aop:after> = @After
package com.gp6.aop.aspectJ.xml.after;
import java.util.Arrays;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class LoggingAspect {
public void logAfter(JoinPoint joinPoint) {
System.out.println("logAfter() is running!================START");
System.out.println("hijacked : " + joinPoint.getSignature().getName());
System.out.println("logAfter() is running!================END");
}
}
<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 ">
<!-- 在 Spring 配置文件,把“<aop:aspectj-autoproxy />”,并定义Aspect(拦截)和普通的bean -->
<aop:aspectj-autoproxy />
<bean id="customerBo" class="com.gp6.aop.aspectJ.xml.after.CustomerBoImpl" />
<!-- Aspect -->
<bean id="logAspect" class="com.gp6.aop.aspectJ.xml.after.LoggingAspect" />
<aop:config>
<aop:aspect id="aspectLoggging" ref="logAspect" >
<!-- @After -->
<aop:pointcut id="pointCutAfter" expression="execution(* com.gp6.aop.aspectJ.xml.after.CustomerBo.addCustomer(..))" />
<aop:after method="logAfter" pointcut-ref="pointCutAfter" />
</aop:aspect>
</aop:config>
</beans>
package com.gp6.aop.aspectJ.xml.after;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) throws Exception {
ApplicationContext appContext = new ClassPathXmlApplicationContext("com/gp6/aop/aspectJ/xml/after/applicationContext.xml");
CustomerBo customer = (CustomerBo) appContext.getBean("customerBo");
customer.addCustomer();
//customer.addCustomerReturnValue();
//customer.addCustomerThrowException();
//customer.addCustomerAround("gp6");
}
}
运行
addCustomer() is running
logAfter() is running!================START
hijacked : addCustomer
logAfter() is running!================END
3. AspectJ <aop:after-returning> = @AfterReturning
AspectJ @AfterReturning 示例.
package com.gp6.aop.aspectJ.xml.afterReturn;
import java.util.Arrays;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class LoggingAspect {
public void logAfterReturning(JoinPoint joinPoint, Object result) {
System.out.println("logAfterReturning() is running!================START");
System.out.println("hijacked : " + joinPoint.getSignature().getName());
System.out.println("Method returned value is : " + result);
System.out.println("logAfterReturning() is running!================END");
}
}
<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 ">
<!-- 在 Spring 配置文件,把“<aop:aspectj-autoproxy />”,并定义Aspect(拦截)和普通的bean -->
<aop:aspectj-autoproxy />
<bean id="customerBo" class="com.gp6.aop.aspectJ.xml.afterReturn.CustomerBoImpl" />
<!-- Aspect -->
<bean id="logAspect" class="com.gp6.aop.aspectJ.xml.afterReturn.LoggingAspect" />
<aop:config>
<aop:aspect id="aspectLoggging" ref="logAspect" >
<!-- @AfterReturning -->
<aop:pointcut id="pointCutAfterReturning" expression="execution(* com.gp6.aop.aspectJ.xml.afterReturn.CustomerBo.addCustomerReturnValue(..))" />
<aop:after-returning method="logAfterReturning" returning="result" pointcut-ref="pointCutAfterReturning" />
</aop:aspect>
</aop:config>
</beans>
package com.gp6.aop.aspectJ.xml.afterReturn;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) throws Exception {
ApplicationContext appContext = new ClassPathXmlApplicationContext("com/gp6/aop/aspectJ/xml/afterReturn/applicationContext.xml");
CustomerBo customer = (CustomerBo) appContext.getBean("customerBo");
//customer.addCustomer();
customer.addCustomerReturnValue();
//customer.addCustomerThrowException();
//customer.addCustomerAround("gp6");
}
}
运行
addCustomerReturnValue() is running
logAfterReturning() is running!================START
hijacked : addCustomerReturnValue
Method returned value is : abc
logAfterReturning() is running!================END
4. AspectJ <aop:after-throwing> = @AfterReturning
AspectJ @AfterReturning 示例.
package com.gp6.aop.aspectJ.xml.afterThrowing;
import java.util.Arrays;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class LoggingAspect {
public void logAfterThrowing(JoinPoint joinPoint, Throwable error) {
System.out.println("logAfterThrowing() is running!================Start");
System.out.println("hijacked : " + joinPoint.getSignature().getName());
System.out.println("Exception : " + error);
System.out.println("logAfterThrowing() is running!================END");
}
}
<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 ">
<!-- 在 Spring 配置文件,把“<aop:aspectj-autoproxy />”,并定义Aspect(拦截)和普通的bean -->
<aop:aspectj-autoproxy />
<bean id="customerBo" class="com.gp6.aop.aspectJ.xml.afterThrowing.CustomerBoImpl" />
<!-- Aspect -->
<bean id="logAspect" class="com.gp6.aop.aspectJ.xml.afterThrowing.LoggingAspect" />
<aop:config>
<aop:aspect id="aspectLoggging" ref="logAspect" >
<!-- @AfterThrowing -->
<aop:pointcut id="pointCutAfterThrowing" expression="execution(* com.gp6.aop.aspectJ.xml.afterThrowing.CustomerBo.addCustomerThrowException(..))" />
<aop:after-throwing method="logAfterThrowing" throwing="error" pointcut-ref="pointCutAfterThrowing" />
</aop:aspect>
</aop:config>
</beans>
package com.gp6.aop.aspectJ.xml.afterThrowing;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) throws Exception {
ApplicationContext appContext = new ClassPathXmlApplicationContext("com/gp6/aop/aspectJ/xml/afterThrowing/applicationContext.xml");
CustomerBo customer = (CustomerBo) appContext.getBean("customerBo");
//customer.addCustomer();
//customer.addCustomerReturnValue();
customer.addCustomerThrowException();
//customer.addCustomerAround("gp6");
}
}
运行
addCustomerThrowException() is running
logAfterThrowing() is running!================Start
hijacked : addCustomerThrowException
Exception : java.lang.Exception: Generic Error
logAfterThrowing() is running!================END
Exception in thread "main" java.lang.Exception: Generic Error
at com.gp6.aop.aspectJ.xml.afterThrowing.CustomerBoImpl.addCustomerThrowException(CustomerBoImpl.java:15)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:55)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:90)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at com.sun.proxy.$Proxy4.addCustomerThrowException(Unknown Source)
at com.gp6.aop.aspectJ.xml.afterThrowing.Test.main(Test.java:16)
5. AspectJ <aop:after-around> = @Around
AspectJ @Around 示例.
package com.gp6.aop.aspectJ.xml.around;
import java.util.Arrays;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class LoggingAspect {
public void logAround(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("logAround() is running!================START");
System.out.println("hijacked method : " + joinPoint.getSignature().getName());
System.out.println("hijacked arguments : " + Arrays.toString(joinPoint.getArgs()));
System.out.println("Around before is running!================START");
joinPoint.proceed(); //continue on the intercepted method
System.out.println("Around after is running!================START");
System.out.println("******");
}
}
<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 ">
<!-- 在 Spring 配置文件,把“<aop:aspectj-autoproxy />”,并定义Aspect(拦截)和普通的bean -->
<aop:aspectj-autoproxy />
<bean id="customerBo" class="com.gp6.aop.aspectJ.xml.around.CustomerBoImpl" />
<!-- Aspect -->
<bean id="logAspect" class="com.gp6.aop.aspectJ.xml.around.LoggingAspect" />
<aop:config>
<aop:aspect id="aspectLoggging" ref="logAspect" >
<!-- @Around -->
<aop:pointcut id="pointCutAround" expression="execution(* com.gp6.aop.aspectJ.xml.around.CustomerBo.addCustomerAround(..))" />
<aop:around method="logAround" pointcut-ref="pointCutAround" />
</aop:aspect>
</aop:config>
</beans>
package com.gp6.aop.aspectJ.xml.around;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) throws Exception {
ApplicationContext appContext = new ClassPathXmlApplicationContext("com/gp6/aop/aspectJ/xml/around/applicationContext.xml");
CustomerBo customer = (CustomerBo) appContext.getBean("customerBo");
//customer.addCustomer();
//customer.addCustomerReturnValue();
//customer.addCustomerThrowException();
customer.addCustomerAround("gp6");
}
}
运行
logAround() is running!================START
hijacked method : addCustomerAround
hijacked arguments : [gp6]
Around before is running!================START
addCustomerAround() is running, args : gp6
Around after is running!================START
******
网友评论