Search in sources :

Example 1 with RpcReference

use of com.jim.framework.rpc.client.RpcReference in project jim-framework by jiangmin168168.

the class BeanPostPrcessorReference method initRpcReferenceBean.

public Object initRpcReferenceBean(Object bean, String beanName) {
    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 {
                RpcReference reference = method.getAnnotation(RpcReference.class);
                if (reference != null) {
                    Object value = this.rpcClient.createProxy(method.getParameterTypes()[0], reference);
                    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);
            }
            RpcReference reference = field.getAnnotation(RpcReference.class);
            if (reference != null) {
                Object value = this.rpcClient.createProxy(field.getType(), reference);
                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) Method(java.lang.reflect.Method) BeansException(org.springframework.beans.BeansException) BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) RpcReference(com.jim.framework.rpc.client.RpcReference)

Aggregations

RpcReference (com.jim.framework.rpc.client.RpcReference)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