经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Spring » 查看文章
Spring-AOP @AspectJ切点函数之@annotation()用法
来源:jb51  时间:2021/7/19 13:31:09  对本文有异议

@annotation()概述

@annotation表示标注了某个注解的所有方法。

下面通过一个实例说明@annotation()的用法。 AnnotationTestAspect定义了一个后置切面增强,该增强将应用到标注了NeedTest的目标方法中。

实例

代码已托管到Github—> https://github.com/yangshangwei/SpringMaster

这里写图片描述

首先我们先自定义一个注解@NeedTest。

如何自定义注解请参考Java-Java5.0注解解读

  1. package com.xgj.aop.spring.advisor.aspectJ.function;
  2. import java.lang.annotation.Documented;
  3. import java.lang.annotation.ElementType;
  4. import java.lang.annotation.Retention;
  5. import java.lang.annotation.RetentionPolicy;
  6. import java.lang.annotation.Target;
  7. /**
  8. *
  9. *
  10. * @ClassName: NeedTest
  11. *
  12. * @Description: 自定义注解@NeedTest
  13. *
  14. * @author: Mr.Yang
  15. *
  16. * @date: 2017年8月26日 下午11:19:12
  17. */
  18. // 声明注解的保留期限
  19. @Retention(RetentionPolicy.RUNTIME)
  20. // 声明可以使用该注解的目标类型
  21. @Target(ElementType.METHOD)
  22. @Documented
  23. public @interface NeedTest {
  24. // 声明注解成员
  25. boolean value() default false;
  26. }

下面我们定义接口 Waiter

  1. package com.xgj.aop.spring.advisor.aspectJ.function;
  2. public interface Waiter {
  3. public void greetTo(String clientName);
  4. public void serverTo(String clientName);
  5. }

接口实现类 两个NaiveWaiter 和 NaughtWaiter

  1. package com.xgj.aop.spring.advisor.aspectJ.function;
  2. public class NaiveWaiter implements Waiter {
  3. @NeedTest(true)
  4. @Override
  5. public void greetTo(String clientName) {
  6. System.out.println("NaiveWaiter:greet to " + clientName);
  7. }
  8. @Override
  9. public void serverTo(String clientName) {
  10. System.out.println("NaiveWaiter:server to " + clientName);
  11. }
  12. public void smile(String clientName, int times) {
  13. System.out.println("NaiveWaiter:smile to " + clientName + " " + times
  14. + " times");
  15. }
  16. }
  1. package com.xgj.aop.spring.advisor.aspectJ.function;
  2. public class NaughtWaiter implements Waiter {
  3. @Override
  4. public void greetTo(String clientName) {
  5. System.out.println("NaughtWaiter:greet to " + clientName);
  6. }
  7. @NeedTest(true)
  8. @Override
  9. public void serverTo(String clientName) {
  10. System.out.println("NaughtWaiter:server to " + clientName);
  11. }
  12. public void joke(String clientName, int times) {
  13. System.out.println("NaughtyWaiter:play " + times + " jokes to "
  14. + clientName);
  15. }
  16. }

我们可以看到 NaiveWaiter#greetTo()方法标注了@NeedTest, NaughtWaiter#serverTo()也标注了@NeedTest,我们的目标就是将后置增强织入到这两个标注了@NeedTest的方法中。

接下来编写切面的横切逻辑

  1. package com.xgj.aop.spring.advisor.aspectJ.function.annotationFun;
  2. import org.aspectj.lang.annotation.AfterReturning;
  3. import org.aspectj.lang.annotation.Aspect;
  4. /**
  5. *
  6. *
  7. * @ClassName: AnnotationTestAspect
  8. *
  9. * @Description: 切面 、 后置增强 ,@annotation表示标注了某个注解的所有方法
  10. *
  11. * @author: Mr.Yang
  12. *
  13. * @date: 2017年8月26日 下午11:23:53
  14. */
  15. @Aspect
  16. public class AnnotationTestAspect {
  17. @AfterReturning("@annotation(com.xgj.aop.spring.advisor.aspectJ.function.NeedTest)")
  18. public void needTest() {
  19. System.out.println("needTest() executed,some logic is here");
  20. }
  21. }

接下来通过Spring自动应用切面,配置文件如下

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:aop="http://www.springframework.org/schema/aop"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/aop
  8. http://www.springframework.org/schema/aop/spring-aop.xsd">
  9. <!-- 使用基于Schema的aop命名空间进行配置 -->
  10. <!-- 基于@AspectJ切面的驱动器 -->
  11. <aop:aspectj-autoproxy/>
  12. <!-- 目标Bean -->
  13. <bean id="naiveWaiter" class="com.xgj.aop.spring.advisor.aspectJ.function.NaiveWaiter"/>
  14. <bean id="naughtWaiter" class="com.xgj.aop.spring.advisor.aspectJ.function.NaughtWaiter"/>
  15. <!-- 使用了@AspectJ注解的切面类 -->
  16. <bean class="com.xgj.aop.spring.advisor.aspectJ.function.annotationFun.AnnotationTestAspect"/>
  17. </beans>

最后编写测试代码:

  1. package com.xgj.aop.spring.advisor.aspectJ.function.annotationFun;
  2. import org.junit.Test;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. import com.xgj.aop.spring.advisor.aspectJ.function.Waiter;
  6. public class AnnotationTestAspcetTest {
  7. @Test
  8. public void test() {
  9. ApplicationContext ctx = new ClassPathXmlApplicationContext(
  10. "com/xgj/aop/spring/advisor/aspectJ/function/annotationFun/conf-annotation.xml");
  11. // 必须是接口类型,否则抛类型转换异常
  12. Waiter waiter = (Waiter) ctx.getBean("naiveWaiter");
  13. // 因为greetTo标注了@NeedTest,因此会被后置增强
  14. waiter.greetTo("XiaoGongJiang");
  15. waiter.serverTo("XiaoGongJiang");
  16. Waiter naughtWaiter = (Waiter) ctx.getBean("naughtWaiter");
  17. // serverTo标注了@NeedTest,因此会被后置增强
  18. naughtWaiter.serverTo("XiaoGongJiang");
  19. }
  20. }

运行结果:

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注解的方法中。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号