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 ...
随机推荐
- EOF是什么?(笔记)
一.参考文章 1.EOF是什么?(阮一峰网络日志) 2.Linux 中的 EOF 到底是什么 二.知识点 1.EOF 定义在 /usr/include/stdio.h 文件中: 从上面 EOF 的定义 ...
- javascript中的location的用法
javascript中的location.href有很多种用法,主要如下. self.location.href="/url" 当前页面打开URL页面 location.href= ...
- IT培训行业变革大会,7月11日启程!
自上世纪八十年代PC时代起,IT行业走过了以2G移动网络和宽带网络.PC终端为主要载体,软件产品.应用软件和门户网站为特征产品的PC互联网时代. 以3/4G移动和高速宽带和移动终端为主要载体,移动支付 ...
- UML-逻辑架构&包图-相关概念
1.逻辑架构 软件的宏观组织结构.含: 1).包 2).子系统 3).层 2.层 对类.包.子系统的分组(内聚).例如:mvc.在OOA/D中要重点关注核心应用逻辑(或领域)层. 3.UML包图 描述 ...
- MySQL学习笔记——〇三 MySQL习题
在前面讲了MySQL的初步使用方法以后,在这里放出来一些案例来看看怎么做. 先看看database的结构,一共5个表 外键关系: class的cid是student的class_id的外键,teach ...
- Maven--归类依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- ios ktvhttpcache 音视频缓存插件使用
1.PodFile 文件增加 pod 'KTVHTTPCache', '~> 2.0.0' 2.在终端 需要先cd到podfile文件所在目录 执行pod install 3.在header ...
- windows server 2012 安装sql server集群
第一步:准备工作 虚拟环境下模拟创建: 准备好3台虚拟机 操作系统,WindowsServer2012R2 操作系统安装完成后,需要注意如果虚拟机是克隆出来的,后面操作集群的时候需要计算机的sid不同 ...
- 微信小程序java8 java7 java6 encryptedData 解密 异常处理
使用java8 java7 java6 解密微信小程序encryptedData可以回遇到一些错误 1.java.security.NoSuchAlgorithmException: Cannot ...
- python的稀疏矩阵计算
尽量避免稀疏矩阵, 加快计算. 比如计算稀疏矩阵S的F范数 a = norm(S, 'fro'), 方法1效率比方法2高很多. 方法 1 import numpy as np a = np.linal ...