Search in sources :

Example 1 with FieldInjector

use of com.blade.ioc.bean.FieldInjector in project blade by biezhi.

the class IocKit method initInjection.

public static void initInjection(Ioc ioc, BeanDefine beanDefine) {
    ClassDefine classDefine = ClassDefine.create(beanDefine.getType());
    List<FieldInjector> fieldInjectors = getInjectFields(ioc, classDefine);
    Object bean = beanDefine.getBean();
    AtomicBoolean hasPrototypeField = new AtomicBoolean(false);
    fieldInjectors.forEach(fieldInjector -> {
        if (fieldInjector.isSingleton()) {
            fieldInjector.injection(bean);
        } else {
            hasPrototypeField.set(true);
        }
    });
    beanDefine.setFieldHasPrototype(hasPrototypeField.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClassDefine(com.blade.ioc.bean.ClassDefine) FieldInjector(com.blade.ioc.bean.FieldInjector)

Example 2 with FieldInjector

use of com.blade.ioc.bean.FieldInjector in project blade by biezhi.

the class FieldInjectorTest method testFieldInjector.

@Test
public void testFieldInjector() {
    Ioc ioc = new SimpleIoc();
    ioc.addBean("jack");
    ioc.addBean(new BladeBeanDefineType());
    Field field = BladeBeanDefineType.class.getDeclaredFields()[0];
    FieldInjector fieldInjector = new FieldInjector(ioc, field);
    fieldInjector.injection(ioc.getBean(BladeBeanDefineType.class));
}
Also used : Field(java.lang.reflect.Field) BladeBeanDefineType(com.blade.types.BladeBeanDefineType) FieldInjector(com.blade.ioc.bean.FieldInjector) Test(org.junit.Test)

Example 3 with FieldInjector

use of com.blade.ioc.bean.FieldInjector in project blade by biezhi.

the class IocKit method injection.

public static void injection(Ioc ioc, BeanDefine beanDefine) {
    ClassDefine classDefine = ClassDefine.create(beanDefine.getType());
    List<FieldInjector> fieldInjectors = getInjectFields(ioc, classDefine);
    Object bean = beanDefine.getBean();
    fieldInjectors.forEach(fieldInjector -> {
        Object fieldInstance = ReflectKit.newInstance(fieldInjector.getType());
        if (fieldInjector.hasInjectFields()) {
            injection(ioc, new BeanDefine(fieldInstance));
        }
        fieldInjector.injection(bean, fieldInstance);
    });
}
Also used : ClassDefine(com.blade.ioc.bean.ClassDefine) FieldInjector(com.blade.ioc.bean.FieldInjector) BeanDefine(com.blade.ioc.bean.BeanDefine)

Example 4 with FieldInjector

use of com.blade.ioc.bean.FieldInjector in project blade by biezhi.

the class FieldInjectorTest method testFieldInjectorError.

@Test(expected = RuntimeException.class)
public void testFieldInjectorError() {
    Ioc ioc = new SimpleIoc();
    FieldInjector fieldInjector = new FieldInjector(ioc, null);
    fieldInjector.injection(new BladeBeanDefineType());
}
Also used : BladeBeanDefineType(com.blade.types.BladeBeanDefineType) FieldInjector(com.blade.ioc.bean.FieldInjector) Test(org.junit.Test)

Aggregations

FieldInjector (com.blade.ioc.bean.FieldInjector)4 ClassDefine (com.blade.ioc.bean.ClassDefine)2 BladeBeanDefineType (com.blade.types.BladeBeanDefineType)2 Test (org.junit.Test)2 BeanDefine (com.blade.ioc.bean.BeanDefine)1 Field (java.lang.reflect.Field)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1