自定义注解的使用

自定义注解的使

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 {

}

使用方法同样是将其添加至启动类上

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

意田天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值