Java自定义注解的实现
Java自定义注解的实现,总共三步(eg.@RandomlyThrowsException):
1.首先编写一个自定义注解@RandomlyThrowsException
package com.github.prontera; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface RandomlyThrowsException {
}
2.编写自定义注解@RandomlyThrowsException的实现
package com.github.prontera.aspect; import com.github.prontera.config.ManualExceptionProperties;
import com.github.prontera.exception.ManualException;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.Ordered; import java.security.SecureRandom;
import java.util.Random; @Aspect
public class ManualExceptionAspect implements Ordered {
private final int order;
private final ManualExceptionProperties properties;
private static final Random RANDOM = new SecureRandom(); public ManualExceptionAspect(int order, ManualExceptionProperties properties) {
this.order = order;
this.properties = properties;
} @Around("@annotation(com.github.prontera.RandomlyThrowsException)")
public Object aroundMethod(ProceedingJoinPoint joinPoint) throws Throwable {
if (properties.isEnabled()) {
if (RANDOM.nextInt(100) % properties.getFactor() == 0) {
throw new ManualException("manual exception");
}
}
return joinPoint.proceed();
} @Override
public int getOrder() {
return order;
}
}
3.使用自定义注解@RandomlyThrowsException
@Delay
@RandomlyThrowsException
@ApiOperation(value = "下单", notes = "生成预订单")
@RequestMapping(value = "/orders", method = RequestMethod.POST)
public ObjectDataResponse<Order> placeOrder(@Valid @RequestBody PlaceOrderRequest request, BindingResult result) {
return orderService.placeOrder(request);
}
Java自定义注解的实现的更多相关文章
- java自定义注解类
一.前言 今天阅读帆哥代码的时候,看到了之前没有见过的新东西, 比如java自定义注解类,如何获取注解,如何反射内部类,this$0是什么意思? 于是乎,学习并整理了一下. 二.代码示例 import ...
- java自定义注解实现前后台参数校验
2016.07.26 qq:992591601,欢迎交流 首先介绍些基本概念: Annotations(also known as metadata)provide a formalized way ...
- java自定义注解注解方法、类、属性等等【转】
http://anole1982.iteye.com/blog/1450421 http://www.open-open.com/doc/view/51fe76de67214563b20b385320 ...
- java自定义注解知识实例及SSH框架下,拦截器中无法获得java注解属性值的问题
一.java自定义注解相关知识 注解这东西是java语言本身就带有的功能特点,于struts,hibernate,spring这三个框架无关.使用得当特别方便.基于注解的xml文件配置方式也受到人们的 ...
- Java自定义注解源码+原理解释(使用Java自定义注解校验bean传入参数合法性)
Java自定义注解源码+原理解释(使用Java自定义注解校验bean传入参数合法性) 前言:由于前段时间忙于写接口,在接口中需要做很多的参数校验,本着简洁.高效的原则,便写了这个小工具供自己使用(内容 ...
- JAVA自定义注解 ------ Annotation
日常开发工作中,合理的使用注解,可以简化代码编写以及使代码结构更加简单,下面记录下,JAVA自定义注解的开发过程. 定义注解声明类. 编写注解处理器(主要起作用部分). 使用注解. 相关知识点介绍, ...
- Java自定义注解和运行时靠反射获取注解
转载:http://blog.csdn.net/bao19901210/article/details/17201173/ java自定义注解 Java注解是附加在代码中的一些元信息,用于一些工具在编 ...
- JAVA自定义注解
在学习使用Spring和MyBatis框架的时候,使用了很多的注解来标注Bean或者数据访问层参数,那么JAVA的注解到底是个东西,作用是什么,又怎样自定义注解呢?这篇文章,即将作出简单易懂的解释. ...
- Java 自定义注解
在spring的应用中,经常使用注解进行开发,这样有利于加快开发的速度. 介绍一下自定义注解: 首先,自定义注解要新建一个@interface,这个是一个注解的接口,在此接口上有这样几个注解: @Do ...
随机推荐
- 【转】支持向量机(SVM)
什么是支持向量机(SVM)? SVM 是一种有监督的机器学习算法,可用于分类或回归问题.它使用一种称为核函数(kernel)的技术来变换数据,然后基于这种变换,算法找到预测可能的两种分类之间的最佳边界 ...
- Ansible自动化运维工具-上
[Ansible特点] 1)Ansible与saltstack均是基于Python语言开发的 2)安装使用简单,基于不同插件和模块实现各种软件,平台,版本的管理以及支持虚拟容器多层级的部署 3)不需要 ...
- Python内置函数(35)——next
英文文档: next(iterator[, default]) Retrieve the next item from the iterator by calling its __next__() m ...
- Spring Security 入门(1-1)Spring Security是什么?
1.Spring Security是什么? Spring Security 是一个安全框架,前身是 Acegi Security , 能够为 Spring企业应用系统提供声明式的安全访问控制. Spr ...
- Spring MVC拦截器的配置
最近在用SpringMVC,想用它的拦截器,但是配置了几次都不成功了,最后翻阅了不少文章终于成功了,遂记录于此,以方便他人. 首先引入命名空间: xmlns:mvc="http://www. ...
- 数据结构与算法 —— 链表linked list(03)
继续关于linked list的算法题: 删除排序链表中的重复元素 给定一个排序链表,删除所有重复的元素使得每个元素只留下一个. 案例: 给定 1->1->2,返回 1->2 给定 ...
- leetcode算法: Keyboard Row
Given a List of words, return the words that can be typed using letters of alphabet on only one row' ...
- spring boot定制Jackson ObjectMapper,为什么不生效
先说结论: 项目中定制了spring 的redisTemplate,而这个template没有使用我自定义的Jackson ObjectMapper.所以不生效. 下面是详细过程: 起因是spring ...
- (第一章)对程序员来说CPU是什么
这几天,看到一本书,<程序是怎么跑起来的>,觉得之前都没有完整的看完一本书,现在要从这本书开始,慢慢的培养自己写读书笔记的习惯,不能度过去就忘了. 学习是一个螺旋上升的过程,不要指望一下子 ...
- Django(博客系统):文章内容使用django-ckeditor、文章简介使用django-tinymce
文章内容使用django-ckeditor 1)安装django-ckeditor pip install django-ckeditorpip install Pillow 2)在settings. ...