当使用spring AOP时,判断目标方法上的注解进行相关操作,如缓存,认证权限等

自定义注解

packagecom.agent.annotation;importjava.lang.annotation.ElementType;importjava.lang.annotation.Retention;importjava.lang.annotation.RetentionPolicy;importjava.lang.annotation.Target;importorg.springframework.stereotype.Component;

@Component

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.METHOD)public @interfaceMyAnnotation {public boolean isEnable() default true;

}

Spring AOP的AspectJ

packagecom.agent.aop;importjava.lang.reflect.Method;importorg.aspectj.lang.ProceedingJoinPoint;importorg.aspectj.lang.annotation.Around;importorg.aspectj.lang.annotation.Aspect;importorg.springframework.stereotype.Component;importcom.agent.annotation.MyAnnotation;

@Component

@Aspectpublic classLogUtil {

@Around("@annotation(com.agent.annotation.MyAnnotation)")public Object logWrited(ProceedingJoinPoint point) throwsThrowable {

Object[] args=point.getArgs();

Class>[] argTypes = newClass[point.getArgs().length];for (int i = 0; i < args.length; i++) {

argTypes[i]=args[i].getClass();

}

Method method= null;try{

method=point.getTarget().getClass()

.getMethod(point.getSignature().getName(), argTypes);

}catch(NoSuchMethodException e) {

e.printStackTrace();

}catch(SecurityException e) {

e.printStackTrace();

}

MyAnnotation ma= method.getAnnotation(MyAnnotation.class);

System.out.println(ma.isEnable());returnpoint.proceed();

}

}

Service接口

packagecom.agent.service;public interfaceUserService {voidaddUser(String name, String password);

}

service接口的实现类,被自定义注解所注解

packagecom.agent.service.impl;importorg.springframework.stereotype.Service;importcom.agent.annotation.MyAnnotation;importcom.agent.service.UserService;

@Service(value="userService")public class UserServiceImpl implementsUserService {

@Override

@MyAnnotationpublic voidaddUser(String name, String password) {

System.out.println("UserServiceImpl.addUser()...... name: " + name + "; password: " +password);

}

}

测试类:

packagecom.agent.test;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;importcom.agent.service.UserService;public classAOPTest {public static voidmain(String[] args) {

ApplicationContext ac= new ClassPathXmlApplicationContext("applicationContext.xml");

UserService us= (UserService)ac.getBean("userService");

us.addUser("张三", "188");

}

}

Spring的配置文件:

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx.xsd">

测试结果:

如果使用的是接口的模式,而注解在实现类上,则不能使用如下方式获取目标方法的对象,因为该方式获取的是该类的接口或者顶级父类的方法的对象

MethodSignature methodSignature =(MethodSignature)point.getSignature();

Method method= methodSignature.getMethod();

