javaBeans 属性的概念

不只是字段,而是其get set 方法

且该get方法有返回值的称为属性,继承Object类的getClass方法

package com.swift.demo1;

public class Person {
String name;
int age;
String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getAd() {//这个算一个属性,虽让没有字段,但如果没有返回值不算一个属性
return "getAd.....";
}
}

属性个数

package com.swift.demo1;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor; import org.junit.jupiter.api.Test; public class TestIntro {
@Test
public void test1() throws Exception {
BeanInfo info=Introspector.getBeanInfo(Person.class);
PropertyDescriptor[] pds=info.getPropertyDescriptors();
for(PropertyDescriptor des:pds) {
System.out.println(des.getName());
}
}
}

阻止父类的getClass属性用

package com.swift.demo1;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor; import org.junit.jupiter.api.Test; public class TestIntro {
@Test
public void test1() throws Exception {
BeanInfo info=Introspector.getBeanInfo(Person.class,Object.class);
PropertyDescriptor[] pds=info.getPropertyDescriptors();
for(PropertyDescriptor des:pds) {
System.out.println(des.getName());
}
}
}

BeanUtils使用jar包

需要两个:

都可以在Apache网站下载

BeanUtils具有比Introspector更强大的功能,可以在基本数据类型间直接转换,也可以把文本框中的字符串通过注册器转换器进行转换

自己转日期格式

package com.swift.demo1;

import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date; import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.junit.Test; public class TestUtils {
@Test
public void test() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { Person p=new Person(); ConvertUtils.register(new Converter() { @Override
public Object convert(Class type, Object value) {
String str=(String) value;
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
try {
return sdf.parse(str);
} catch (ParseException e) {
throw new RuntimeException(e);//
}
}
}, Date.class); BeanUtils.setProperty(p, "name", "swift");
BeanUtils.setProperty(p, "age", "30");
BeanUtils.setProperty(p, "password", "123");
BeanUtils.setProperty(p, "date", "2018-02-19"); System.out.println(p.getName());
System.out.println(BeanUtils.getProperty(p, "name"));
System.out.println(BeanUtils.getProperty(p, "age"));
System.out.println(BeanUtils.getProperty(p, "password"));
System.out.println(BeanUtils.getProperty(p, "date"));
}
}

可以用现成的

package com.swift.demo1;

import java.lang.reflect.InvocationTargetException;
import java.util.Date; import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;
import org.junit.Test; public class TestUtils {
@Test
public void test() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { Person p=new Person(); ConvertUtils.register(new DateLocaleConverter(),Date.class); BeanUtils.setProperty(p, "name", "swift");
BeanUtils.setProperty(p, "age", "30");
BeanUtils.setProperty(p, "password", "123");
BeanUtils.setProperty(p, "date", "2018-02-19"); System.out.println(p.getName());
System.out.println(BeanUtils.getProperty(p, "name"));
System.out.println(BeanUtils.getProperty(p, "age"));
System.out.println(BeanUtils.getProperty(p, "password"));
System.out.println(BeanUtils.getProperty(p, "date"));
}
}

集合map加到BeanUtils

package com.swift.demo1;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map; import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;
import org.junit.Test; public class TestUtils {
@Test
public void test() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { Person p=new Person(); ConvertUtils.register(new DateLocaleConverter(),Date.class); BeanUtils.setProperty(p, "name", "swift");
BeanUtils.setProperty(p, "age", "30");
BeanUtils.setProperty(p, "password", "123");
BeanUtils.setProperty(p, "date", "2018-02-19"); System.out.println(p.getName());
System.out.println(BeanUtils.getProperty(p, "name"));
System.out.println(BeanUtils.getProperty(p, "age"));
System.out.println(BeanUtils.getProperty(p, "password"));
System.out.println(BeanUtils.getProperty(p, "date"));
} @Test
public void test1() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { Person p=new Person(); ConvertUtils.register(new DateLocaleConverter(),Date.class); Map<String, String> map=new HashMap<String, String>();
map.put("name", "swift");
map.put("age", "30");
map.put("password", "123");
map.put("date", "2018-02-19"); BeanUtils.populate(p, map); System.out.println(p.getName());
System.out.println(BeanUtils.getProperty(p, "name"));
System.out.println(BeanUtils.getProperty(p, "age"));
System.out.println(BeanUtils.getProperty(p, "password"));
System.out.println(BeanUtils.getProperty(p, "date"));
}
}

