Search in sources :

Example 1 with Autowired

use of io.nuls.kernel.lite.annotation.Autowired in project nuls by nuls-io.

the class SpringLiteContext method injectionBeanField.

// /**
// * 检查某个对象的某个属性,如果对象被标记了Autowired注解,则去相应的依赖,并将依赖赋值给对象的该属性
// * Check an attribute of an object, and if the object is marked with Autowired annotations,
// * it is dependent and will depend on the attribute that is assigned to the object.
// *
// * @param obj   bean对象
// * @param field 对象的一个属性
// */
private static boolean injectionBeanField(Object obj, Field field) throws Exception {
    Annotation[] anns = field.getDeclaredAnnotations();
    if (anns == null || anns.length == 0) {
        return true;
    }
    Annotation automired = getFromArray(anns, Autowired.class);
    if (null == automired) {
        return true;
    }
    String name = ((Autowired) automired).value();
    Object value = null;
    if (null == name || name.trim().length() == 0) {
        Set<String> nameSet = CLASS_NAME_SET_MAP.get(field.getType());
        if (nameSet == null || nameSet.isEmpty()) {
            throw new Exception("Can't find the bean,field:" + field.getName());
        } else if (nameSet.size() == 1) {
            name = nameSet.iterator().next();
        } else {
            name = field.getName();
        }
    }
    value = getBean(name);
    if (null == value) {
        throw new Exception("Can't find the bean named:" + name);
    }
    field.setAccessible(true);
    field.set(obj, value);
    field.setAccessible(false);
    return true;
}
Also used : Autowired(io.nuls.kernel.lite.annotation.Autowired) Annotation(java.lang.annotation.Annotation) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) NulsException(io.nuls.kernel.exception.NulsException)

Aggregations

NulsException (io.nuls.kernel.exception.NulsException)1 NulsRuntimeException (io.nuls.kernel.exception.NulsRuntimeException)1 Autowired (io.nuls.kernel.lite.annotation.Autowired)1 Annotation (java.lang.annotation.Annotation)1