Spring-AOP @AspectJ切点函数之@annotation()
@annotation()概述
@annotation表示标注了某个注解的所有方法。
下面通过一个实例说明@annotation()的用法。 AnnotationTestAspect定义了一个后置切面增强,该增强将应用到标注了NeedTest的目标方法中。
实例
代码已托管到Github—> https://github.com/yangshangwei/SpringMaster
首先我们先自定义一个注解@NeedTest。
如何自定义注解请参考Java-Java5.0注解解读
package com.xgj.aop.spring.advisor.aspectJ.function; import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; /**
*
*
* @ClassName: NeedTest
*
* @Description: 自定义注解@NeedTest
*
* @author: Mr.Yang
*
* @date: 2017年8月26日 下午11:19:12
*/ // 声明注解的保留期限
@Retention(RetentionPolicy.RUNTIME)
// 声明可以使用该注解的目标类型
@Target(ElementType.METHOD)
@Documented
public @interface NeedTest {
// 声明注解成员
boolean value() default false;
}
下面我们定义接口 Waiter
package com.xgj.aop.spring.advisor.aspectJ.function;
public interface Waiter {
public void greetTo(String clientName);
public void serverTo(String clientName);
}
接口实现类 两个NaiveWaiter 和 NaughtWaiter
package com.xgj.aop.spring.advisor.aspectJ.function;
public class NaiveWaiter implements Waiter {
@NeedTest(true)
@Override
public void greetTo(String clientName) {
System.out.println("NaiveWaiter:greet to " + clientName);
}
@Override
public void serverTo(String clientName) {
System.out.println("NaiveWaiter:server to " + clientName);
}
public void smile(String clientName, int times) {
System.out.println("NaiveWaiter:smile to " + clientName + " " + times
+ " times");
}
}
package com.xgj.aop.spring.advisor.aspectJ.function;
public class NaughtWaiter implements Waiter {
@Override
public void greetTo(String clientName) {
System.out.println("NaughtWaiter:greet to " + clientName);
}
@NeedTest(true)
@Override
public void serverTo(String clientName) {
System.out.println("NaughtWaiter:server to " + clientName);
}
public void joke(String clientName, int times) {
System.out.println("NaughtyWaiter:play " + times + " jokes to "
+ clientName);
}
}
我们可以看到 NaiveWaiter#greetTo()方法标注了@NeedTest, NaughtWaiter#serverTo()也标注了@NeedTest,我们的目标就是将后置增强织入到这两个标注了@NeedTest的方法中。
接下来编写切面的横切逻辑
package com.xgj.aop.spring.advisor.aspectJ.function.annotationFun; import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect; /**
*
*
* @ClassName: AnnotationTestAspect
*
* @Description: 切面 、 后置增强 ,@annotation表示标注了某个注解的所有方法
*
* @author: Mr.Yang
*
* @date: 2017年8月26日 下午11:23:53
*/ @Aspect
public class AnnotationTestAspect { @AfterReturning("@annotation(com.xgj.aop.spring.advisor.aspectJ.function.NeedTest)")
public void needTest() {
System.out.println("needTest() executed,some logic is here");
} }
接下来通过Spring自动应用切面,配置文件如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 使用基于Schema的aop命名空间进行配置 -->
<!-- 基于@AspectJ切面的驱动器 -->
<aop:aspectj-autoproxy/>
<!-- 目标Bean -->
<bean id="naiveWaiter" class="com.xgj.aop.spring.advisor.aspectJ.function.NaiveWaiter"/>
<bean id="naughtWaiter" class="com.xgj.aop.spring.advisor.aspectJ.function.NaughtWaiter"/>
<!-- 使用了@AspectJ注解的切面类 -->
<bean class="com.xgj.aop.spring.advisor.aspectJ.function.annotationFun.AnnotationTestAspect"/>
</beans>
最后编写测试代码:
package com.xgj.aop.spring.advisor.aspectJ.function.annotationFun; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.xgj.aop.spring.advisor.aspectJ.function.Waiter; public class AnnotationTestAspcetTest { @Test
public void test() {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"com/xgj/aop/spring/advisor/aspectJ/function/annotationFun/conf-annotation.xml"); // 必须是接口类型,否则抛类型转换异常
Waiter waiter = (Waiter) ctx.getBean("naiveWaiter"); // 因为greetTo标注了@NeedTest,因此会被后置增强
waiter.greetTo("XiaoGongJiang");
waiter.serverTo("XiaoGongJiang"); Waiter naughtWaiter = (Waiter) ctx.getBean("naughtWaiter");
// serverTo标注了@NeedTest,因此会被后置增强
naughtWaiter.serverTo("XiaoGongJiang");
}
}
运行结果:
2017-08-27 01:24:22,551 INFO [main] (AbstractApplicationContext.java:583) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6ac604: startup date [Sun Aug 27 01:24:22 BOT 2017]; root of context hierarchy
2017-08-27 01:24:22,647 INFO [main] (XmlBeanDefinitionReader.java:317) - Loading XML bean definitions from class path resource [com/xgj/aop/spring/advisor/aspectJ/function/annotationFun/conf-annotation.xml]
NaiveWaiter:greet to XiaoGongJiang
needTest() executed,some logic is here
NaiveWaiter:server to XiaoGongJiang
NaughtWaiter:server to XiaoGongJiang
needTest() executed,some logic is here
从输出结果中可以看出,切面被正确的织入到了标注有@NeedTest注解的方法中。
Spring-AOP @AspectJ切点函数之@annotation()的更多相关文章
- Spring AOP + AspectJ annotation example
In this tutorial, we show you how to integrate AspectJ annotation with Spring AOP framework. In simp ...
- Spring AOP + AspectJ Annotation Example---reference
In this tutorial, we show you how to integrate AspectJ annotation with Spring AOP framework. In simp ...
- 关于 Spring AOP (AspectJ) 该知晓的一切
关联文章: 关于Spring IOC (DI-依赖注入)你需要知道的一切 关于 Spring AOP (AspectJ) 你该知晓的一切 本篇是年后第一篇博文,由于博主用了不少时间在构思这篇博文,加上 ...
- 关于 Spring AOP (AspectJ) 你该知晓的一切
版权声明:本文为CSDN博主「zejian_」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明.原文链接:https://blog.csdn.net/javazej ...
- Spring学习(十八)----- Spring AOP+AspectJ注解实例
我们将向你展示如何将AspectJ注解集成到Spring AOP框架.在这个Spring AOP+ AspectJ 示例中,让您轻松实现拦截方法. 常见AspectJ的注解: @Before – 方法 ...
- 关于 Spring AOP (AspectJ) 你该知晓的一切 (转)
出处:关于 Spring AOP (AspectJ) 你该知晓的一切
- 基于@AspectJ和schema的aop(三)---切点函数详解
切点函数是AspectJ表达式语言的核心, 也是使用@AspectJ进行切面定义的难点.本小节我们通过具体的实例对切点函数进行深入学习. 1.@annotation() @annotation()表示 ...
- Spring AOP @AspectJ 入门基础
需要的类包: 1.一个简单的例子 Waiter接口: package com.yyq.annotation; public interface Waiter { void greetTo(String ...
- spring AOP AspectJ 定义切面实现拦截
总结记录一下AOP常用的应用场景及使用方式,如有错误,请留言. 1. 讲AOP之前,先来总结web项目的几种拦截方式 A: 过滤器 使用过滤器可以过滤URL请求,以及请求和响应的信息,但是过 ...
随机推荐
- 【Flask】 python学习第一章 - 创建与运行参数
windos 创建环境 sudo pip install virtualenv # 安装virtualenv virtualenv -p python dir_name cd dir_name p ...
- (Linux基础学习)第五章:Linux中的screen应用
第1节:安装screen1.加载系统镜像文件,因为screen的安装包在系统镜像文件中图001 2.列出系统上所有的磁盘[root@centos6 ~]# lsblk图002 3.安装screen应用 ...
- netty实现websocket发送文本和二进制数据
原文:https://huan1993.iteye.com/blog/2433552 最近在学习netty相关的知识,看到netty可以实现 websoket,因此记录一下在netty中实现webso ...
- MVC模式:action、dao、model、service、util
这就是一个典型的MVC: action:主要是Struts2,用来做跳转,比如jsp页面提交的表单就是进入到action里面,然后action再调用service里面的逻辑,最后返回到jsp响应请求. ...
- 《发际线总是和我作队》第九次团队作业:Beta冲刺Scrum Meeting2
项目 内容 这个作业属于哪个课程 软件工程 这个作业的要求在哪里 实验十三 团队作业9:Beta冲刺与团队项目冲刺 团队名称 发际线总和我作队 作业学习目标 (1)掌握软件黑盒测试技术:(2)掌握软件 ...
- html页面标记 点击目录跳转到页面相应位置 简易回到顶部
html页面标记 点击目录跳转到页面相应位置 简易回到顶部 参考博客:
- LightOJ - 1236 - Pairs Forming LCM(唯一分解定理)
链接: https://vjudge.net/problem/LightOJ-1236 题意: Find the result of the following code: long long pai ...
- 使用unsafe.Pointer将结构体转为[]byte
package main import ( "fmt" "unsafe" ) type TestStructTobytes struct { data int6 ...
- xml文件整理
xml 97-2003 格式 \s*\n\s*\n\s*\n\s*\n\n(^个人补充信息.*)\n(.*)\n(^总成绩.*)$1$2\n$3(^个人补充信息.*)\n(.*)\n(.*)\n(^总 ...
- Tensorflow细节-P190-输入文件队列
以下代码要学会几个地方 1.filename = ('data.tfrecords-%.5d-of-%.5d' % (i, num_shards)) 这个东西就是要会data.tfrecords-%. ...