@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()的更多相关文章

  1. Spring AOP + AspectJ annotation example

    In this tutorial, we show you how to integrate AspectJ annotation with Spring AOP framework. In simp ...

  2. Spring AOP + AspectJ Annotation Example---reference

    In this tutorial, we show you how to integrate AspectJ annotation with Spring AOP framework. In simp ...

  3. 关于 Spring AOP (AspectJ) 该知晓的一切

    关联文章: 关于Spring IOC (DI-依赖注入)你需要知道的一切 关于 Spring AOP (AspectJ) 你该知晓的一切 本篇是年后第一篇博文,由于博主用了不少时间在构思这篇博文,加上 ...

  4. 关于 Spring AOP (AspectJ) 你该知晓的一切

    版权声明:本文为CSDN博主「zejian_」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明.原文链接:https://blog.csdn.net/javazej ...

  5. Spring学习(十八)----- Spring AOP+AspectJ注解实例

    我们将向你展示如何将AspectJ注解集成到Spring AOP框架.在这个Spring AOP+ AspectJ 示例中,让您轻松实现拦截方法. 常见AspectJ的注解: @Before – 方法 ...

  6. 关于 Spring AOP (AspectJ) 你该知晓的一切 (转)

    出处:关于 Spring AOP (AspectJ) 你该知晓的一切

  7. 基于@AspectJ和schema的aop(三)---切点函数详解

    切点函数是AspectJ表达式语言的核心, 也是使用@AspectJ进行切面定义的难点.本小节我们通过具体的实例对切点函数进行深入学习. 1.@annotation() @annotation()表示 ...

  8. Spring AOP @AspectJ 入门基础

    需要的类包: 1.一个简单的例子 Waiter接口: package com.yyq.annotation; public interface Waiter { void greetTo(String ...

  9. spring AOP AspectJ 定义切面实现拦截

    总结记录一下AOP常用的应用场景及使用方式,如有错误,请留言. 1.  讲AOP之前,先来总结web项目的几种拦截方式    A:  过滤器 使用过滤器可以过滤URL请求,以及请求和响应的信息,但是过 ...

随机推荐

  1. 在openwrt上使用autossh(已放弃)

    用了一天后发现,这东西真不靠谱,还不如自已写的SHELL检测重连来的精准和方便,放弃中 参考文章: https://my.oschina.net/umu618/blog/849345 https:// ...

  2. WebForm SignalR 实时消息推送

    原文:https://www.jianshu.com/p/ae25d0d77011 官方文档:https://docs.microsoft.com/zh-cn/aspnet/signalr/ 实现效果 ...

  3. Lombok的使用详解(最详尽的解释,覆盖讲解所有可用注解),解决@Builder.Default默认值问题

    原文:https://blog.csdn.net/f641385712/article/details/82081900 前言 Lombok是一款Java开发插件,使得Java开发者可以通过其定义的一 ...

  4. homebrew一直处于updating状态

    vim ~/.bash_profile 增加一行 export HOMEBREW_NO_AUTO_UPDATE=true 之后再source一下

  5. CentOS 7 修改时区例如上海时区

    Linux 系统(我特指发行版, 没说内核) 下大部分软件的风格就是不会仔细去考虑向后 的兼容性, 比如你上个版本能用这种程序配置, 没准到了下一个版本, 该程序已经不见了. 比如 sysvinit ...

  6. python 私有和保护成员变量如何实现?—— "单下划线 " 开始的成员变量叫做保护变量,意思是只有类实例和子类实例能访问到这些变量;" 双下划线 " 开始的是私有成员,意思是只有类对象自己能访问,连子类对象也不能访问到这个数据

    默认情况下,Python中的成员函数和成员变量都是公开的(public),在python中没有类似public,private等关键词来修饰成员函数和成员变量.在python中定义私有变量只需要在变量 ...

  7. 怎样制作一个 Python Egg

    from:http://liluo.org/blog/2012/08/how-to-create-python-egg/ 制作打包一个 Python Egg 并部署整个过程还蛮有意思的,下面小教程(这 ...

  8. Btrace打印自定义引用类方法参数

    简介 BTrace是sun公司推出的一款Java 动态.安全追踪(监控)工具,可以在不用重启的情况下监控系统运行情况,方便的获取程序运行时的数据信息,如方法参数.返回值.全局变量和堆栈信息等,并且做到 ...

  9. Linux 服务器性能出问题,排查下这些参数指标

    taozj马哥Linux运维 一个基于 Linux 操作系统的服务器运行的同时,也会表征出各种各样参数信息.通常来说运维人员.系统管理员会对这些数据会极为敏感,但是这些参数对于开发者来说也十分重要,尤 ...

  10. 扫雷小游戏PyQt5开发【附源代码】

    也没啥可介绍哒,扫雷大家都玩过. 雷的分布算法也很简单,就是在雷地图(map:二维数组)中,随机放雷,然后这个雷的8个方位(上下左右.四个对角)的数字(非雷的标记.加一后不为雷的标记)都加一. 如何判 ...