java获取方法上的注解_Spring:使用Spring AOP时,如何获取目标方法上的注解相关推荐

  1. java around_java - 使用Spring AOP时,在单个连接点上具有参数绑定的多个Around建议会导致错误 - 堆栈内存溢出...

    我在一个方法上写了2条注释,并在2条周围建议中处理了每个注释值. 连接点方法如下: @CacheFetch(cacheName = CacheManager.CACHE_DATASOURCE_INFO ...

  2. 基于Spring AOP的统一响应体的实现(注解版)

    基于Spring AOP的统一响应体的实现(注解版) 一.前言 在上一篇系列中 我们 统一参数校验,统一结果响应,统一异常处理,统一错误处理,统一日志记录,统一生成api文档, 对于统一数据响应返回规 ...

  3. preg_match 参数获取两个_「死磕 Spring」—– IOC 之 获取 Document 对象

    原文出自:http://cmsblogs.com作者:chenssy 在 XmlBeanDefinitionReader.doLoadDocument() 方法中做了两件事情,一是调用 getVali ...

  4. java方法设置切点_java相关:Spring AOP中定义切点的实现方法示例

    java相关:Spring AOP中定义切点的实现方法示例 发布于 2020-6-6| 复制链接 摘记: 本文实例讲述了Spring AOP中定义切点的实现方法.分享给大家供大家参考,具体如下:一 配 ...

  5. 在线记录源码调试之@EnableAspectJAutoProxy与Spring AOP(三)调用被代理方法userServiceImpl.save(user)

    测试用例1 连接点 @Component public class UserServiceImpl implements UserService {@Overridepublic void save( ...

  6. AOP开发——在不修改源代码的前提下,对类里面的方法进行增强 : 前置 后置 环绕 异常||如何得到目标方法的参数和返回值

    AOP开发 @Transactionl 在不修改源代码的前提下,对类里面的方法进行增强 前置 后置 环绕 异常 创建项目前引入aop的包 <dependency>     <grou ...

  7. erp系统服务器连不上客户端,安装ERP企业管理软件客户端时个别电脑连不上服务器...

    安装泛普软件ERP企业管理软件客户端时个别电脑连不上服务器, 提示服务器未正确配制 解决方案: 1) 检查数据库SQL 2000是否启动;是否打过SP4补丁(版本号为8.00.2039). 2) Sv ...

  8. 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(六): 一些常用的数据库 注解...

    一. 实体 Bean 每个持久化POJO类都是一个实体Bean, 通过在类的定义中使用 @Entity 注解来进行声明. 声明实体Bean @Entity public class Flight im ...

  9. java实现微信服务(公众)号用户关注时,获取openid,安全模式下的加密解密实现

    大家好,我是雄雄,欢迎你的到来~ 前言 需求是这样的,当用户关注公司服务(公众)号时,就自动注册成为了会员,且通过小程序设置提醒,然后后台可以通过服务(公众)号给用户推送模板信息. 实现方式(java ...

最新文章

  1. View绘制流程的入口
  2. axure按钮切换颜色_如何用Axure画出Web产品的列表组件:基础画法
  3. DSVS7050签名服务器的网站,吉大正元数字签名服务器-安装部署手册(COM版 VCTK_S接口)2.1.1.doc...
  4. socket read阻塞_go tcp中的ioutil.ReadAll阻塞的问题
  5. ASP.NET XML Web 服务的工作流解决方案
  6. 计算机基础知识与基本操作txt,计算机基础知识复习-20210707023705.pdf-原创力文档...
  7. 如何将自开发的SAP UI5应用以tile的方式配置到SAP Fiori Launchpad里
  8. 动手学pytorch之通俗易懂何为卷积-深度AI科普团队
  9. 机器学习的训练数据(Training Dataset)、测试数据(Testing Dataset)和验证数据(Validation Dataset)
  10. MongoDB可视化工具MongoChef永久有效
  11. 游戏帐号被盗,但是杀毒软件没有发现病毒?DNS挟持盗号原理剖析
  12. 实变函数自制笔记6:初识可测函数
  13. 获取计算机用户名,java获取计算机用户名
  14. 由边际成本引发的思考
  15. 本地SecureCRT连接VMware中XUbuntu
  16. (Emitted value instead of an instance of Error) Do not use v-for index as key on <transition-group>
  17. 深度学习机器学习面试题(理论基础)
  18. 知云文献翻译打不开_有了这几个软件,轻松读懂英文文献
  19. 服务器复制文件出现io错误,win7系统复制文件时发生IO错误的解决方法
  20. 渤海船舶职业学院计算机应用,我院在第十一届全国大学生计算机应用能力与信息素养大赛中取得佳绩...

热门文章

  1. hdu4982 暴搜+剪枝(k个数和是n,k-1个数的和是平方数)
  2. 【错误记录】PyCharm 运行 Python 程序报错 ( PEP 8: E402 module level import not at top of file )
  3. 【Java 虚拟机原理】Class 字节码二进制文件分析 一 ( 字节码文件附加信息 | 魔数 | 次版本号 | 主版本号 | 常量池个数 )
  4. 【设计模式】命令模式 ( 简介 | 适用场景 | 优缺点 | 代码示例 )
  5. JVM 参数及各部分含义(转)
  6. 一站式机器学习平台TI-ONE是什么?——云+未来峰会开发者专场回顾
  7. [算法题] Search in Rotated Sorted Array ii
  8. 《转》八大算法详细讲解
  9. Eclipse Class Decompiler---Java反编译插件
  10. Fedora安装Texlive2013时出现Can't locate Digest/MD5.pm的解决方法