(1)Javabean
1、JavaBean需要遵循的规范:类必须是public修饰;必须由公共的无参数构造器;包含属性的操作方法
2、获取属性的方法 :方法必须是public修饰;方法必须有返回类型;方法没有参数;若果数据类型是Boolean类型,此时方法是setXxx()
3、设置属性的方法:方法必须是public修饰;方法返回类型必须是void;方法必须是有参数的、
(2)内省机制
内省机制的目的:通过内省机制获取和操作JavaBean的成员信息(方法、事件、属性),核心类;java.bean.Introspector
person.java
public class person {
private Long id ;
private String username ;
private Integer age ;
private String classes ;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getClasses() {
return classes;
}
public void setClasses(String classes) {
this.classes = classes;
}
}
introspectorDemo.java
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
public class introspectorDemo {
public static void main(String[] args) throws Exception {
//获取字节码的描述对象
BeanInfo beaninfo = Introspector.getBeanInfo(person.class,Object.class);
//获取Javabean中所有属性的描述器
PropertyDescriptor[] pro = beaninfo.getPropertyDescriptors();
for (PropertyDescriptor propertyDescriptor : pro) {
System.out.println(propertyDescriptor);
//=========================
String name = propertyDescriptor.getName();//获取属性名
Class type = propertyDescriptor.getPropertyType();//获取属性类型
Method getter = propertyDescriptor.getReadMethod();//获取getter方法
Method setter = propertyDescriptor.getWriteMethod();//获取setter方法
System.out.println(name+","+type);
System.out.println(getter);
System.out.println(setter);
System.out.println("-----------------------");
}
}
}
(3)lombok(转载:https://blog.csdn.net/u010695794/article/details/70441432)
1、配置lombok依赖
public void setName(String name) {
this.name = name;
}
@NonNull,该注解快速判断是否为空,如果为空,则抛出java.lang.NullPointerException使用方法:
/**
-
author: andy
-
date: 17-4-22
-
blog: www.andyqian.com
*/
public class Person {private String name;
@Setter@Getter@NonNull
private List member;
}
等价源码:
@NonNull
private List members;
public Family(@NonNull final List members) {
if (members == null) throw new java.lang.NullPointerException(“members”);
this.members = members;
}
@NonNull
public List getMembers() {
return members;
}
public void setMembers(@NonNull final List members) {
if (members == null) throw new java.lang.NullPointerException(“members”);
this.members = members;
}
@Synchronized该注解自动添加到同步机制,有趣的是,生成的代码并不是直接锁方法,而是锁代码块, 作用范围是方法上使用方法:
private DateFormat format = new SimpleDateFormat(“MM-dd-YYYY”);
@Synchronized
public String synchronizedFormat(Date date) {
return format.format(date);
}
等价源码:
private final java.lang.Object $lock = new java.lang.Object[0];
private DateFormat format = new SimpleDateFormat(“MM-dd-YYYY”);
public String synchronizedFormat(Date date) {
synchronized ($lock) {
return format.format(date);
}
}
@ToString该方法大家应该非常熟悉,但需要注意的是:@ToString有多个属性可以进一步设置:
callSuper 是否输出父类的toString方法,默认为false
includeFieldNames 是否包含字段名称,默认为true
exclude 排除生成tostring的字段
使用方法:
/**
- author: andy
- date: 17-4-22
- blog: www.andyqian.com
*/
@ToString(callSuper = true,exclude ={“name”})
public class Person {
private String name;
private String address;
}
等价源码:
public String toString() {
return “Person{” +
“address=’” + address + ‘’’ +
‘}’;
}
@Cleanup,注释可用于确保已分配的资源被释放,如IO的连接关闭,使用方法:
public void testCleanUp() {
try {
@Cleanup ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write(new byte[] {‘Y’,‘e’,‘s’});
System.out.println(baos.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
等价源码:
public void testCleanUp() {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
baos.write(new byte[]{‘Y’, ‘e’, ‘s’});
System.out.println(baos.toString());
} finally {
baos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}