模拟set注入
spring的注入方式主要有set注入和构造注入,本文将使用反射技术模拟set注入;
spring中set注入
首先是set注入的方式,需要在配置文件中配置Bean
<bean id="wifeBean" class="com.xidu.spring.damain.Wife" scope="singleton">
<property name="name" value="织女"/>
<property name="husband" ref="husbandBean"/>
</bean>
spring 容器会根据配置信息配置类型,在内部创建对象,供用户使用。
反射模拟set注入
xml脚本提供用户配置信息,作者将需用户配置的信息放置于map集合中(方便)。一下直接看代码吧;
User.java
public class User {
//public、private
public String name;
public int age;
private String address;
//无参构造
public User() {
}
//有参构造
public User(String name, int age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
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 "User{" +
"name='" + name + '\'' +
", age=" + age +
", address='" + address + '\'' +
'}';
}
}
SetclassBySet.java
import java.lang.reflect.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
//根据set方法创建类
public class SetClassBySet {
public static void main(String[] args) {
//反射将类的属性赋值
//准备数据;
Map<String,String> filedValueMap = new HashMap();
filedValueMap.put("name","张三");
filedValueMap.put("age","15");
filedValueMap.put("address","北京");
//根据输入类,获得类名
User user = new User();
String className = user.getClass().toString().split(" ")[1];
try {
//根据类型获取无参构造
Class<?> aClass = Class.forName(className);
Constructor<?> declaredConstructor = aClass.getDeclaredConstructor();
//根据无参构造创建对象;
Object agency = declaredConstructor.newInstance();
//根据输入的map,获得map集合中的key
Set<String> keySet = filedValueMap.keySet();
//迭代map集合
Iterator<String> iterator = keySet.iterator();
while (iterator.hasNext()) {
//获得属性名
String fieldName = iterator.next();
//获得该属性名的类型
Class<?> fieldType = aClass.getDeclaredField(fieldName).getType();
//构建set方法名
String setMethodName = "set"+fieldName.toUpperCase().charAt(0)+fieldName.substring(1);
//根据set方法名和输入的类型获得set方法;
Method declaredMethod = aClass.getDeclaredMethod(setMethodName,fieldType);
//获得set方法中所有输入变量;
Parameter[] parameters = declaredMethod.getParameters();
//因为是set方法,所以仅有一个,只对第一个操作即可
Parameter parameter = parameters[0];
//获得输入属性的类型
String fieldTypeName = parameter.getType().getSimpleName();
//根据类型重新调整输入类型的值;
Object filedValue = setValueByFiledType(fieldTypeName, filedValueMap.get(fieldName));
declaredMethod.invoke(agency,filedValue);
}
//打印输出
System.out.println(agency);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
}
/**
* 根据属性类型,修改value类型
* @param filedType
* @param value
*/
public static Object setValueByFiledType(String filedType,Object value){
//byte int short long float double boolean char
//Byte Integer Short Long Float Double Boolean Char
//String
if (filedType.equals("byte") || filedType.equals("Byte")) {
value = Byte.parseByte(value.toString());
}
if (filedType.equals("int") || filedType.equals("Integer")) {
value = Integer.parseInt(value.toString());
}
if (filedType.equals("short") || filedType.equals("Short")) {
value = Short.parseShort(value.toString());
}
if (filedType.equals("long") || filedType.equals("Long")) {
value = Long.parseLong(value.toString());
}
if (filedType.equals("float") || filedType.equals("Float")) {
value = Float.parseFloat(value.toString());
}
if (filedType.equals("double") || filedType.equals("Double")) {
value = Double.parseDouble(value.toString());
}
if (filedType.equals("boolean") || filedType.equals("Boolean")) {
value = Boolean.parseBoolean(value.toString());
}
if (filedType.equals("char") || filedType.equals("Character")) {
value = value.toString().charAt(0);
}
if (filedType.equals("String")) {
value = value;
}
return value;
}
}