Search in sources :

Example 1 with MotanReferer

use of com.weibo.api.motan.config.springsupport.annotation.MotanReferer in project motan by weibocom.

the class AnnotationBean method postProcessBeforeInitialization.

/**
     * init reference field
     *
     * @param bean
     * @param beanName
     * @return
     * @throws BeansException
     */
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    if (!isMatchPackage(bean)) {
        return bean;
    }
    Class<?> clazz = bean.getClass();
    if (isProxyBean(bean)) {
        clazz = AopUtils.getTargetClass(bean);
    }
    Method[] methods = clazz.getMethods();
    for (Method method : methods) {
        String name = method.getName();
        if (name.length() > 3 && name.startsWith("set") && method.getParameterTypes().length == 1 && Modifier.isPublic(method.getModifiers()) && !Modifier.isStatic(method.getModifiers())) {
            try {
                MotanReferer reference = method.getAnnotation(MotanReferer.class);
                if (reference != null) {
                    Object value = refer(reference, method.getParameterTypes()[0]);
                    if (value != null) {
                        method.invoke(bean, new Object[] { value });
                    }
                }
            } catch (Exception e) {
                throw new BeanInitializationException("Failed to init remote service reference at method " + name + " in class " + bean.getClass().getName(), e);
            }
        }
    }
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {
        try {
            if (!field.isAccessible()) {
                field.setAccessible(true);
            }
            MotanReferer reference = field.getAnnotation(MotanReferer.class);
            if (reference != null) {
                Object value = refer(reference, field.getType());
                if (value != null) {
                    field.set(bean, value);
                }
            }
        } catch (Exception e) {
            throw new BeanInitializationException("Failed to init remote service reference at filed " + field.getName() + " in class " + bean.getClass().getName(), e);
        }
    }
    return bean;
}
Also used : BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) Field(java.lang.reflect.Field) MotanReferer(com.weibo.api.motan.config.springsupport.annotation.MotanReferer) Method(java.lang.reflect.Method) BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) BeansException(org.springframework.beans.BeansException)

Aggregations

MotanReferer (com.weibo.api.motan.config.springsupport.annotation.MotanReferer)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 BeansException (org.springframework.beans.BeansException)1 BeanInitializationException (org.springframework.beans.factory.BeanInitializationException)1