zl程序教程

您现在的位置是:首页 >  其它

当前栏目

自定义简单注解并获取值

简单 获取 自定义 注解
2023-09-11 14:19:38 时间

 

1、自定义注解Car_color

package com.dist.annotation;
 
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
 
//@Target(ElementType.PARAMETER)  //表示这个注解的只适用于属性,也可以写多个({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
@Retention(RetentionPolicy.RUNTIME) //表示注解只在执行期起作用
public @interface Car_color {
    //返回值String就是参数的类型,只能是基本类型
    //这里用default默认参数是“白色”
    String color() default "白色";  //color方法其实是声明了一个配置参数
 
}

2、将注解带到类或接口或字段或方法上

//@Car_color(color="黑色")
public class Car {
    @Car_color(color="黑色")
    private String color;
 
}

3、获取方法

1)类上注解获取值

public class Test {
 
    public static void main(String[] args) throws ClassNotFoundException {
        Class cls=Class.forName("com.dist.annotation.Car");  //获取类对象
 
        Car_color annotation = (Car_color) cls.getAnnotation(Car_color.class);
        System.out.println(annotation.color());
 
    }
}

2)获取字段上的

public class Test {
 
    public static void main(String[] args) throws ClassNotFoundException {
        Class cls=Class.forName("com.dist.annotation.Car");  //获取类对象
 
        Field[] field=cls.getDeclaredFields();          //获取类的属性数组
        for(Field f:field){                             //循环属性
            if(f.isAnnotationPresent(Car_color.class)){ //获取属性的注解,并判断是否是Car_color.class注解
                Car_color car=f.getAnnotation(Car_color.class);     //获取Car_color注解对象
                System.out.println("汽车颜色:"+car.color());            //输出注解的color配置参数
            }
        }
 
    }
}

 

 Spring MVC实现获取

@Controller
@RequestMapping("/test")
public class testApp {

    //请求映射处理映射器
    //Spring MVC 在启动的时候将所有贴有@RequestMapping注解的请求,收集起来,封装到该对象中
    @Autowired
    private RequestMappingHandlerMapping rmhm;

    @RequestMapping("/testann")
    public String testAnn(){

        /**
         * Spring获取注解的内容:
         * 1.先要获取控制器方法(可以通过请求映射处理映射器获取方法上的@RequestMapping)
         * 2.遍历方法,可以获取其注解值
         */

        //1.获取controller中所有带有@RequestMapping标签的方法
        Map<RequestMappingInfo, HandlerMethod> handlerMethods = rmhm.getHandlerMethods();
        Collection<HandlerMethod> methods = handlerMethods.values();//获得所有方法
        for (HandlerMethod method:methods){
            System.out.println(method);
            //2.遍历所有方法,获得方法上的指定备注值
            Car_color annotation = method.getMethodAnnotation(Car_color.class);
            if(annotation != null){
               String resource =  annotation.color();
            }
        }


        return null;
    }

}

 

 如果上面的 rmhm变量:

RequestMappingHandlerMapping 找不到,报错误 Could not autowire. No beans of 'RequestMappingHandlerMapping' type found

那在spring-mvc.xml文件配置这个bean (如果是spring boot 看我下下一篇文章:https://www.cnblogs.com/fps2tao/p/13677544.html)
    <bean id="requestMappingHandlerMapping" class="org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping">
    </bean>

 

 

 

转 : https://www.cnblogs.com/x54256/p/9429815.html