Spring:使用Spring AOP时,如何获取目标方法上的注解
当使用spring AOP时,判断目标方法上的注解进行相关操作,如缓存,认证权限等
自定义注解
package com.agent.annotation; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import org.springframework.stereotype.Component; @Component
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyAnnotation { public boolean isEnable() default true;
}
Spring AOP的AspectJ
package com.agent.aop; import java.lang.reflect.Method; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component; import com.agent.annotation.MyAnnotation; @Component
@Aspect
public class LogUtil { @Around("@annotation(com.agent.annotation.MyAnnotation)")
public Object logWrited(ProceedingJoinPoint point) throws Throwable { Object[] args = point.getArgs();
Class<?>[] argTypes = new Class[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()); return point.proceed();
} }
Service接口
package com.agent.service;
public interface UserService {
void addUser(String name, String password);
}
service接口的实现类,被自定义注解所注解
package com.agent.service.impl; import org.springframework.stereotype.Service; import com.agent.annotation.MyAnnotation;
import com.agent.service.UserService; @Service(value="userService")
public class UserServiceImpl implements UserService { @Override
@MyAnnotation
public void addUser(String name, String password) {
System.out.println("UserServiceImpl.addUser()...... name: " + name + "; password: " + password);
} }
测试类:
package com.agent.test; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.agent.service.UserService; public class AOPTest { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService us = (UserService)ac.getBean("userService");
us.addUser("张三", "188");
} }
Spring的配置文件:
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
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"> <context:component-scan base-package="com.agent" /> <!-- <bean id="aspect" class="com.agent.aop.LogUtil" />
<aop:config>
<aop:aspect ref="aspect">
<aop:pointcut expression="execution(* add*(..))" id="mypointcut"/>
<aop:after method="logWrited" pointcut-ref="mypointcut"/>
<aop:around method="logWrited" pointcut-ref="mypointcut" />
</aop:aspect>
</aop:config>
-->
<aop:aspectj-autoproxy/>
</beans>
测试结果:

如果使用的是接口的模式,而注解在实现类上,则不能使用如下方式获取目标方法的对象,因为该方式获取的是该类的接口或者顶级父类的方法的对象
MethodSignature methodSignature = (MethodSignature)point.getSignature();
Method method = methodSignature.getMethod();
Spring:使用Spring AOP时,如何获取目标方法上的注解的更多相关文章
- AOP中ProceedingJoinPoint获取目标方法,参数,注解
private void saveLog(ProceedingJoinPoint jp,long time)throws Throwable { package com.cy.pj.common.as ...
- 学习spring第6天(aop获取目标方法参数)
关于<aop:around>中的方法,需要第一个参数为ProceedJoinPoint,在方法体中通过该参数调用proceed()才能使目标方法得到调用. 当一个切面中有多个<aop ...
- spring aop获取目标对象的方法对象(包括方法上的注解)
这两天在学习权限控制模块.以前看过传智播客黎活明老师的巴巴运动网视频教程,里面就讲到权限控制的解决方案,当时也只是看看视频,没有动手实践,虽说看过几遍,可是对于系统中的权限控制还是很迷茫,所以借着这次 ...
- thinkphp5 查询的数据是对象时,获取原始数据方法
获取原始数据 如果你定义了获取器的情况下,希望获取数据表中的原始数据,可以使用: $user = User::get(1); // 通过获取器获取字段 echo $user->status; / ...
- SpringMVC + Spring + MyBatis 学习笔记:在类和方法上都使用RequestMapping如何访问
系统:WIN8.1 数据库:Oracle 11GR2 开发工具:MyEclipse 8.6 框架:Spring3.2.9.SpringMVC3.2.9.MyBatis3.2.8 先看代码: @Requ ...
- 黑马-Spring(IOC&DI) AOP
IOC(控制翻转) 概念 把对象的创建.初始化.销毁等工作交给spring容器来做 案例 环境 步骤 1. 写一个HelloWorld类 2. 写一个配置文件 把hello类放到spring容 ...
- spring5 源码深度解析----- 创建AOP代理之获取增强器
在上一篇的博文中我们讲解了通过自定义配置完成了对AnnotationAwareAspectJAutoProxyCreator类型的自动注册,那么这个类到底做了什么工作来完成AOP的操作呢?首先我们看看 ...
- Spring学习之Aop的各种增强方法
AspectJ允许使用注解用于定义切面.切入点和增强处理,而Spring框架则可以识别并根据这些注解来生成AOP代理.Spring只是使用了和AspectJ 5一样的注解,但并没有使用AspectJ的 ...
- Spring中的AOP 专题
Caused by: java.lang.IllegalArgumentException: ProceedingJoinPoint is only supported for around advi ...
随机推荐
- PHP的isset()函数 一般用来检测变量是否设置
PHP的isset()函数 一般用来检测变量是否设置 格式:bool isset ( mixed var [, mixed var [, ...]] ) 功能:检测变量是否设置 返回值: 若变量不存在 ...
- 邮件发送,无尽的501错误。TCP发送邮件解决方案
先贴上错误信息,便于搜索引擎采集,也送给遇到此问题的技术朋友们. smtp 501 Syntax error (no parameters allowed) (#5.5.4) 背景描述: 使用TCP发 ...
- Properties in Algebra
附录-Properties in Algebra 部分证明转载自标注,仅作个人整理查阅用. 范数 (norm) \(^{[1]}\) 要更好的理解范数,就要从函数.几何与矩阵的角度去理解,我尽量讲的通 ...
- MySql 相关面试题
1.mysql 慢查询 目的:通过慢查询日志,记录超过指定时间的 SQL 语句,优化 sql 查询 步骤:查看慢查询开启状态-->设置慢查询 http://www.cnblogs.com/luy ...
- bzoj3218 a+b Problem(最小割+主席树优化建边)
由于6.22博主要学测,大半时间学文化课,近期刷题量&写题解的数量会急剧下降. 这题出得挺经典的,首先一眼最小割,考虑朴素的做法:与S联通表示白色,与T联通表示黑色,S向i连流量为w[i]的边 ...
- request请求生命周期
request请求生命周期 一.request请求分析 1.1. request数据请求 # views.py from rest_framework.views import APIView fro ...
- goweb-访问数据库
访问数据库 对许多Web应用程序而言,数据库都是其核心所在.数据库几乎可以用来存储你想查询和修改的任何信息,比如用户信息.产品目录或者新闻列表等. Go没有内置的驱动支持任何的数据库,但是Go定义了d ...
- java常用工具类(三)
一.连接数据库的综合类 package com.itjh.javaUtil; import java.sql.Connection; import java.sql.DriverManager; im ...
- TPO6-1Powering the Industrial Revolution
The source had long been known but not exploited. Early in the eighteenth century, a pump had come i ...
- 【shell】概述
功能简介 批量自动初始化系统(update,软件安装,时区设置,安全策略...) 批量自动部署软件(LAMP,LNMP,Nginx,LVS,Tomcat) 管理应用程序(KVM,集群管理扩容,MySQ ...