本文主要简单的介绍了如何基于Annotation方式使用AOP。本文测试使用的是Spring3.1 + AspectJ1.6.
一 示例代码
1.IHelloWord.java
package com.SpringAOP.HelloWord; public interface IHelloWord { public void sayHello(String message); }
2.HelloWord.java
package com.SpringAOP.HelloWord; public class HelloWord implements IHelloWord { public void sayHello(String message){ System.out.println(message); //int i=10/0; } }
3.HelloWordAspect.java
package com.SpringAOP.HelloWord; 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 HelloWordAspect { @Before(value="execution(* com.SpringAOP.HelloWord.HelloWord.*(..))") public void beforeSayHello(JoinPoint joinPoint){ System.out.println("Before :"+joinPoint.getArgs()[0]); } @After(value="execution(public void com.SpringAOP.HelloWord.HelloWord.sayHello(..)) && args(message)") public void afterSayHello(String message){ System.out.println("After : "+message); } @Around(value="execution(public void com.SpringAOP.HelloWord.HelloWord.sayHello(..))") public void aroundSayHello(ProceedingJoinPoint joinPoint) throws Throwable{ System.out.println("Around Before !! "); joinPoint.proceed(); System.out.println("Around After !! "); } @AfterThrowing(value="execution(public void com.SpringAOP.HelloWord.HelloWord.sayHello(..))",throwing="ex") public void afterThrowingSayHello(Exception ex){ System.out.println("After Throwing : "+ex.getMessage()); } @AfterReturning(value="execution(public void com.SpringAOP.HelloWord.HelloWord.sayHello(..))",returning="reval") public void afterReturningSayHello(String reval){ System.out.println("After Returning : "+reval); } }
4.HelloWord.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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> <bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" /> <bean id="helloWordAspect" class="com.SpringAOP.HelloWord.HelloWordAspect"></bean> <bean id="helloWord" class="com.SpringAOP.HelloWord.HelloWord"></bean> </beans>
5.HelloWordTest.java
package com.SpringAOP.HelloWord; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class HelloWordTest { @Test public void test(){ ApplicationContext ctx =new ClassPathXmlApplicationContext("com/SpringAOP/HelloWord/HelloWord.xml"); IHelloWord helloWord = (IHelloWord)ctx.getBean("helloWord"); helloWord.sayHello("Hello Word!!"); } }
6.需要导入的包
7.运行结果
二 说明
1.pointcut语法
execution ——for matching method execution join points
within —— 指定连接点所在的Java类型。
this ——bean reference (Spring AOP proxy) is an instance of the given type
target —— the target object (application object being proxied) is an instance of the given type
args —— 指定传入到连接点的参数
@target —— the class of the executing object has an annotation of the given type
@args —— the runtime type of the actual arguments passed have annotations of the given type(s)
@within —— limits matching to join points within types that have the given annotation
@annotation —— the subject of the join point (method being executed in Spring AOP) has the given annotation
组合pointcut表达式
可以使用 && 、|| 、! 组合pointcut表达式。
execution格式
execution(
可见性(可选) —— 使用public、protected、private指定可见性。也可以为*,表示任意可见性
返回值类型(必须) —— 指定返回值类型
声明类型(可选)—— java包
方法名(参数模式)(必须) —— 方法名称,和方法接收的参数
异常模式(可选) —— 指定方法签名中是否存在异常类型
)
在pointcut表达式中可以使用 * 、.. 、 +等通配符。
* :表示若干字符(排除 . 在外)
.. :表示若干字符(包括 . 在内)
+ :表示子类 ,比如Info+ 表示Info类及其子类
作者:红枫落叶
出处:http://www.cnblogs.com/wushiqi54719880/
关于作者:专注于Java企业运用、海量数据处理、hadoop、数字图像处理等。
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,如有问题,可以通过[email protected] 联系我,非常感谢。