javabean和注解

1.javabean及内省机制

1.1 javabean生成(利用lombok插件)

思路:导入lombok插件后,利用注释自动生成javabean
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

1.2 内省

1.2.1 主要用到的类

主要用到的类:
在这里插入图片描述
注意Introspector的两个方法(方法重载)
static BeanInfo getBeanInfo(Class<?> beanClass)
static BeanInfo getBeanInfo(Class<?> beanClass, Class<?> stopClass)
上面那个方法包括父类的属性,下面那个排除指定的父类

1.2.2 Demo1 利用反省机制为javabean属性赋值赋值

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

  1. 需要注意这种无参构造的快捷方式
    Person person = Person.class.newInstance();
  2. 注意这里用了getBeanInfo的方法的重载(两个参数,把Object 类的属性排除)

1.2.2 Demo2 javabean和map的相互转换

public class Bean2JavabeanTest {
	
	
	public static void main(String[] args) throws Exception {
//		Person person=new Person(15,"王晶");
//		Map<String, Object> 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. //注意这里读取javabean 属性信息的方法
    Object value = propertyDescriptor.getReadMethod().invoke(bean);
  2. public static T mapToBean(Map<String,Object> map,Class bean)
    //注意这里的参数有map 字节码对象(表示要把map的信息封装到哪个对象)
  3. 注意mapToBean 方法的返回值是object对象强转T
    return (T)bbb;

2.注解

2.1元注解

在这里插入图片描述

2.2自定义注解

//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

  1. 抽象方法的返回值(属性)必须是基本类型, String, Class, annotation, enumeration 和集合 (不能是Integer之类的引用类)
  2. 因为这里的属性即抽象方法 所有引用这个注解的时候必须要赋值(除非 default)
  3. 取出其属性的时候 调用方法的形式
    System.out.println(diyAnno.value());
  4. 因为数组类型没有重写 toString 方法 所以直接返回是地址值 可用Arrays.toString()转换成字符串再输出
    System.out.println(Arrays.toString(diyAnno.counts()));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值