注解自定义
IdAnno
注解就是有特殊功能的接口
直接新建一个interface类就可以
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface IdAnno {
String name();
int age();
}
此interface有两个属性:
1、String类型的name属性
2、int类型的age属性
需要注意:一定要加@Target和@Retention注解:@Target({ElementType.METHOD})定义此注解使用在方法上,@Retention(RetentionPolicy.RUNTIME)定义此注解在运行时生效
注解测试
AnnotationKit——要被测试的工具类
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
import org.springframework.web.method.HandlerMethod;
import com.br.common.annotation.RequirePermission;
import com.br.kit.JavaKit;
import com.br.kit.collection.ListKit;
public class AnnotationKit {
public static <T extends Annotation> T getAnnotation(Object handler,Class<T> clazz) {
if(handler instanceof HandlerMethod) {
return getAnnotation((HandlerMethod)handler,clazz);
}
return null;
}
/**
* 从方法上获得注解,如果没有,尝试从类上获得
* @param <T>
* @param handlerMethod
* @param clazz
* @return
*/
public static <T extends Annotation> T getAnnotation(HandlerMethod handlerMethod,Class<T> clazz) {
final Method method = handlerMethod.getMethod();
T annotation = method.getAnnotation(clazz);
if(annotation==null) {
final Class<?> declaringClass = method.getDeclaringClass();
annotation = declaringClass.getAnnotation(clazz);
}
return annotation;
}
}
AnnotationTestVo、AnnotationKitTest——进行测试
AnnotationTestVo包含了使用自定义注解@IdAnno的方法
import org.springframework.web.bind.annotation.GetMapping;
import com.br.common.annotation.RequirePermission;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.Scope;
public class AnnotationTestVo {
@RequirePermission(value = "123456")
@ApiModelProperty(name="peng", value="测试11111")
@GetMapping(value = "zhangsan")
@IdAnno(name = "get", age = 1)
public String getName(String name ,int age){
return String.format("%s___%s ",name, age);
}
@ApiModelProperty(name="peng", value="测试11111")
@GetMapping(value = "zhangsan")
@IdAnno(name = "read", age = 10)
public String readName(String name ,int age){
return String.format("%s___%s ",name, age);
}
}
测试1:并未调用AnnotationKit
AnnotationKitTest正式进行测试:添加@Test注解
测试自定义注解IdAnno
import java.lang.reflect.Method;
import org.junit.Test;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.method.HandlerMethod;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.Scope;
public class AnnotationKitTest {
@Test
public void getAnnotation() throws NoSuchMethodException {
AnnotationTestVo annotationTestVo= new AnnotationTestVo();
IdAnno idAnno;
for (Method declaredMethod : annotationTestVo.getClass().getDeclaredMethods()) {
idAnno=declaredMethod.getDeclaredAnnotation(IdAnno.class);
if(idAnno!=null){
System.out.println(String.format("%s___%s-%s",declaredMethod.getName(),idAnno.name(),idAnno.age()));
Student student=new Student();
student.setName(idAnno.name());
student.setAge(idAnno.age());
System.out.println(JSONObject.toJSONString(student));
}else{
System.out.println(String.format("%s_没有注解%s",declaredMethod.getName(),"IdAnno" ));
}
}
}
}
输出结果:
getName___get-1
{"age":1,"name":"get"}
readName___read-10
{"age":10,"name":"read"}
测试2:调用AnnotationKit
AnnotationKitTest正式进行测试:添加@Test注解
测试注解:
@RequirePermission(value = "123456")
@ApiModelProperty(name="peng", value="测试11111")
@Scope(name="ssss" ,description = "ssss")
@JsonFormat(pattern="YYYY-MM-DD")
@GetMapping(value = "zhangsan")
在AnnotationKitTest类中新建方法:
@Test
public void getAnnotation() throws NoSuchMethodException {
AnnotationTestVo annotationTestVo= new AnnotationTestVo();
HandlerMethod handlerMethod =new HandlerMethod(annotationTestVo, "getName", String.class, int.class);
ApiModelProperty apiModelProperty= AnnotationKit.getAnnotation(handlerMethod, ApiModelProperty.class);
System.out.println(apiModelProperty.value());
System.out.println(apiModelProperty.name());
JsonFormat jsonFormat= AnnotationKit.getAnnotation(handlerMethod, JsonFormat.class);
System.out.println(jsonFormat.pattern());
GetMapping getMapping= AnnotationKit.getAnnotation(handlerMethod, GetMapping.class);
System.out.println(JSONObject.toJSONString(getMapping.value()));
}
输出结果:
测试11111
peng
YYYY-MM-DD
["zhangsan"]