1.业务场景
前端访问后端,部分接口需要先进行判断。可以把当前需要判断的代码做成一个注解。
例如:
盘点期间,不允许责任人,保管员等的变动。多个地方都要对当前是否有盘点任务进行判断。
2.自定义注解
1.定义注解
/**
* 自定义注解
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.TYPE})
public @interface TestTask {
}
2.自定义异常
public class LockException extends RuntimeException {
private static final long serialVersionUID = 1L;
private String code;
private String msg;
public LockException(String code,String msg) {
this.code = code;
this.msg=msg;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
3.定义切面
@Component
@Aspect
public class Validation {
@Before("@annotation(com.example.onemodule.aop.TestTask)")
public void CheckEndTask(){
//模拟查询业务,1:有业务 0:无业务
int rd= Math.random()>0.5?1:0;
if(rd==1){
throw new LockException("333","主动抛出来一个异常,任务未结束");
}
}
}
4.捕获异常
@RestControllerAdvice
public class ExceptionControllerHandler {
@ExceptionHandler(value = Exception.class)
public String handler(Exception ex) {
if (ex instanceof LockException) {
return "接到——————333主动抛出来一个异常,任务未结束";
}
return "22222222222";
}
}
5.使用
@RestController
@RequestMapping("/hello")
public class Hello {
@TestTask
@RequestMapping()
public String hello(){
return "我是张三";
}
}
6.结果
6.1正常访问
6.2拦截提醒
3.元注解
@Target :用于描述注解的使用范围
public enum ElementType {
/**
* Class, interface (including annotation type), or enum declaration
* 类、接口(包括注释类型)或枚举声明
*/
TYPE,
/**
* Field declaration (includes enum constants)
* 字段声明(包括枚举常量)
*/
FIELD,
/**
* Method declaration
* 方法声明
*/
METHOD,
/**
* Formal parameter declaration
* 形式参数声明
*/
PARAMETER,
/**
* Constructor declaration
* 构造函数声明
*/
CONSTRUCTOR,
/**
* Local variable declaration
* 局部变量声明
*/
LOCAL_VARIABLE,
/**
* Annotation type declaration
* 注释类型声明
*/
ANNOTATION_TYPE,
/**
* Package declaration
* 包声明
*/
PACKAGE,
/**
* Type parameter declaration
* 类型参数声明
* @since 1.8
*/
TYPE_PARAMETER,
/**
* Use of a type
* 类型的使用
* @since 1.8
*/
TYPE_USE
}
@Retention:被描述的注解在什么范围内有效
public enum RetentionPolicy {
/**
* Annotations are to be discarded by the compiler.
* 注释将被编译器丢弃。
*/
SOURCE,
/**
* Annotations are to be recorded in the class file by the compiler
* but need not be retained by the VM at run time. This is the default
* behavior.
* 注释将由编译器记录在类文件中但不需要在运行时被VM保留。这是默认值行为。
*/
CLASS,
/**
* Annotations are to be recorded in the class file by the compiler and
* retained by the VM at run time, so they may be read reflectively.
* 注释将由编译器记录在类文件中,并且在运行时被VM保留,因此可以反射性地读取它们。
* @see java.lang.reflect.AnnotatedElement
*/
RUNTIME
}
@Documented
/**
* Indicates that annotations with a type are to be documented by javadoc
* and similar tools by default. This type should be used to annotate the
* declarations of types whose annotations affect the use of annotated
* elements by their clients. If a type declaration is annotated with
* Documented, its annotations become part of the public API
* of the annotated elements.
*指示类型为的注释将由javadoc记录以及默认的类似工具。此类型应用于注释类型的声明,其注释会影响带注释的用
*客户提供的元素。如果类型声明用在文档中,它的注释成为公共API的一部分注释元素的。
* @author Joshua Bloch
* @since 1.5
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Documented {
}
@Inherited
/**
* Indicates that an annotation type is automatically inherited. If
* an Inherited meta-annotation is present on an annotation type
* declaration, and the user queries the annotation type on a class
* declaration, and the class declaration has no annotation for this type,
* then the class's superclass will automatically be queried for the
* annotation type. This process will be repeated until an annotation for this
* type is found, or the top of the class hierarchy (Object)
* is reached. If no superclass has an annotation for this type, then
* the query will indicate that the class in question has no such annotation.
*
* <p>Note that this meta-annotation type has no effect if the annotated
* type is used to annotate anything other than a class. Note also
* that this meta-annotation only causes annotations to be inherited
* from superclasses; annotations on implemented interfaces have no
* effect.
*
* @author Joshua Bloch
* @since 1.5
* @jls 9.6.3.3 @Inherited
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Inherited {
}