码迷,mamicode.com
首页 > 编程语言 > 详细

JavaBean规范

时间:2018-10-13 00:03:41      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:首字母   main   tde   static   vat   依赖   出现   构造器   创建   

  JavaBean必须要为成员提供get/set方法,只提供其中的一个也是可以的,必须要有一个无参构造器,一般对于具有get或这个set方法的成员我们称之为属性,就算一个属性,没有对应的成员变量只有get/set方法,也是可以的。只有get方法的属性我们称为只读属性,属性的名称是get/set方法去除get/set之后,再将首字母变小写之后的名称,由set/get方法的名称来决定属性名称,而不是成员变量名,但是一般都是一致的。boolean类型的属性的get方法可以是is或者get开头。

package yu.bean;

/**
 * @Auther yusiming
 * @Date 2018/10/12 20:30
 */
public class Person {
    private String name;
    private int age;
    private String gender;

    // 必须提供无参构造器
    public Person() {
    }

    public Person(String name, int age, String gender) {
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    // 这个方法没有对应的成员,但是id也是一个属性
    public int getId() {
        return 11;
    }

    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 getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }
}

 

内省

  内省就是通过反射来操作javaBean,但是内省使用起来更加方便一点,因为它掩盖了反射的操作,内省的过程,Introspector这个类叫做内省类

public class Test {
    public static void main(String[] args) {
        Person person = new Person();
        try {
            // BeanInfo是一个javaBean类型的信息类
            BeanInfo beanInfo = Introspector.getBeanInfo(Person.class);
            // 通过BeanInfo可以得到所有的属性描述符对象,
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            Method readMethod = propertyDescriptors[0].getReadMethod();
            Method writeMethod = propertyDescriptors[0].getWriteMethod();
            writeMethod.invoke(person, 11);
            System.out.println(person.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

  但是这样操作bean还是太麻烦了,我们可以使用Apache提供的工具,commons-beanutils来完成对bean的操作,commons-beanutils对内省进行了进一步的封装,使得对bean的操作更加方便,要使用commons-beanutils首先要导入JAR包:

  • commons-beanutils.jar
  • commons-logging.jar 

  这两jar包之间有依赖关系,

  使用BeanUtils设置属性和获取属性

public class Test {
    public static void main(String[] args) throws Exception {
        String className = "yu.bean.Person";
        Class<?> clazz = Class.forName(className);
        Object bean = clazz.getDeclaredConstructor().newInstance();
        // 设置属性
        BeanUtils.setProperty(bean, "name", "张三");
        BeanUtils.setProperty(bean, "age", 20);
        BeanUtils.setProperty(bean, "gender", "男");
        // Person{name=‘张三‘, age=20, gender=‘男‘}
        System.out.println(bean.toString());
        // 获取属性
        String age = BeanUtils.getProperty(bean, "age");
        // 20
        System.out.println(age);
    }
}

  使用BeanUtils将map中的数据封装到bean中,注意map中的键必须要与bean的属性名一致,才能够封装进去

public class Test {
    public static void main(String[] args) throws Exception {
        Map<String, String> map = new HashMap<>();
        map.put("username", "张三");
        map.put("password", "123");
        User user = new User();
        BeanUtils.populate(user, map);
        // User{username=‘张三‘, password=‘123‘}
        System.out.println(user.toString());
    }
}

  我们可以对BeanUtils再次封装

public static <T> T toBean(Map map, Class<T> clazz) {
    try {
        T bean = clazz.getDeclaredConstructor().newInstance();
        BeanUtils.populate(bean, map);
        return bean;
     } catch (Exception e) {
         throw new RuntimeException(e);
     }
}

   封装之后再次封装map中的数据到bean中,

public class Test {
    public static void main(String[] args) throws Exception {
        Map<String, String> map = new HashMap<>();
        map.put("username", "张三");
        map.put("password", "123");
        User user = CommonUtils.toBean(map, User.class);
        // User{username=‘张三‘, password=‘123‘}
        System.out.println(user.toString());
    }
}

  

 

JSP中与JavaBean相关的标签

  • <jsp:useBean>:创建或者查找bean,可以指定在哪一个域中创建或者查找bean  
  • <jsp:setProperty>:给bean设置属性
  • <jsp:getProperty>:获取bean的属性

  使用方法:

<%--创建或者查找bean,不能重复出现--%>
<jsp:useBean id="user" class="yu.bean.User" scope="request"/>
<jsp:setProperty name="user" property="username" value="张三"/>
<jsp:setProperty name="user" property="password" value="123"/>
<jsp:getProperty name="user" property="username"/>
<jsp:getProperty name="user" property="password"/>

  

 

JavaBean规范

标签:首字母   main   tde   static   vat   依赖   出现   构造器   创建   

原文地址:https://www.cnblogs.com/yusiming/p/9780726.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!