模拟spring加载过程-创新互联

package bean;

创新互联自2013年起,先为景谷等服务建站,景谷等地企业,进行企业商务咨询服务。为景谷企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

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 {
    ClassaClass = 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;
}

}

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


本文名称:模拟spring加载过程-创新互联
文章分享:http://myzitong.com/article/hiedc.html