结合spring 实现自定义注解
注解类
import java.lang.annotation.*; /**
* Created by Administrator on 2016/6/28.
*/
//ElementType.METHOD 在方法上使用
@Target(ElementType.METHOD)
//范围
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Cacheable { String key();
String fieldKey() ;
int expireTime() default 1800000;
}
注解实现类
@Aspect
public class CacheAspect { private Logger logger = LoggerFactory.getLogger(CacheAspect.class); public CacheAspect(){
} @Pointcut(value = "execution(@Cacheable * *.*(..))")
public void setCacheRedis(){} /**
* aop实现自定缓存注解
*
* @param joinPoint
* @return
*/
//@Around("@annotation(com.manage.annotations.Cacheable)") 不知道为什么这么写不行
//这个里面的值要上面的方法名一致
@Around("setCacheRedis()")
public Object setCache(ProceedingJoinPoint joinPoint) {
Object result = null; Method method = getMethod(joinPoint); //自定义注解类
Cacheable cacheable = method.getAnnotation(Cacheable.class);
//获取key值
String key = cacheable.key();
String fieldKey=cacheable.fieldKey();
//获取方法的返回类型,让缓存可以返回正确的类型
Class returnType=((MethodSignature)joinPoint.getSignature()).getReturnType();
下面就是根据业务来自行操作
return result;
}
public Method getMethod(ProceedingJoinPoint pjp) {
//获取参数的类型
Object[] args = pjp.getArgs();
Class[] argTypes = new Class[pjp.getArgs().length];
for (int i = 0; i < args.length; i++) {
argTypes[i] = args[i].getClass();
}
Method method = null;
try {
method = pjp.getTarget().getClass().getMethod(pjp.getSignature().getName(), argTypes);
} catch (NoSuchMethodException e) {
logger.error("annotation no sucheMehtod", e);
} catch (SecurityException e) {
logger.error("annotation SecurityException", e);
}
return method; }
}
调用类
@Service
@Transactional
public class UserServiceImpl extends BaseServiceImpl<User, Integer> implements UserService { @Autowired
private UserDaoImpl userDaoImpl; @Override
//key名字自定义 fieldKey可以看Spring EL表达式
@Cacheable(key = "getUser",fieldKey = "#user.getUserName()")
public User getUser(User user) {
List<User> users = userDaoImpl.getUser(user);
if (!users.isEmpty() && users.size() > 0) {
user=users.get(0);
return user;
} else {
return null;
}
}
}
结合spring 实现自定义注解的更多相关文章
- 利用Spring AOP自定义注解解决日志和签名校验
转载:http://www.cnblogs.com/shipengzhi/articles/2716004.html 一.需解决的问题 部分API有签名参数(signature),Passport首先 ...
- spring AOP自定义注解方式实现日志管理
今天继续实现AOP,到这里我个人认为是最灵活,可扩展的方式了,就拿日志管理来说,用Spring AOP 自定义注解形式实现日志管理.废话不多说,直接开始!!! 关于配置我还是的再说一遍. 在appli ...
- spring AOP自定义注解 实现日志管理
今天继续实现AOP,到这里我个人认为是最灵活,可扩展的方式了,就拿日志管理来说,用Spring AOP 自定义注解形式实现日志管理.废话不多说,直接开始!!! 关于配置我还是的再说一遍. 在appli ...
- (转)利用Spring AOP自定义注解解决日志和签名校验
一.需解决的问题 部分API有签名参数(signature),Passport首先对签名进行校验,校验通过才会执行实现方法. 第一种实现方式(Origin):在需要签名校验的接口里写校验的代码,例如: ...
- 使用Spring Aop自定义注解实现自动记录日志
百度加自己琢磨,以下亲测有效,所以写下来记录,也方便自己回顾浏览加深印象之类,有什么问题可以评论一起解决,不完整之处也请大佬指正,一起进步哈哈(1)首先配置文件: <!-- 声明自动为sprin ...
- day05 Spring中自定义注解的用处-之获取自定义的Servie
PS: 在RPC远程调用中,想要获取自定义的service的方法,就得自定义标签遍历拿到方法 PS:在spring中,两个最核心的 概念是aop和ioc,aop其实就是动态代理. ioc 就是解决对象 ...
- Spring aop+自定义注解统一记录用户行为日志
写在前面 本文不涉及过多的Spring aop基本概念以及基本用法介绍,以实际场景使用为主. 场景 我们通常有这样一个需求:打印后台接口请求的具体参数,打印接口请求的最终响应结果,以及记录哪个用户在什 ...
- Spring AOP 自定义注解实现统一日志管理
一.AOP的基本概念: AOP,面向切面编程,常用于日志,事务,权限等业务处理.AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容(Spring核心之一),是函数式编程 ...
- 【spring】自定义注解 custom annotation
自定义注解 custom annotation 使用场景 类属性自动赋值. 验证对象属性完整性. 代替配置文件功能,像spring基于注解的配置. 可以生成文档,像java代码注释中的@see,@pa ...
随机推荐
- nivicat premium连接阿里云数据库
1.首先打开Navicat,文件>新建连接>MySQL连接,其他的如一图所示 其中: 连接名:自己取一个名字 主机名:填写mysql的地址 用户名:mysql的登录的用户名 密码:登录的密 ...
- Flask 的整体流程
Flask 的整体流程 封装 requestContext 对象, full_dispatch_request(视图函数 执行), response返回 从app.run() 开始 -->> ...
- Unity中使用柏林噪声生成地图
孙广东 2017.3.27 http://blog.csdn.NET/u010019717 主要是利用Unity的 Mathf.PerlinNoise 函数(柏林噪声)的不同寻常的功能. htt ...
- linux rm删除含有特殊符号目录或者文件
想要删除time$1.class,用rm time$1.class是不行的,可以用 rm time"$"1.class 删掉 假设Linux系统中有一个文件名叫“-polo”. ...
- HDU2032 杨辉三角
解题思路:不要小看这题水题,如果数据类型没有用long long, 当n开为35时,会出现TLE,而且会报非法内存访问,现在还 不理解为什么,若有高手,请不吝赐教. 上代码: #include< ...
- 拦截器springmvc防止表单重复提交【2】
[参考博客:http://my.oschina.net/mushui/blog/143397] 原理:在新建页面中Session保存token随机码,当保存时验证,通过后删除,当再次点击保存时由于服务 ...
- Redis设计与实现 (三): 字典
哈希表 结构定义dict.h/dictht /* * 哈希表 * * 每个字典都使用两个哈希表,从而实现渐进式 rehash . */ typedef struct dictht { // 哈希表数 ...
- 51nod 1089 最长回文子串 V2(Manacher算法)
回文串是指aba.abba.cccbccc.aaaa这种左右对称的字符串. 输入一个字符串Str,输出Str里最长回文子串的长度. 收起 输入 输入Str(Str的长度 <= 100000) ...
- 四种线性相位FIR滤波器振幅谱统一形式
- MAC OS X常用命令总结2
1. dir:显示某个目录下的子目录与文件. 格式:dir [x:] [Path] [filename][ parameter] 参数解释: /a 显示所有文件夹与文件. /p 分页 ...