注解类

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. css11动态效果

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  2. Huffman编码实现文件的压缩与解压缩。

    以前没事的时候写的,c++写的,原理很简单,代码如下: #include <cstdio> #include <cstdlib> #include <iostream&g ...

  3. JavaScript JSON.parse()和JSON.stringify()

    parse用于从一个字符串中解析出json对象,如 var str = '{"name":"huangxiaojian","age":&qu ...

  4. sql server显示行号

    --    工具-> --    选项-> --    文本编辑器-> --    所有语言-> --    常规-> --    显示-> --    行号

  5. QT画图

    if (0) { QApplication a(argv, args); QGraphicsScene scene; scene.setSceneRect(-300,-300,600,600); sc ...

  6. 用百度地图API打造方便自己使用的手机地图

    有钱人咱就不说了,因为偶是个穷银--因为穷,所以去年买的Huawei C8650+到现在还在上岗,对于没有钱买好的配置的手机的童鞋来说,类似于百度,谷歌,高德等商家的地图在自己的机器上跑起来确实是有点 ...

  7. Leetcode 890. Find and Replace Pattern

    把pattern映射到数字,也就是把pattern标准化. 比如abb和cdd如果都能标准化为011,那么就是同构的. class Solution: def findAndReplacePatter ...

  8. Spring源码学习之:ClassLoader学习(5)-自测

    [一]测试目的(ClassLoader的作用) 1:测试涉及三个jar包,nonbankcard-configure-0.0.1-SNAPSHOT.jar,nonbankcard-persist-0. ...

  9. django的多对一,一对一,多对多关系

    from django.db import models class Publisher(models.Model): name = models.CharField(max_length=30) a ...

  10. bootstrap table 兼容ie8 -- refreshOptions

    今天项目使用 bootstrap table 在ie8下发现 方法 refreshOptions 报错. 经过调试监控发现错误如下: 153 行 代码 Object.getOwnPropertyNam ...