★ 准备工作
定义一个Model类,里面所有的属性都是private的,然后为每个属性提供getter和setter方法;
再准备一个Map,map的key值都是类里面的属性字段的字符串表示,值任意。
★ 真正的工作
设计一个方法Object getModel(Map map,Class cls),传入一个包含所有值的Map,然后再传入Model类的class,
那么返回Model类的实例,这个实例里面已经包含好了所有相关的数据。
也就是把Map中的数据通过反射,设置回到Model类实例中。
1)核心代码---模仿spring
package reflect2.spring;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
public class SpringDaoUtil {
/**
* 给定Map<K, V> map与Class<C> cls,
* 返回一个值对象。
* 目的:模仿spring的工作原理
*/
public static <V, C> C getModel(Map<String, V> map, Class<C> cls)
throws InstantiationException, IllegalAccessException,
SecurityException, NoSuchMethodException, IllegalArgumentException,
InvocationTargetException {
C instan = cls.newInstance();
Field[] flds = cls.getDeclaredFields();
for (Field f : flds) {
String key = f.getName();
V v = map.get(key);
if (v == null) {
System.out.println("字段 " + key + " 为空!");
} else {
// 定义形参
String methodName = "set" + key.substring(0, 1).toUpperCase()
+ key.substring(1);
@SuppressWarnings("rawtypes")
Class[] parameterTypes = new Class[] { f.getType() };
Method m = cls.getDeclaredMethod(methodName, parameterTypes);
// 传递实参
Object[] objs = new Object[] { v };
m.invoke(instan, objs);
}
}
return instan;
}
}
2)测试代码
测试图片
package reflect2.spring;
import java.util.HashMap;
import java.util.Map;
import reflect2.spring.vo.Person;
import reflect2.spring.vo.UserModel;
public class Client {
public static void main(String[] args) {
try {
Map<String, Object> map = new HashMap<String, Object>();
map.put("uuid", "001");
map.put("name", "Lucy");
map.put("type", Integer.valueOf(3));
map.put("pwd", "222");
UserModel user = SpringDaoUtil.getModel(map, UserModel.class);
System.out.println(user);
System.out.println("-------测试事件分割线----------");
Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("name", "Lily");
Person p = SpringDaoUtil.getModel(map2, Person.class);
System.out.println(p);
} catch (Exception e) {
e.printStackTrace();
}
}
}
3)测试数据(值对象)-----JavaBean
package reflect2.spring.vo;
public class Person {
private String name;
public int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public Person(){
}
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;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
/**
*
*/
package reflect2.spring.vo;
public class UserModel{
private String uuid;
private String name;
private int type;
private String pwd;
public UserModel(String uuid, String name, int type, String pwd) {
this.uuid = uuid;
this.name = name;
this.type = type;
this.pwd = pwd;
}
public UserModel(String uuid, int type){
this.uuid = uuid;
this.type = type;
}
public UserModel(){
}
private UserModel(String uuid){
this.uuid = uuid;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String toString() {
return "{"+uuid+","+name+","+type+","+pwd+"}";
}
}
------