aop和注解将权限和操作日志与业务解耦
来源:互联网 发布:淘宝中国制造怎么申请 编辑:程序博客网 时间:2024/05/29 11:52
0.概述
本文主要介绍利用AOP和注解技术权限、操作日志解耦出来~
1.AOP(面向切面编程)简介
Aspect是一种新的模块化机制,用来描述分散在对象、类或函数中的横切关注点(crosscutting concern)。从关注点中分离出横切关注点是面向切面的程序设计的核心概念。分离关注点使解决特定领域问题的代码从业务逻辑中独立出来,业务逻辑的代码中不再含有针对特定领域问题代码的调用,业务逻辑同特定领域问题的关系通过切面来封装、维护,这样原本分散在整个应用程序中的变动就可以很好地管理起来。(来源spring 技术内幕)
2.具体实现
权限注解,利用注解中code区分
@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface AuthCode { String code() default "";}
操作日志注解(具体对象和操作类型可以设置为枚举)
@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface ActionLog { //对象类型 String objectType() default ""; //操作类型 String operateType() default "";; //处理对象 Class<? extends ActionLogHandler> handler() default DefaultActionLogHandler.class;}
操作日志handler 接口(可以根据需要设计相关接口)
public interface ActionLogHandler { boolean process(Object[] args, ApplicationContext ctx, ActionLog actionLog);}
public class DefaultActionLogHandler implements ActionLogHandler { public boolean process(Object[] args, ApplicationContext ctx, ActionLog actionLog) { return false; }}
@Component@Aspectpublic class AspectTest implements ApplicationContextAware { private static ApplicationContext context; @Around("public execution( * com.hsc.study.aop.*.*(..))") public Object around(ProceedingJoinPoint joinPoint) throws Throwable { //获取方法 MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); Object[] args = joinPoint.getArgs(); //获取方法上的权限注解 AuthCode authCode = method.getAnnotation(AuthCode.class); if (authCode != null) { String code = authCode.code(); //校验权限 } //获取日志操作注解 ActionLog actionLog = method.getAnnotation(ActionLog.class); ActionLogHandler actionLogHandler = null; Object ret = joinPoint.proceed(); //如果返回成功,且加的有注解,执行相应的方法 if (actionLog != null) { //实例化处理对象 actionLogHandler = actionLog.handler().newInstance(); actionLogHandler.process(args, getContext(), actionLog); } return ret; } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { context = applicationContext; } public static ApplicationContext getContext() { return context; }}
使用实例
@Servicepublic class StudentService { @ActionLog(objectType = "学生",operateType = "添加",handler = DefaultActionLogHandler.class) @AuthCode(code="write") public Student insert(Student i) { return null; }}
实现ApplicationContextAware接口是为了获取上下文环境,可能handler要用。
3.总结
aop和注解相结合可以很好将权限和操作日志与业务代码解耦出来,aop还可以做统一异常处理,这样不至于每个service层的方法都有try catch。
0 0
- aop和注解将权限和操作日志与业务解耦
- 采用spring AOP 和注解解决日志
- 使用spring aop + 注解完成对业务操作的日志记录
- 利用Spring AOP实现业务和异常日志记录
- 基于Spring aop 和JAVA注解方式添加日志
- 基于Spring aop 和JAVA注解方式添加日志
- 利用Spring AOP自定义注解解决日志和签名校验
- 基于Spring aop 和JAVA注解方式添加日志
- 利用Spring AOP自定义注解解决日志和签名校验
- Spring AOP自定义注解解决日志和签名校验
- 利用Spring AOP自定义注解解决日志和签名校验
- 自制aop和注解
- spring aop记录操作日志和错误日志
- 使用AOP实现日志和权限认证组件
- Spring 使用Aop 做切面日志,和权限。
- aop+自定义注解实现操作日志记录
- aop+自定义注解实现操作日志记录
- Spring AOP 自定义注解记录操作日志
- C++第1次实验-标准体重
- privacy polic
- php的默认配置文件在 /usr/local/apache2/conf/php.ini,通过为了使你的web更安全,我们需要对php.ini进行一些设置!
- python调用videocapture出错的问题
- Simple Tutorial on SVM and Parameter Tuning in Python and R
- aop和注解将权限和操作日志与业务解耦
- 在framework中添加字符串资源
- 爬虫-漫画喵的100行逆袭
- Activity生命周期
- [Python]学习笔记(1)
- java Value '0000-00-00 00:00:00' can not be represented as java.sql.Timestamp
- C# BASE64中文乱码问题
- 深入简出mysql
- 基于C++和OpenCv的SIFT_图像局部特征检测算法代码的实现