package bean;
import org.springframework.beans.factory.annotation.Autowired;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SimpleSpringParse implements MyApplicationAware {
@Autowired
private Person person;
private Factory factory;
private static ListbeanNameList = new ArrayList();
private static ListannotationList = new ArrayList();
public static Object createInstance(String name) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
Class>aClass = Class.forName(name);
Field[] fields = aClass.getDeclaredFields();
for(Field f:fields){
if(f.getAnnotation(Autowired.class) != null){
beanNameList.add(f.getType().getName());
annotationList.add(f.getName());
System.out.println("has annotationsByType Autowired ");
}else{
System.out.println("not has annotationsByType Autowired ");
}
}
return aClass.newInstance();
}
public static void populate(Object instance) throws IllegalAccessException {
Field field = null;
try {
field = instance.getClass().getDeclaredField(annotationList.get(0));
} catch (NoSuchFieldException ex) {
// 子類不存在該變量那么嘗試去父類獲取變量
// field=object.getClass().getSuperclass().getDeclaredField(“name”);
}
field.setAccessible(true);
Person p = new Person();
p.setName(“xxxxx”);
field.set(instance, p);
}
public static void initializeBean(Object instance){
if(instance instanceof MyApplicationAware){
((MyApplicationAware) instance).setFactory(new DefaultFactory());
}
}
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
SimpleSpringParse instance = (SimpleSpringParse)createInstance("bean.SimpleSpringParse");
populate(instance);
initializeBean(instance);
System.out.println(instance.getPerson().getName());
System.out.println(beanNameList.get(0));
System.out.println(annotationList.get(0));
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
@Override
public void setFactory(Factory factory) {
this.factory = factory;
}
}
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧
分享文章:模擬spring加載過(guò)程-創(chuàng)新互聯(lián)
轉(zhuǎn)載注明:http://jinyejixie.com/article12/hiedc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)、App設(shè)計(jì)、標(biāo)簽優(yōu)化、移動(dòng)網(wǎng)站建設(shè)、軟件開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容