SpringAOP实例之简单实现(Annotation形式)
来源:互联网 发布:郭天祥单片机教材下载 编辑:程序博客网 时间:2024/05/20 10:11
1 启用对@AspectJ的支持
Spring默认不支持@AspectJ风格的切面声明,为了支持需要使用如下配置:
<!-- 开启支持@AspectJ风格的切面声明 --> <aop:aspectj-autoproxy/></span>
这样Spring就能发现@AspectJ风格的切面并且将切面应用到目标对象。
2 声明切面
@AspectJ风格的声明切面非常简单,使用@Aspect注解进行声明:
package zmx.spring.aop.test;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;//定义切面@Aspect()public class LoggerAspect {//定义切入点@Pointcut(value="execution(* zmx.spring.aop.test..*.*(..)) && args(param)", argNames = "param") public void beforePointcut(String param) {}@Pointcut(value="execution(* zmx.spring.aop.test..*.*(..))") public void afterPointcut(){}//定义通知 //前置日志通知 @Before(value = "beforePointcut(param)", argNames = "param") public void beforeAdvice(String param) { System.out.println("===========before advice param:" + param); } //后置日志通知 @After(value = "afterPointcut()") public void afterAdvice() { System.out.println("===========after advice param"); }}
然后将该切面在配置文件中声明为Bean后,Spring就能自动识别并进行AOP方面的配置:
<!-- 切面 --> <bean id="loggerAspect" class="zmx.spring.aop.test.LoggerAspect" />
<span style="font-size:12px;"> 该切面就是一个POJO,可以在该切面中进行切入点及通知定义,接着往下看吧。</span>
3 声明切入点
@AspectJ风格的命名切入点使用org.aspectj.lang.annotation包下的@Pointcut+方法(方法必须是返回void类型)实现。
value:指定切入点表达式;
argNames:指定命名切入点方法参数列表参数名字,可以有多个用“,”分隔,这些参数将传递给通知方法同名的参数,同时比如切入点表达式“args(param)”将匹配参数类型为命名切入点方法同名参数指定的参数类型。
pointcutName:切入点名字,可以使用该名字进行引用该切入点表达式。
定义了一个切入点,名字为“beforePointcut”,该切入点将匹配目标方法的第一个参数类型为通知方法实现中参数名为“param”的参数类型。
4 声明通知
@AspectJ风格的声明通知也支持5种通知类型:
一、前置通知:使用org.aspectj.lang.annotation 包下的@Before注解声明;
value:指定切入点表达式或命名切入点;
argNames:与Schema方式配置中的同义。
5 实例
接下来示例一下吧:
1、定义接口和实现,在此我们就使用Schema风格时的定义;
接口:
package zmx.spring.aop.test;public interface IHelloWorldService {public void sayHello(); public void recordLog(String log);}
实现:
package zmx.spring.aop.test;public class HelloWorldService implements IHelloWorldService {@Overridepublic void sayHello() { System.out.println("============Hello World!================"); }@Overridepublic void recordLog(String log) { System.out.println("============record Log================"+log);}}
2、定义切面:
3、定义切入点:
4、定义通知:
package zmx.spring.aop.test;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;//2、定义切面@Aspect()public class LoggerAspect {//3、定义切入点@Pointcut(value="execution(* zmx.spring.aop.test..*.*(..)) && args(param)", argNames = "param") public void beforePointcut(String param) {}@Pointcut(value="execution(* zmx.spring.aop.test..*.*(..))") public void afterPointcut(){}//4、定义通知 //前置日志通知 @Before(value = "beforePointcut(param)", argNames = "param") public void beforeAdvice(String param) { System.out.println("===========before advice param:" + param); } //后置日志通知 @After(value = "afterPointcut()") public void afterAdvice() { System.out.println("===========after advice param"); }}
5、在配置文件中进行如下配置
<?xml version="1.0" encoding="UTF-8" standalone="no"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 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/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <!-- 开启支持@AspectJ风格的切面声明 --> <aop:aspectj-autoproxy/> <!-- 目标对象 --> <bean id="helloWorldService" class="zmx.spring.aop.test.HelloWorldService" /> <!-- 切面 --> <bean id="loggerAspect" class="zmx.spring.aop.test.LoggerAspect" /> </beans>
6、测试代码:
package zmx.spring.aop.test;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class MainTest {@Testpublic void testHelloWorld(){ ApplicationContext ctx = new ClassPathXmlApplicationContext("zmx/spring/applicationContext.xml"); IHelloWorldService helloworldService = ctx.getBean("helloWorldService", IHelloWorldService.class); helloworldService.recordLog("日志");}}
将输出:
===========before advice param:日志
============recordLog================日志
===========after advice param</span>
切面、切入点、通知全部使用注解完成:
1)使用@Aspect将POJO声明为切面;
2)使用@Pointcut进行命名切入点声明,同时指定目标方法第一个参数类型必须是java.lang.String,对于其他匹配的方法但参数类型不一致的将也是不匹配的,通过argNames = "param"指定了将把该匹配的目标方法参数传递给通知同名的参数上;
3)使用@Before进行前置通知声明,其中value用于定义切入点表达式或引用命名切入点;
4)配置文件需要使用<aop:aspectj-autoproxy/>来开启注解风格的@AspectJ支持;
5)需要将切面注册为Bean,如“aspect”Bean;
6)测试代码完全一样。
二、后置返回通知:使用org.aspectj.lang.annotation 包下的@AfterReturning注解声明;
value:指定切入点表达式或命名切入点;
pointcut:同样是指定切入点表达式或命名切入点,如果指定了将覆盖value属性指定的,pointcut具有高优先级;
argNames:与Schema方式配置中的同义;
returning:与Schema方式配置中的同义。
其中测试代码与Schema方式几乎一样,在此就不演示了,如果需要请参考AopTest.java中的testAnnotationAfterReturningAdvice测试方法。
三、后置异常通知:使用org.aspectj.lang.annotation 包下的@AfterThrowing注解声明;
value:指定切入点表达式或命名切入点;
pointcut:同样是指定切入点表达式或命名切入点,如果指定了将覆盖value属性指定的,pointcut具有高优先级;
argNames:与Schema方式配置中的同义;
throwing:与Schema方式配置中的同义。
其中测试代码与Schema方式几乎一样,在此就不演示了,如果需要请参考AopTest.java中的testAnnotationAfterThrowingAdvice测试方法。
四、后置最终通知:使用org.aspectj.lang.annotation 包下的@After注解声明;
value:指定切入点表达式或命名切入点;
argNames:与Schema方式配置中的同义;
其中测试代码与Schema方式几乎一样,在此就不演示了,如果需要请参考AopTest.java中的testAnnotationAfterFinallyAdvice测试方法。
五、环绕通知:使用org.aspectj.lang.annotation 包下的@Around注解声明;
value:指定切入点表达式或命名切入点;
argNames:与Schema方式配置中的同义;
其中测试代码与Schema方式几乎一样,在此就不演示了,如果需要请参考AopTest.java中的annotationAroundAdviceTest测试方法。
6.4.5 引入
@AspectJ风格的引入声明在切面中使用org.aspectj.lang.annotation包下的@DeclareParents声明:
value:匹配需要引入接口的目标对象的AspectJ语法类型表达式;与Schema方式中的types-matching属性同义;
private Interface interface:指定需要引入的接口;
defaultImpl:指定引入接口的默认实现类,没有与Schema方式中的delegate-ref属性同义的定义方式;
其中测试代码与Schema方式几乎一样,在此就不演示了,如果需要请参考AopTest.java中的testAnnotationIntroduction测试方法。
- SpringAOP实例之简单实现(Annotation形式)
- SpringAOP实例之简单实现(Annotation形式)
- SpringAOP实例之简单实现(xml形式)
- SpringAOP实例自定义Annotation形式详解
- springAOP的简单实现
- SpringAOP--Annotation
- spring中动态代理的实现(annotation形式实现)
- springAOP实现之动态代理
- spring 使用annotation替代xml配置实例(spring实现mail简单实现)
- SpringAOP之我的理解(二)------具体实现
- SpringAop的注解形式
- SpringAop事务注解形式
- SpringAOP过滤目标方法的简单实现
- Hibernate Annotation简单实现
- Hibernate Annotation简单实现
- Spring AOP annotation 简单实例
- SpringAOP面向切面详解(带实例)
- Spring架构详解之SpringAOP如何实现
- Android中XML文件的读写
- jxl操作excel表格
- 安卓应用开发Eclipse目录结构
- Java调用批处理文件(.bat)
- 第一次作业(4.C语言)
- SpringAOP实例之简单实现(Annotation形式)
- DB2中使用事件监控器抓取锁超时、死锁信息
- iOS常用的一些库
- 课程练习二-1013-A strange lift
- 缺页中断——FIFO、LRU、OPT这三种置换算法
- innodb_flush_log_at_trx_commit理解!
- android 布局间设置分隔线
- TCP三次握手、四次挥手及四层模型
- HDU1518 DFS Square