注解类

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 实现自定义注解的更多相关文章

  1. 利用Spring AOP自定义注解解决日志和签名校验

    转载:http://www.cnblogs.com/shipengzhi/articles/2716004.html 一.需解决的问题 部分API有签名参数(signature),Passport首先 ...

  2. spring AOP自定义注解方式实现日志管理

    今天继续实现AOP,到这里我个人认为是最灵活,可扩展的方式了,就拿日志管理来说,用Spring AOP 自定义注解形式实现日志管理.废话不多说,直接开始!!! 关于配置我还是的再说一遍. 在appli ...

  3. spring AOP自定义注解 实现日志管理

    今天继续实现AOP,到这里我个人认为是最灵活,可扩展的方式了,就拿日志管理来说,用Spring AOP 自定义注解形式实现日志管理.废话不多说,直接开始!!! 关于配置我还是的再说一遍. 在appli ...

  4. (转)利用Spring AOP自定义注解解决日志和签名校验

    一.需解决的问题 部分API有签名参数(signature),Passport首先对签名进行校验,校验通过才会执行实现方法. 第一种实现方式(Origin):在需要签名校验的接口里写校验的代码,例如: ...

  5. 使用Spring Aop自定义注解实现自动记录日志

    百度加自己琢磨,以下亲测有效,所以写下来记录,也方便自己回顾浏览加深印象之类,有什么问题可以评论一起解决,不完整之处也请大佬指正,一起进步哈哈(1)首先配置文件: <!-- 声明自动为sprin ...

  6. day05 Spring中自定义注解的用处-之获取自定义的Servie

    PS: 在RPC远程调用中,想要获取自定义的service的方法,就得自定义标签遍历拿到方法 PS:在spring中,两个最核心的 概念是aop和ioc,aop其实就是动态代理. ioc 就是解决对象 ...

  7. Spring aop+自定义注解统一记录用户行为日志

    写在前面 本文不涉及过多的Spring aop基本概念以及基本用法介绍,以实际场景使用为主. 场景 我们通常有这样一个需求:打印后台接口请求的具体参数,打印接口请求的最终响应结果,以及记录哪个用户在什 ...

  8. Spring AOP 自定义注解实现统一日志管理

    一.AOP的基本概念: AOP,面向切面编程,常用于日志,事务,权限等业务处理.AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容(Spring核心之一),是函数式编程 ...

  9. 【spring】自定义注解 custom annotation

    自定义注解 custom annotation 使用场景 类属性自动赋值. 验证对象属性完整性. 代替配置文件功能,像spring基于注解的配置. 可以生成文档,像java代码注释中的@see,@pa ...

随机推荐

  1. LeetCode OJ:Integer to Roman(转换整数到罗马字符)

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

  2. 编写自己的validate校验框架原理(转)

    原文链接:http://blog.csdn.net/a973893384/article/details/51517388 具体思路: 我们使用自定义注解实现.然后需要解决的是两个问题: 1是如何扫描 ...

  3. Android application testing with the Android test framework

    目录(?)[-] Android automated testing 1 How to test Android applications Tip 2 Unit tests vs functional ...

  4. HDU - 6096 :String (AC自动机,已知前后缀,匹配单词,弱数据)

    Bob has a dictionary with N words in it. Now there is a list of words in which the middle part of th ...

  5. LG3391 【模板】文艺平衡树(Splay)

    题意 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1 \(n,m ...

  6. 监听文本框输入oninput和onpropertychange事件

    前端页面开发的很多情况下都需要实时监听文本框输入,比如腾讯微博编写140字的微博时输入框动态显示还可以输入的字数.过去一般都使用onchange/onkeyup/onkeypress/onkeydow ...

  7. Collections排序

    0.前言 ThreeSet的底层实现是红黑树,它在创建set的过程中实现排序.Collections.sort是在对整个集合进行排序,按道理来说使用TreeSet插入集合元素直至建立整个TreeSet ...

  8. The type javax.xml.rpc.ServiceException cannot be resolved.It is indirectly

    The type javax.xml.rpc.ServiceException cannot be resolved.It is indirectly 博客分类: 解决方案_Java   问题描述:T ...

  9. java中String和char的区别

    首先来看一下Java的数据类型.Java 包括两种数据类型: 1.原始数据类型(primitive data type):byte,short, char, int, long,float,doubl ...

  10. AppCan上下拉列表刷新

    function initBounce(funcTop, funcBottom){ uexWindow.setBounce("1"); if (!funcTop && ...