当前位置: 首页 > news >正文

郑州模板建站平台课程网站资源建设小结

郑州模板建站平台,课程网站资源建设小结,河北省建设银行网站首页,网站制作毕业设计文章目录 1. 自定义注解如何使用2. 自定义注解使用场景2.1 自定义注解使用AOP做权限校验2.2 自定义注解使用AOP记录用户操作日志2.3 自定义注解使用AOP记录接口请求时长 1. 自定义注解如何使用 需要使用interface修饰#xff0c;加上三个元注解 Documented#xff1a;生成API… 文章目录 1. 自定义注解如何使用2. 自定义注解使用场景2.1 自定义注解使用AOP做权限校验2.2 自定义注解使用AOP记录用户操作日志2.3 自定义注解使用AOP记录接口请求时长 1. 自定义注解如何使用 需要使用interface修饰加上三个元注解 Documented生成API文档使用Target决定此注解能加的范围类、方法、属性上面Retention作用域(在什么时候有作用java–》class–》运行时) Documented Target(ElementType.METHOD)//决定此注解能加的范围类方法属性 Retention(RetentionPolicy.RUNTIME)//作用域(在什么时候有作用java--》class--》运行时) public interface SysLog {String name() default ; }2. 自定义注解使用场景 2.1 自定义注解使用AOP做权限校验 1. 编写自定义注解 Retention(RetentionPolicy.RUNTIME) Target({ElementType.METHOD}) Documented public interface Authority {String authority() default ; } 2. 编写切面 定义切面并编写权限放行的逻辑 Aspect Component public class AuthoritionAspect {Pointcut(execution(* com.aigaofeng.aopdemo.controller.UserController.*(..)))public void pointcut() {}Around(pointcut())public void advice(ProceedingJoinPoint joinPoint) throws Throwable {MethodSignature methodSignature (MethodSignature) joinPoint.getSignature();Method method methodSignature.getMethod();if (!method.isAnnotationPresent(Authority.class)) {//没有使用注解默认放行joinPoint.proceed(); } else {Authority authority fetchPermission(methodSignature);//[1] 取出请求方的权限信息String userPermission root; //假设用户权限为 TESTSystem.out.println(用户权限: userPermission);//[2] 和注解的值做比较 authority.authority()if (userPermission.equals(authority.authority())){//[3] 校验通过放行用户System.out.println(放行);joinPoint.proceed();}return;}}private Authority fetchPermission(MethodSignature methodSignature) {return methodSignature.getMethod().getAnnotation(Authority.class);}}3. 编写测试类 在方法上使用自定义注解 Authority(authority “root”) RestController public class UserController {AutowiredUserInfoService userInfoService;Authority(authority root)RequestMapping(/getUser)public String getUser(){Integer userCount userInfoService.getUserCount();System.out.println(获取数据库的users:userCount);return success;} }4. 测试 在浏览器或postman上请接口观察日志 2.2 自定义注解使用AOP记录用户操作日志 1. 编写自定义注解 Retention(RetentionPolicy.RUNTIME) Target({ElementType.METHOD}) Documented public interface SysLog {String operation() default ;//操作内容 }2. 创建对应的实体类 Data TableName(value user_info) public class UserInfo implements Serializable {private static final long serialVersionUID 1L;private String id;private String name;private String email;private String phone;private Date logTime; } 3. 编写切面 Aspect Component public class SysLogAspect {/*** 定义Before增强拦截带有SysLog注解的方法并记录操作日志*/// 定义切点拦截的规则Pointcut(execution(* com.aigaofeng.aopdemo.controller.UserController.*(..)))public void pointcut() {}Around(pointcut())public Object around(ProceedingJoinPoint point) throws Throwable {long start System.currentTimeMillis();Object[] args point.getArgs();Object proceed point.proceed(point.getArgs());//调用目标方法long end System.currentTimeMillis();long consuming end - start;//方法执行时间//获取签名从而获取自定义注解上的value值MethodSignature methodSignature (MethodSignature) point.getSignature();SysLog sysLog methodSignature.getMethod().getAnnotation(SysLog.class);String declaringTypeName methodSignature.getDeclaringTypeName();//类名String name methodSignature.getName();//方法名String qualified declaringTypeNamename;//全限定名UserInfo userInfo new UserInfo();userInfo.setName(张三);userInfo.setId(1001);userInfo.setPhone(15523637367);userInfo.setEmail(526381269.qq.com);userInfo.setLogTime(new Date());System.out.println(请求对对象 userInfo.getName());return proceed;}}4. 编写测试方法 SysLog(operation 添加用户) // 调用加了SysLog注解的方法需要额外的记录用户的操作日志GetMapping(/test)public String test() {System.out.println(谁请求了该方法);return ok;}5. 测试 2.3 自定义注解使用AOP记录接口请求时长 步骤 1.自定义注解 2.编写切面类 Around(pointcut())public Object around(ProceedingJoinPoint point) throws Throwable {long start System.currentTimeMillis();Object[] args point.getArgs();Object proceed point.proceed(point.getArgs());//调用目标方法long end System.currentTimeMillis();long consuming end - start;//方法执行时间return proceed;} 3.在方法上使用自定义注解。
http://www.zqtcl.cn/news/251728/

相关文章:

  • 网站微信推广方案衡水外贸网站建设
  • 怎么打造自己的网站如何做自已网站
  • 美容美发网站模板wordpress适合优化吗
  • 网站开发的著作权和版权沧州市做网站价格
  • 优客逸家网站源码酒吧装修
  • 深圳网站制作的公司怎么样开工作室做网站怎样找资源
  • 大连城乡建设局网站seo编辑招聘
  • 网站建设意见怎么在中国移动做网站备案
  • 做内贸哪个网站找客户网络外包
  • 古玩网站建设意义钟山县住房和城乡建设局网站
  • 网站开发微信公众号自定义菜单规则网站建设
  • 营销网站建设工作教育培训wordpress主题
  • 温州地区做网站公司如何注册新公司
  • 做的网站怎样评估价值全国信息公示系统官网
  • 外国网站签到做任务每月挣钱1g内存vps 开电影网站
  • 营销型网站案例易网拓互联购物
  • 河南企业网站制作微信小程序如何做
  • 金坛住房和城乡建设局网站wordpress 需要授权吗
  • 个人理财的网站开发天津 公司网站建设
  • 做电脑游戏破解的网站大宗交易平台软件
  • 男女做暖暖视频免费网站网络营销策划案ppt
  • 普通网站 多大空间网站开发报告参考文献
  • 来宾住房和城乡建设网站pc网站建设哪
  • WordPress一键开启全站SSL东莞企业网站建设公司
  • 青海省公路建设管理局官方网站wordpress 加入地图
  • 建湖专业做网站的公司如何制作wordpress网站地图
  • 做自媒体查找素材的网站石家庄网站建设费用
  • 建立局域网网站怎么做外国网站
  • 绍兴专业网站建设公司网站seo设计
  • 开发网站需要多久建设银行招聘网站