spring mvc 小记(一):spring注解与java原注解

使用spring已有2年之久,却还是停留在使用阶段,感觉这么下去不是办法,所以还是想往深处一探究竟。

今天无意中查询到java注解,才了解到原来那些框架里的注解全是基于java所提供的元注解上编写的,也就是说,我们自己也可以自定义注解。参考资料出处:http://blog.sina.com.cn/s/blog_93dc666c0101gzn5.html

首先,新建一个自定义的注解类

<span style="font-size:18px;">import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.TYPE,ElementType.METHOD,ElementType.FIELD,ElementType.CONSTRUCTOR})
@Retention(RetentionPolicy.RUNTIME)
public @interface TestA {
    String name();
    int id() default 0;
    Class gid();
}</span>
其中各参数具体的含义可以自行去网上查询,现在注解类已建完,可以直接拿来去注解别的类或方法了,新建一个测试方法
<span style="font-size:18px;">import java.util.HashMap;
import java.util.Map;

@TestA(name="type",gid=Long.class) //类成员注解
public class UserAnnotation {

    @TestA(name="param",id=1,gid=Long.class) //类成员注解
    private Integer age;

    @TestA (name="construct",id=2,gid=Long.class)//构造方法注解
    public UserAnnotation(){
    }

    @TestA(name="public method",id=3,gid=Long.class) //类方法注解
    public void a(){
        Map m = new HashMap(0);
    }

    @TestA(name="protected method",id=4,gid=Long.class) //类方法注解
    protected void b(){
        Map m = new HashMap(0);
    }

    @TestA(name="private method",id=5,gid=Long.class) //类方法注解
    private void c(){
        Map m = new HashMap(0);
    }
}</span>
将注解放上去后,没报错,好像也没啥特别的效果,这个时候就可以想想spring之类的框架了,比如为什么URL可以执行被注解的方法,大概情况就是,通过注解找到被注解的方法,然后通过java反射去执行方法,那么我们也可以试试通过注解找到这些被注解的元素:

<span style="font-size:18px;">public class ParseAnnotation {

    public static void parseTypeAnnotation() throws ClassNotFoundException {
        Class clazz = Class.forName("com.tan.Test.UserAnnotation");

        Annotation[] annotations = clazz.getAnnotations();
        for (Annotation annotation : annotations) {
            TestA testA = (TestA)annotation;
            System.out.println("id= "+testA.id()+"; name= "+testA.name()+"; gid = "+testA.gid());
        }
    }

    public static void parseMethodAnnotation(){
        Method[] methods = UserAnnotation.class.getDeclaredMethods();
        for (Method method : methods) {
            boolean hasAnnotation = method.isAnnotationPresent(TestA.class);
            if (hasAnnotation) {

                TestA annotation = method.getAnnotation(TestA.class);
                System.out.println("method = " + method.getName()
                        + " ; id = " + annotation.id() + " ; description = "
                        + annotation.name() + "; gid= "+annotation.gid());
            }
        }
    }

    public static void parseConstructAnnotation(){
        Constructor[] constructors = UserAnnotation.class.getConstructors();
        for (Constructor constructor : constructors) {

            boolean hasAnnotation = constructor.isAnnotationPresent(TestA.class);
            if (hasAnnotation) {

                TestA annotation =(TestA) constructor.getAnnotation(TestA.class);
                System.out.println("constructor = " + constructor.getName()
                        + " ; id = " + annotation.id() + " ; description = "
                        + annotation.name() + "; gid= "+annotation.gid());
            }
        }
    }

    //参数注解解析
    public static void parseParameterAnnotation() {
        Field[] fields = UserAnnotation.class.getDeclaredFields();
        for(int i=0;i<fields.length;i++){
            boolean hasAnnotation = fields[i].isAnnotationPresent(TestA.class);
            if(hasAnnotation){
                TestA annotation = fields[i].getAnnotation(TestA.class);
                System.out.println("变量名:"+fields[i].getName()+"注解:"+annotation.name()+"==="+annotation.id()+"=="+annotation.gid());
            }
        }
    }

    public static void main(String[] args) throws ClassNotFoundException {
//        parseTypeAnnotation();
//        parseMethodAnnotation();
//        parseConstructAnnotation();
        parseParameterAnnotation();
    }
}</span>
运行main方法,看到这个被注解的变量打印出来了,逻辑是查询所有变量,判断被注解变量,打印。

猜你喜欢

转载自blog.csdn.net/u012602046/article/details/52448325