自定义注解的使
1. 例如自定义开启swagger注解
package com.etouch.common.annotation;import
com.etouch.common.exception.GlobalExceptionHandler;import org.springframework.context.annotation.Import;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;
/** * @author fei */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)]
@EnErrorHandler
@EnSelfSwagger
public @interface EnableCyc {}
2. 自定义统一异常管理注解
package com.etouch.common.annotation;
import com.etouch.common.exception.GlobalExceptionHandler;
import org.springframework.context.annotation.Import;
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.TYPE)
@Import(GlobalExceptionHandler.class) //表示这个注解使用上面的类
public @interface EnErrorHandler { //名字自己起
}
使用方法
启动类上添加上该注解就可以了
整合多个注解为一个注解
示例: 将以上swagger的注解和统一异常处理的注解整合为一个
package com.etouch.common.annotation;
import com.etouch.common.exception.GlobalExceptionHandler;
import org.springframework.context.annotation.Import;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author fei
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@EnErrorHandler
@EnSelfSwagger
public @interface EnableCyc {
}
使用方法同样是将其添加至启动类上