• Java


    package testannotation;
    
    import  java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    @Target({ElementType.FIELD,ElementType.CONSTRUCTOR,ElementType.METHOD})
    @Retention(RetentionPolicy.RUNTIME)//运行时
    public @interface MyAnnotation {
         String [] value();
    }
    
    package testannotation;
    
    public class Person {
            private String name;
            private Integer age;
            private String sex;
            @MyAnnotation({"杨晓航","20","男"})
            public Person(){}
    
            public String getName() {
                return name;
            }
            public void setName(String name) {
                this.name = name;
            }
            public Integer getAge() {
                return age;
            }
            public void setAge(Integer age) {
                this.age = age;
            }
            public String getSex() {
                return sex;
            }
            public void setSex(String sex) {
                this.sex = sex;
            }
    
    }
    
    public class User {
        private String username;
        private String password;
        private Float balance;
    
        @MyAnnotation({"杨晓航","123","10000"})
        public User(){}
    
        public String getUsername() {
            return username;
        }
        public void setUsername(String username) {
            this.username = username;
        }
        public String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }
        public Float getBalance() {
            return balance;
        }
        public void setBalance(Float balance) {
            this.balance = balance;
        }
    
    }
    
    package testannotation;
    import java.lang.annotation.Annotation;
    import java.lang.reflect.Constructor;
    import java.lang.reflect.Field;
    import java.lang.reflect.Method;
    
    public class MySpring {
    
        public Object getBean(String classname){
            Object obj = null;
            try {
                //1.先通过传递的类名获得对应的Class
                Class clazz = Class.forName(classname);
                //2.通过Class获取无参数的构造方法
                Constructor con = clazz.getConstructor();
                //3.构造方法创建一个对象
                obj = con.newInstance();
    
                //4.获取属性的值
                Annotation a = con.getAnnotation(MyAnnotation.class);
                //5.执行value方法
                Class clazz2 = a.getClass();
                Method method = clazz2.getMethod("value");
                String [] values = (String[]) method.invoke(a);
                //6.将values的值存入属性中
                //找寻属性对应的set方法赋值
                //获取传递的Class类全部的私有属性
                Field[] filelds = clazz.getDeclaredFields();
                for(int i = 0; i < filelds.length; i++){
                    //获取属性的名字
                    String fieldName = filelds[i].getName();
                    String firstName = fieldName.toUpperCase().substring(0,1);
                    String nextName = fieldName.substring(1);
                    StringBuilder stringBuilder = new StringBuilder("set");
                    stringBuilder.append(firstName);
                    stringBuilder.append(nextName);
                    //将值通过调用对应的set方法存入属性
                            //获取数据类型
                    Class fieldType = filelds[i].getType();
                    //方法名字,数据类型
                    Method setMethod = clazz.getMethod(stringBuilder.toString(),fieldType);
                    //需要将注解内读取到的String类型的值 转化成属性类型对应的值
                    setMethod.invoke(obj,fieldType.getConstructor(String.class).newInstance(values[i]));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return obj;
        }
    }
    
    package testannotation;
    
    public class TestMain {
        public static void main(String []args){
            MySpring ms = new MySpring();
            Person person = (Person) ms.getBean("testannotation.Person");
            System.out.println(person.getName()+"--"+person.getSex()+"--"+person.getAge());
            User user = (User) ms.getBean("testannotation.User");
            System.out.println(user.getBalance()+"--"+user.getPassword()+"--"+user.getUsername());
        }
    }
  • 相关阅读:
    《u-boot.lds分析》
    《uboot源码解析(二)启动第二阶段分析》
    《uboot源码解析(一)启动第一阶段——start.s分析》
    《uboot的目录结构说明》
    《uboot环境变量:详谈bootcmd 和bootargs》
    关于关注和取消关注的nodejs写法
    jquery去除字符串首尾空格的方法:$.trim()
    javascript数组去重算法-----3
    javascript数组去重算法-----2
    javascript数组去重算法-----1
  • 原文地址:https://www.cnblogs.com/yyanghang/p/11391541.html
Copyright © 2020-2023  润新知