【java--反射】注解(反射解析注解+注解应用)
创建实体类 package cn.com.codingce.iocannotation; /** * @Author: Jiangjun * @Date: 2019/10/7 9:54 */ public class Person { private String name; private Integer age; private String sex; (get set方法省略) public Person(String name,Integer age,String sex) { this.name = name; this.age = age; this.sex = sex; } @MyAnnotation({"掌上编程","18","男"}) public Person() { } @Override public String toString() { return "Person{" + "name='" + name + ''' + ",age=" + age + ",sex='" + sex + ''' + '}'; } } MySpring类 package cn.com.codingce.iocannotation; import java.lang.annotation.Annotation; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; /** * @Author: Jiangjun * @Date: 2019/10/7 10:15 */ public class MySpring { public Object getBean(String className) { Object obj = null; try { Class clazz = Class.forName(className); Constructor con = clazz.getConstructor(); obj = con.newInstance(); Annotation a = con.getAnnotation(MyAnnotation.class); Class aclazz = a.getClass(); Method amethod = aclazz.getMethod("value"); String[] values = (String[]) amethod.invoke(a); System.out.println("注解内获取的属性值》》》》》》》" + values.length); Field[] fields = clazz.getDeclaredFields(); System.out.println("从类中获取的全部属性包括私有》》》》》" + fields.length); for(int i = 0;i<fields.length;i++){ /** * 获取属性名字 * 处理set方法字符串 * 第一个字母大写 * 拼接字符串 StringBuilder * 通过处理好的set方法名字找到对应的set方法 * 执行找到的set方法 * 需要将注解读取的值转换成属性类型对应的值 属性类型的构造方法 (Integer String) */ String fieldName = fields[i].getName(); String firstLetter = fieldName.substring(0,1).toUpperCase(); String otherLetter = fieldName.substring(1); StringBuilder setMethodName = new StringBuilder("set"); setMethodName.append(firstLetter); setMethodName.append(otherLetter); Class fieldType = fields[i].getType(); Method setMethod = clazz.getMethod(setMethodName.toString(),fieldType); setMethod.invoke(obj,fieldType.getConstructor(String.class).newInstance(values[i])); } } catch (Exception e) { e.printStackTrace(); } return obj; } } 自定义注解 package cn.com.codingce.iocannotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import static java.lang.annotation.ElementType.ANNOTATION_TYPE; /** * @Author: Jiangjun * @Date: 2019/10/7 9:57 */ @Target({ANNOTATION_TYPE,ElementType.METHOD,ElementType.FIELD,ElementType.CONSTRUCTOR}) @Retention(RetentionPolicy.RUNTIME) public @interface MyAnnotation { String[] value(); } 主函数测试 package cn.com.codingce.iocannotation; /** * @Author: Jiangjun * @Date: 2019/10/7 10:52 */ public class TestMain { public static void main(String[] args) { /** * 获取Person对象 * 之前 Person p = new Person(); p.setName("掌上编程"); * 改变处理 不用自己处理 跟被人要 MySpring * 对象创建权力反转(IOC) 管理 赋值(自动) 别人处理 * ms 管理者 帮助我们创建对象 自动赋值 */ MySpring ms = new MySpring(); Person p = (Person) ms.getBean("cn.com.codingce.iocannotation.Person"); System.out.println(p.toString()); } } 运行细节 最后运行结果 原文链接 https://blog.csdn.net/weixin_43874301/article/details/102295531 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |