1.通过对象调用的方法,执行注解的value方法
2.通过反射执行注解的value方法
注解类
封装的MySpring.java
package org.example.test;
import javax.security.auth.Subject;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class MySpring {
public Object getBean(String className){
Object obj = null;
try {
// 1.获取类
Class clazz = Class.forName(className);
// 2.获取构造方法
Constructor con = clazz.getConstructor();
// 3.创建一个空对象
obj = con.newInstance();
// 4.获取构造方法上的属性值(注解的对象)
Annotation a = con.getAnnotation(MyAnnotation.class);
// 5. 获取a注解对象的值,
// 5.1 获取注解类
Class aclazz = a.getClass();
// 5.2 获取注解的value方法
Method amethod = aclazz.getMethod("value");
// 5.3 调用value方法
String[] values = (String[])amethod.invoke(a);
// 6.将values中对应的属性值赋值给obj
// 6.1 找寻属性对应的set方法
Field[] fields = clazz.getDeclaredFields();
for(int i=0;i<fields.length;i++){
String fieldName = fields[i].getName();
String firstLetter = fieldName.substring(0,1).toUpperCase();
String otherLetter = fieldName.substring(1);
StringBuilder setMethodName = new StringBuilder("set");
setMethodName.append(firstLetter);
setMethodName.append(otherLetter);
// 6。2根据方法字符串找到set方法
Class fieldType = fields[i].getType();
Method method = clazz.getMethod(setMethodName.toString(),fieldType);
method.invoke(obj,fieldType.getConstructor(String.class).newInstance(values[i]));
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return obj;
}
}
Student的domain
package org.example.test;
public class Student {
private String name;
private Integer age;
private Float money;
@MyAnnotation({"孙燕婷","18","9999.23"})
public Student() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Float getMoney() {
return money;
}
public void setMoney(Float money) {
this.money = money;
}
}
测试代码块
拓展》从文件中读取属性值
public class MySpring {
public Object getBean(String className){
Object obj = null;
try {
// 1.获取类
Class clazz = Class.forName(className);
// 2.获取构造方法
Constructor con = clazz.getConstructor();
// 3.创建一个空对象
obj = con.newInstance();
// 4。从文件中读取属性值
// 4。1 由于Properties是无序的,读出的内容和文件内容顺序不一致,写了个辅助类LinkedProperities继承Properties
LinkedProperities pro = new LinkedProperities();
pro.load(new FileReader("src/main/resources/test.properties"));
LinkedList<String> en = pro.orderStringPropertyNames();
String[] values = new String[en.size()];
for(int i=0;i<en.size();i++){
String key = en.get(i);
String value = pro.getProperty(key);
values[i] = value;
}
// 6.将values中对应的属性值赋值给obj
// 6.1 找寻属性对应的set方法
Field[] fields = clazz.getDeclaredFields();
for(int i=0;i<fields.length;i++){
String fieldName = fields[i].getName();
String firstLetter = fieldName.substring(0,1).toUpperCase();
String otherLetter = fieldName.substring(1);
StringBuilder setMethodName = new StringBuilder("set");
setMethodName.append(firstLetter);
setMethodName.append(otherLetter);
// 6。2根据方法字符串找到set方法
Class fieldType = fields[i].getType();
Method method = clazz.getMethod(setMethodName.toString(),fieldType);
method.invoke(obj,fieldType.getConstructor(String.class).newInstance(values[i]));
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return obj;
}
}
核心代码就是
辅助类LinkedProperities
public class LinkedProperities extends Properties {
private LinkedList<String> linkedList = new LinkedList<String>();
@Override
public synchronized Object put(Object key, Object value) {
linkedList.add((String) key);
return super.put(key, value);
}
public LinkedList<String> orderStringPropertyNames() {
return linkedList;
}
}