结合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 ...
随机推荐
- docker-es
镜像地址https://hub.docker.com/_/elasticsearch/ docker pull elasticsearch 这个版本是dockerhub最新,官方最新版:https:/ ...
- Android以root起一个process[shell脚本的方法]
有时候我们写的app要用uid=0的方式启动一个process,framework层和app层是做不到的,只有通过写脚本,利用am来实现.下面是具体步骤: 1.创建一个包含Main()方法Java p ...
- c++ 字符串查找函数
头文件:#include <string.h> 定义函数:int strcasecmp (const char *s1, const char *s2); 函数说明:strcasecmp( ...
- Java进阶知识点7:不要只会写synchronized - JDK十大并发编程组件总结
一.背景 提到Java中的并发编程,首先想到的便是使用synchronized代码块,保证代码块在并发环境下有序执行,从而避免冲突.如果涉及多线程间通信,可以再在synchronized代码块中使用w ...
- Python面对对象相关知识总结
很有一段时间没使用python了,前两天研究微信公众号使用了下python的django服务,感觉好多知识都遗忘了,毕竟之前没有深入的实践,长期不使用就忘得快.本博的主要目的就是对Python中我认为 ...
- SSH框架(2)
个人分类: Java面试 Struts 谈谈你对Struts的理解. 答: 1.struts是一个按MVC模式设计的Web层框架,其实它就是一个大大的servlet,这个Servlet名为Acti ...
- self = [super init]
Objective-C的推荐init方法写法如下: - (id) init { if(self = [super init]) { //为子类增加属性进行初始化 } return self; } 返回 ...
- 十三、python沉淀之路--文件操作
一.文件的读操作 例1 f = open('学习',encoding='utf-8') #首先要打开文件,不然直接读,是读不出来的 data = f.read() #read后的括号里不添加任何东西 ...
- JUnit测试,获取Spring MVC环境
@RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration(locations = { &qu ...
- 织梦 dede 笔记
将项目转移到另一服务器 方法: https://www.genban.org/news/dedecms-13096.html 在实际中,我走的是第二种方法 方法一: 1 后台>系统>备份 ...