思路:导入lombok插件后,利用注释自动生成javabean
主要用到的类:
注意Introspector的两个方法(方法重载)
static BeanInfo getBeanInfo(Class> beanClass)
static BeanInfo getBeanInfo(Class> beanClass, Class> stopClass)
上面那个方法包括父类的属性,下面那个排除指定的父类
public class PersonBeanTest {
@Test
public void test() throws Exception {
//1.获得所有属性,并获得其相关信息
//2.利用内省 反射来给javabean 的属性赋值
BeanInfo beanInfo = Introspector.getBeanInfo(Person.class,Object.class);
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
//需要注意这种无参构造的快捷方式
Person person = Person.class.newInstance();
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
// System.out.println(propertyDescriptor.getName());
// System.out.println(propertyDescriptor.getPropertyType());
// System.out.println(propertyDescriptor.getWriteMethod());
// System.out.println(propertyDescriptor.getReadMethod())
Method writeMethod = propertyDescriptor.getWriteMethod();
if(propertyDescriptor.getName().equals("name")) {
writeMethod.invoke(person, "名字") ;
}else if(propertyDescriptor.getName().equals("age")){
writeMethod.invoke(person, 100);
}
}
System.out.println(person);
}
}
Attention
public class Bean2JavabeanTest {
public static void main(String[] args) throws Exception {
// Person person=new Person(15,"王晶");
// Map mapToBean = beanToMap(person);
// System.out.println(mapToBean);
Person person=new Person();
Map<String,Object> map=new HashMap<>();
map.put("name", "哈哈哈");
map.put("age", 17);
Person result = mapToBean(map,Person.class);
System.out.println(result);
}
//1.javabean 转map
public static Map<String,Object> beanToMap(Object bean) throws Exception {
BeanInfo beanInfo=Introspector.getBeanInfo(bean.getClass(),Object.class);
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
Map<String,Object> map=new HashMap<String, Object>();
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
String key = propertyDescriptor.getName();
//注意这里读取javabean 属性信息的方法
Object value = propertyDescriptor.getReadMethod().invoke(bean);
map.put(key, value);
}
return map;
}
//2.map转 javabean
public static <T> T mapToBean(Map<String,Object> map,Class<T> bean) throws Exception {
//注意这里的参数有map 和 字节码对象(表示要把map的信息封装到哪个对象)
T bbb = bean.newInstance();
BeanInfo beanInfo = Introspector.getBeanInfo(bean, Object.class);
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
String key = propertyDescriptor.getName();
Object value = map.get(key);
propertyDescriptor.getWriteMethod().invoke(bbb, value);
}
return (T)bbb;
//注意返回值是Object对象强转成T
}
}
Attention
//1.元注解
//2.抽象方法(属性)
// 1.必须是基本类型, String, Class, annotation, enumeration 和集合 (不能是Integer之类的引用类)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface DiyAnno {
String value();
String name();
//2.因为这里的属性即抽象方法 所有引用这个注解的时候必须要赋值(除非 default)
int age() default 18;
int[] counts();
}
@DiyAnno(name="王晶",age=15,counts= {
1,2,3}, value = "我是value")
public class Person {
}
public class AnnoTest {
public static void main(String[] args) {
Annotation[] annotations = Person.class.getAnnotations();
for (Annotation annotation : annotations) {
System.out.println(annotation);
}
DiyAnno diyAnno = Person.class.getAnnotation(DiyAnno.class);
//3.展示其属性的时候 调用方法的形式
System.out.println(diyAnno.value());
System.out.println(diyAnno.age());
System.out.println(diyAnno.name());
//4.因为数组类型没有重写 toString 方法 所以直接返回是地址值 可用Arrays.toString()转换成字符串再输出
System.out.println(Arrays.toString(diyAnno.counts()));
}
}
Attention