javabeans 内省 introspector BeanUtils的更多相关文章

  1. JavaBeans与内省(Introspector)

    JavaBean与Introspector 反射和内省操作很多时候都是在以后要做框架的时候作用非常大.    现在你学的是面向对象编程,即:你所写代码都能够找到对应的类或接口,找到具体的方法写出对应的 ...

  2. Java 内省(Introspector)和 BeanUtils

    人生若只如初见,何事秋风悲画扇. 概述 内省(Introspector) 是Java 语言对 JavaBean 类属性.事件的一种缺省处理方法. JavaBean是一种特殊的类,主要用于传递数据信息, ...

  3. 内省(introspector)------>JavaBean

    内省(introspector)------>JavaBean    1.问什么要学内省?        开发框架时,经常需要Java对象的属性来来封装程序的数据,每次使用反射技术完成此操作过于 ...

  4. 内省(Introspector)

    内省(Introspector) 是Java 语言对 JavaBean 类属性.事件的一种缺省处理方法 目的:主要用于传递数据信息,这种类中的方法主要用于访问私有的字段(且方法名符合某种命名规则) p ...

  5. JavaEE JavaBean 反射、内省、BeanUtils

    JavaEE JavaBean 反射.内省.BeanUtils @author ixenos JavaBean是什么 一种规范,表达实体和信息的规范,便于封装重用. 1.所有属性为private2.提 ...

  6. 深入理解Java:内省(Introspector)

    深入理解Java:内省(Introspector) 内省(Introspector) 是Java 语言对 JavaBean 类属性.事件的一种缺省处理方法. JavaBean是一种特殊的类,主要用于传 ...

  7. JAVA中反射机制五(JavaBean的内省与BeanUtils库)

    内省(Introspector) 是Java 语言对JavaBean类属性.事件的一种缺省处理方法. JavaBean是一种特殊的类,主要用于传递数据信息,这种类中的方法主要用于访问私有的字段,且方法 ...

  8. Java 内省(Introspector)深入理解

    Java 内省(Introspector)深入理解 一些概念: 内省(Introspector) 是Java 语言对 JavaBean 类属性.事件的一种缺省处理方法. JavaBean是一种特殊的类 ...

  9. 【小家Spring】聊聊Spring中的数据绑定 --- BeanWrapper以及内省Introspector和PropertyDescriptor

    #### 每篇一句 > 千古以来要饭的没有要早饭的,知道为什么吗? #### 相关阅读 [[小家Spring]聊聊Spring中的数据转换:Converter.ConversionService ...

随机推荐

  1. pyhon虚拟环境的安装和使用

    安装Python2.7: 1.Mac下使用Python2.7 2.Windows下安装Python2.7. *从python官网下载python2.7的版本 *双击python2.7,然后选择安装路径 ...

  2. ANDROID_HOME is not set and "android" command not in your PATH解决

    使用nodejs安装cordova后在项目里面添加平台时出现错误: 原因就是没有配环境变量 使用phonegap开发不仅要配JDK环境变量,还要配ADT环境变量,出现这个错误很显示就是没配ADT环境变 ...

  3. webpack.config.js====图片处理

    1. 安装依赖: cnpm install --save-dev url-loader image-webpack-loader html-loader 2. webpack.config.js规则的 ...

  4. Swift UI开发初探 (转)

    原文地址:http://www.tairan.com/archives/6600 关于Swift语法,可以参考<Apple Swift编程语言入门教程> 效果如下: 开发环境 Xcode6 ...

  5. ActiveMQ实例1--简单的发送和接收消息

    一.环境准备 1,官网http://activemq.apache.org/下载最新版本的ActiveMQ,并解压 2,打开对应的目录,在Mac环境下,一般可以运行命令: cd /Users/***/ ...

  6. MarkDown 编辑器学习

    MarkDown 编辑器学习 是一种简单快键的文字排版工具,可以用于编写说明文档,鉴于其语法简洁明了,且其渲染生成的样式简单美观,很多开发者也用它来写博客,已被国内外很多流行博客平台所支持.生成的文件 ...

  7. Js仿腾讯微博效果

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  8. 思科双出口+策略路由+NAT

    使用策略路由,从教育网出去的,在教育网接口进行nat转换 访问教育网资源平时走教育网,故障走电信 访问internat走电信线路,故障走教育网 服务器静态绑定教育网ip,不管电信.联通.教育网都走教育 ...

  9. "COM Surrogate 已停止工作"解决方案(windows7 64位及32位)

    根据图示步骤,将以下文件添加至“数据执行保护”的例外列表中. 64位:C:Windows\SysWOW64\dllhost.exe 32位:C:\Windows\System32\dllhost.ex ...

  10. 初学React:JSX语法

    这是本人初学React做的学习笔记;讲的不是很深,只算是简单的进行介绍. 这是一个小系列.都是在同一个模板中搭建的,但是代码是不能正常执行的. >>第一个组件.js 'use strick ...