利用注解和反射将map转化成bean
2016-08-06 16:07
295 查看
一、java注解
java注解相关的类放在 java.lang.annotation 包下,其主要内容包含4个元注解和其中两个元注解的枚举类值,相对比较简单,从jdk问答即可了解
4个元注解:
Documented:指示某一类型的注释将通过 javadoc 和类似的默认工具进行文档化。应使用此类型来注释这些类型的声明:其注释会影响由其客户端注释的元素的使用。如果类型声明是用
Documented 来注释的,则其注释将成为注释元素的公共 API 的一部分。
Inherited:指示注释类型被自动继承。如果在注释类型声明中存在 Inherited
元注释,并且用户在某一类声明中查询该注释类型,同时该类声明中没有此类型的注释,则将在该类的超类中自动查询该注释类型。此过程会重复进行,直到找到此类型的注释或到达了该类层次结构的顶层
(Object) 为止。如果没有超类具有该类型的注释,则查询将指示当前类没有这样的注释。
注意,如果使用注释类型注释类以外的任何事物,此元注释类型都是无效的。还要注意,此元注释仅促成从超类继承注释;对已实现接口的注释无效。
Retention:指示注释类型的注释要保留多久。如果注释类型声明中不存在 Retention 注释,则保留策略默认为
RetentionPolicy.CLASS。
只有元注释类型直接用于注释时,Target 元注释才有效。如果元注释类型用作另一种注释类型的成员,则无效。
RetentionPolicy.SOURCE ---------------------------------注解将被编译器丢弃
RetentionPolicy.CLASS -----------------------------------注解在class文件中可用,但会被VM丢弃
RetentionPolicy.RUNTIME VM-------将在运行期也保留注释,因此可以通过反射机制读取注解的信息。
Target: 指示注释类型所适用的程序元素的种类。如果注释类型声明中不存在 Target
元注释,则声明的类型可以用在任一程序元素上。如果存在这样的元注释,则编译器强制实施指定的使用限制
ElemenetType.CONSTRUCTOR----------------------------构造器声明
ElemenetType.FIELD --------------------------------------域声明(包括 enum 实例)
ElemenetType.LOCAL_VARIABLE------------------------- 局部变量声明
ElemenetType.METHOD ----------------------------------方法声明
ElemenetType.PACKAGE --------------------------------- 包声明
ElemenetType.PARAMETER ------------------------------参数声明
ElemenetType.TYPE--------------------------------------- 类,接口(包括注解类型)或enum声明
二、注解例子
该例子有3个文件:注解类Annotation_1、使用注解类UseAnnotation、解析注解类ParseAnnotation
三、利用注解和反射将map转化成bean
该例子使用注解和反射的技术将map类型转化成对应的bean
主要包含4个类:注解类PojoAnnotation、bean类User、转换工具类PojoMappingUtil、测试类PojoTest
java注解相关的类放在 java.lang.annotation 包下,其主要内容包含4个元注解和其中两个元注解的枚举类值,相对比较简单,从jdk问答即可了解
4个元注解:
Documented:指示某一类型的注释将通过 javadoc 和类似的默认工具进行文档化。应使用此类型来注释这些类型的声明:其注释会影响由其客户端注释的元素的使用。如果类型声明是用
Documented 来注释的,则其注释将成为注释元素的公共 API 的一部分。
Inherited:指示注释类型被自动继承。如果在注释类型声明中存在 Inherited
元注释,并且用户在某一类声明中查询该注释类型,同时该类声明中没有此类型的注释,则将在该类的超类中自动查询该注释类型。此过程会重复进行,直到找到此类型的注释或到达了该类层次结构的顶层
(Object) 为止。如果没有超类具有该类型的注释,则查询将指示当前类没有这样的注释。
注意,如果使用注释类型注释类以外的任何事物,此元注释类型都是无效的。还要注意,此元注释仅促成从超类继承注释;对已实现接口的注释无效。
Retention:指示注释类型的注释要保留多久。如果注释类型声明中不存在 Retention 注释,则保留策略默认为
RetentionPolicy.CLASS。
只有元注释类型直接用于注释时,Target 元注释才有效。如果元注释类型用作另一种注释类型的成员,则无效。
RetentionPolicy.SOURCE ---------------------------------注解将被编译器丢弃
RetentionPolicy.CLASS -----------------------------------注解在class文件中可用,但会被VM丢弃
RetentionPolicy.RUNTIME VM-------将在运行期也保留注释,因此可以通过反射机制读取注解的信息。
Target: 指示注释类型所适用的程序元素的种类。如果注释类型声明中不存在 Target
元注释,则声明的类型可以用在任一程序元素上。如果存在这样的元注释,则编译器强制实施指定的使用限制
ElemenetType.CONSTRUCTOR----------------------------构造器声明
ElemenetType.FIELD --------------------------------------域声明(包括 enum 实例)
ElemenetType.LOCAL_VARIABLE------------------------- 局部变量声明
ElemenetType.METHOD ----------------------------------方法声明
ElemenetType.PACKAGE --------------------------------- 包声明
ElemenetType.PARAMETER ------------------------------参数声明
ElemenetType.TYPE--------------------------------------- 类,接口(包括注解类型)或enum声明
二、注解例子
该例子有3个文件:注解类Annotation_1、使用注解类UseAnnotation、解析注解类ParseAnnotation
package com.annotation; 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.CONSTRUCTOR, ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) public @interface Annotation_1 { String name(); int id() default 0; Class gid(); }
package com.annotation; import java.lang.reflect.Method; @Annotation_1(name="useAnnotation", id=1, gid=Long.class) public class UseAnnotation { @Annotation_1(name="a", gid=Integer.class) public int a; @Annotation_1(name="constractor_method", gid=Method.class) public UseAnnotation(){ } @Annotation_1(name="getA", id=2, gid=Method.class) public int getA(){ return this.a; } @Annotation_1(name="setA", id=2, gid=Method.class) public void setA(int a){ this.a = a; } }
package com.annotation; import java.lang.annotation.Annotation; import java.lang.reflect.AccessibleObject; import java.lang.reflect.Field; import java.lang.reflect.Method; public class ParseAnnotation { /** * 解析类的注解 * @throws ClassNotFoundException */ public void parseClass() throws ClassNotFoundException{ // Class clazz = Class.forName("com.annotation.UseAnnotation"); Annotation[] as = UseAnnotation.class.getAnnotations(); for(Annotation a: as){ Annotation_1 a1 = (Annotation_1)a; System.out.println("CLASS: name=" + a1.name() + ", id=" + a1.id() + ", gid=" + a1.gid()); } } /** * 解析方法的注解 */ public void parseMethod(){ Method[] methods = UseAnnotation.class.getMethods(); this.parse(methods); } /** * 解析成员变量的注解 */ public void parseField(){ Field[] fields = UseAnnotation.class.getFields(); this.parse(fields); } private <T extends AccessibleObject> void parse(T[] ts){ for(T t: ts){ Annotation annotation = t.getAnnotation(Annotation_1.class); if(annotation == null){ System.out.println(t.getClass().getName() + " do not have Annotation_1"); }else{ Annotation_1 a1 = (Annotation_1)annotation; System.out.println(t.getClass().getName() + ": name=" + a1.name() + ", id=" + a1.id() + ", gid=" + a1.gid()); } } } public static void main(String[] args) throws ClassNotFoundException { ParseAnnotation pa = new ParseAnnotation(); pa.parseClass(); pa.parseMethod(); pa.parseField(); } }
三、利用注解和反射将map转化成bean
该例子使用注解和反射的技术将map类型转化成对应的bean
主要包含4个类:注解类PojoAnnotation、bean类User、转换工具类PojoMappingUtil、测试类PojoTest
package com.annotation.userdemo; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @Retention(RetentionPolicy.RUNTIME) public @interface PojoAnnotation { String name(); }
package com.annotation.userdemo; public class User { @PojoAnnotation(name="NAME") private String name; @PojoAnnotation(name="AGE") private Integer age; 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; } @Override public String toString() { return "User [name=" + name + ", age=" + age + "]"; } }
package com.annotation.userdemo; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Map; public class PojoMappingUtil { /** * 将map中key与注解一致的值放到对应成员变量中 */ public static <T> T parsePoje(Map<String, Object> map, Class<T> clazz) throws InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException{ T t = clazz.newInstance(); Field[] fields = clazz.getDeclaredFields(); for(Field field : fields){ PojoAnnotation pojoAnnotation = field.getAnnotation(PojoAnnotation.class); if(pojoAnnotation != null){ Object o = map.get(pojoAnnotation.name()); if(o != null){ String fieldName = field.getName(); Method method = clazz.getMethod("set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1), o.getClass()); method.invoke(t, o); } } } return t; } }
package com.annotation.userdemo; import java.lang.reflect.InvocationTargetException; import java.util.HashMap; import java.util.Map; public class PojoTest { public static void main(String[] args) throws InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException { Map<String, Object> map = new HashMap<String , Object>(); map.put("NAME", "小明"); map.put("AGE", 10); User user = PojoMappingUtil.parsePoje(map, User.class); System.out.println(user); } }
相关文章推荐
- 利用java反射机制实现List<Map<String, Object>>转化为List<JavaBean>
- Spring 配置bean, 注解方式配置 xml方式要生产set方法方便属性注入,注解方式不需要,利用反射机制注入
- 利用反射机制 实现 javabean 转化为 map(效率低,仅限学习)
- 利用Java的反射机制实现Bean实体与Map自动转换的工具类
- android中的反射应用场景分析(map转化为bean工具)
- 编码实现Spring 利用@Resource注解实现bean的注入,xml实现基本数据类型的注入
- Java注解(Annotation)用法:利用注解和反射机制指定列名导出数据库数据
- 利用反射机制从DB取数据转化为Entity的全过程
- 利用Java反射机制将Bean转成Map
- bean转化成map
- 利用反射实现简单的Spring注解注入实例
- 如何利用反射获得注解的名称和内部的值
- 利用反射与dom4j读取javabean生成对应XML和读取XML得到对应的javabean对象集合
- Java利用spring注解做反射机制
- 利用java反射实现两个具有相同属性bean赋值
- 利用java的反射来实现java的类对象转化为json对象
- 使用反射机制 来把一个具体的bean 处理成一个map。并且获得其中的field和值
- Java 反射与cglib.proxy与cglib.beanmap与直接赋值 性能对比
- 利用反射与dom4j读取javabean生成对应XML和读取XML得到对应的javabean对象集合
- 利用Gson将Json 转化为Map,javaBean, List转换为json