您的位置:首页 > 编程语言 > Java开发

java内省机制与反射实现工厂模式

2016-07-08 08:42 639 查看
1.什么是内省机制?

是Java语言对Bean类属性、事件的一种缺省处理方法。例如类A中有属性name,那我们可以通过getName,setName来得到其值或者设置新的值。通过getName/setName来访问name属性。

2.在使用内省机制时候的条件是?

类必须是具体的和公共的,并且具有无参数的构造器, 给定的所有属性必须私有,且提供get和set方法,设置get和set方法时候,必须把属性的首字母大写在属性名前添加get和set字样,通过自省机制发现和操作这些JavaBean的属性。

3.实现机制:

(1).   Introspector的静态方法getBeanInfo,获取BeanInfo对象信息

(2).   通过BeanInfo对象调用getPropertyDescriptors获取PropertyDescriptor(bean的所有属性描述)

(3).   调用getWriteMethod和getReadMethod获取当前属性的get和set机制函数

4.具体代码的实现;

定义一个学生类:

package introspect;

public class Student {
private String name;
private int age;
private String sex;
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 getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}


通过内省机制完成对类属性的操作:

package introspect;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Test {

public static void main(String[] args) throws IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {

Student dogbean = new Student();
//1. Introspector的静态方法getBeanInfo,获取BeanInfo对象信息
BeanInfo beaninfo = Introspector.getBeanInfo(dogbean.getClass());
//2.通过beaninfo对象获取所有的属性描述
PropertyDescriptor[] propertyDescriptors = beaninfo.getPropertyDescriptors();
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
//获取当前属性的名字
String name = propertyDescriptor.getName();
//获取当前属性的set方法
Method setMethod = propertyDescriptor.getWriteMethod();
if(name.equals("name")){
setMethod.invoke(dogbean, "小花");
}else if(name.equals("age")){
setMethod.invoke(dogbean, 1);
}else if(name.equals("sex")){
setMethod.invoke(dogbean, "女");
}
}
System.out.println("姓名:"+dogbean.getName()+" 年龄:"+dogbean.getAge()+"岁 性别:"+dogbean.getSex());
}

}

反射实现工厂模式

定义一个动物接口

public interface Animal {

}


创建动物的工厂

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.Properties;

public class AnimalFactory {
// 1.创建一个Properties对象
public static Properties pro = new Properties();
// 2.把properties文件中的数据加载到Properties对象中
static {
try {
// 定义流
InputStream ins = Test.class
.getResourceAsStream("/bean.properties");
// 把流加载到pro对象中
pro.load(ins);
} catch (Exception e) {
e.printStackTrace();
}
}

public static Animal createAnimal(String type) {
Animal animal = null;
try {
//1.根据类型获取指定的类的全路径
String path = (String)pro.get(type);
//2.加载指定类文件
Class classType = Class.forName(path);
//3.创建对象
animal = (Animal)classType.newInstance();

//内省机制帮我们加载属性到对象中
BeanInfo beanInfo = Introspector.getBeanInfo(classType);
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
if("class".equals(propertyDescriptor.getName())){
continue;
}
Method writeMethod = propertyDescriptor.getWriteMethod();
String value = pro.getProperty(propertyDescriptor.getName());
writeMethod.invoke(animal, value);
}
} catch (Exception e) {
e.printStackTrace();
}
return animal;
}

此处用到了 properties文件 java.util.Properties类

文件内容:

dogclass = com.huaxin.introspectFacotry.DogBean

pigclass = com.huaxin.introspectFacotry.PigBean

name = \u5C0F\u738B

type = \u6CF0\u8FEA

sex = \u7537

声明一个DogBean类

public class DogBean implements Animal{
private String type;
private String name;
private String sex;

public void setType(String type){
this.type = type;
}
public String getType(){
return type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
//输出当前对象的时候,得到的内容为toString返回的字符串
@Override
public String toString() {
return "dog类:姓名="+name+"  类型="+type+"  性别="+sex;
}

声明一个PigBean类

public class PigBean implements Animal{
private String type;
private String name;
private String sex;

public void setType(String type){
this.type = type;
}
public String getType(){
return type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
//输出当前对象的时候,得到的内容为toString返回的字符串
@Override
public String toString() {
return "pig类:姓名="+name+" 类型="+type+" 性别="+sex;
}



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: