Search in sources :

Example 1 with SpringValue

use of com.ctrip.framework.apollo.spring.property.SpringValue in project apollo by ctripcorp.

the class ApolloJsonValueProcessor method processField.

@Override
protected void processField(Object bean, String beanName, Field field) {
    ApolloJsonValue apolloJsonValue = AnnotationUtils.getAnnotation(field, ApolloJsonValue.class);
    if (apolloJsonValue == null) {
        return;
    }
    String placeholder = apolloJsonValue.value();
    Object propertyValue = placeholderHelper.resolvePropertyValue(beanFactory, beanName, placeholder);
    // propertyValue will never be null, as @ApolloJsonValue will not allow that
    if (!(propertyValue instanceof String)) {
        return;
    }
    boolean accessible = field.isAccessible();
    field.setAccessible(true);
    ReflectionUtils.setField(field, bean, parseJsonValue((String) propertyValue, field.getGenericType()));
    field.setAccessible(accessible);
    if (configUtil.isAutoUpdateInjectedSpringPropertiesEnabled()) {
        Set<String> keys = placeholderHelper.extractPlaceholderKeys(placeholder);
        for (String key : keys) {
            SpringValue springValue = new SpringValue(key, placeholder, bean, beanName, field, true);
            springValueRegistry.register(key, springValue);
            logger.debug("Monitoring {}", springValue);
        }
    }
}
Also used : SpringValue(com.ctrip.framework.apollo.spring.property.SpringValue)

Example 2 with SpringValue

use of com.ctrip.framework.apollo.spring.property.SpringValue in project apollo by ctripcorp.

the class ApolloJsonValueProcessor method processMethod.

@Override
protected void processMethod(Object bean, String beanName, Method method) {
    ApolloJsonValue apolloJsonValue = AnnotationUtils.getAnnotation(method, ApolloJsonValue.class);
    if (apolloJsonValue == null) {
        return;
    }
    String placeHolder = apolloJsonValue.value();
    Object propertyValue = placeholderHelper.resolvePropertyValue(beanFactory, beanName, placeHolder);
    // propertyValue will never be null, as @ApolloJsonValue will not allow that
    if (!(propertyValue instanceof String)) {
        return;
    }
    Type[] types = method.getGenericParameterTypes();
    Preconditions.checkArgument(types.length == 1, "Ignore @Value setter {}.{}, expecting 1 parameter, actual {} parameters", bean.getClass().getName(), method.getName(), method.getParameterTypes().length);
    boolean accessible = method.isAccessible();
    method.setAccessible(true);
    ReflectionUtils.invokeMethod(method, bean, parseJsonValue((String) propertyValue, types[0]));
    method.setAccessible(accessible);
    if (configUtil.isAutoUpdateInjectedSpringPropertiesEnabled()) {
        Set<String> keys = placeholderHelper.extractPlaceholderKeys(placeHolder);
        for (String key : keys) {
            SpringValue springValue = new SpringValue(key, apolloJsonValue.value(), bean, beanName, method, true);
            springValueRegistry.register(key, springValue);
            logger.debug("Monitoring {}", springValue);
        }
    }
}
Also used : Type(java.lang.reflect.Type) SpringValue(com.ctrip.framework.apollo.spring.property.SpringValue)

Example 3 with SpringValue

use of com.ctrip.framework.apollo.spring.property.SpringValue in project apollo by ctripcorp.

the class SpringValueProcessor method processMethod.

@Override
protected void processMethod(Object bean, String beanName, Method method) {
    // register @Value on method
    Value value = method.getAnnotation(Value.class);
    if (value == null) {
        return;
    }
    // skip Configuration bean methods
    if (method.getAnnotation(Bean.class) != null) {
        return;
    }
    if (method.getParameterTypes().length != 1) {
        logger.error("Ignore @Value setter {}.{}, expecting 1 parameter, actual {} parameters", bean.getClass().getName(), method.getName(), method.getParameterTypes().length);
        return;
    }
    Set<String> keys = placeholderHelper.extractPlaceholderKeys(value.value());
    if (keys.isEmpty()) {
        return;
    }
    for (String key : keys) {
        SpringValue springValue = new SpringValue(key, value.value(), bean, beanName, method, false);
        springValueRegistry.register(key, springValue);
        logger.debug("Monitoring {}", springValue);
    }
}
Also used : SpringValue(com.ctrip.framework.apollo.spring.property.SpringValue) Value(org.springframework.beans.factory.annotation.Value) SpringValue(com.ctrip.framework.apollo.spring.property.SpringValue) Bean(org.springframework.context.annotation.Bean)

Example 4 with SpringValue

use of com.ctrip.framework.apollo.spring.property.SpringValue in project apollo by ctripcorp.

the class SpringValueProcessor method processField.

@Override
protected void processField(Object bean, String beanName, Field field) {
    // register @Value on field
    Value value = field.getAnnotation(Value.class);
    if (value == null) {
        return;
    }
    Set<String> keys = placeholderHelper.extractPlaceholderKeys(value.value());
    if (keys.isEmpty()) {
        return;
    }
    for (String key : keys) {
        SpringValue springValue = new SpringValue(key, value.value(), bean, beanName, field, false);
        springValueRegistry.register(key, springValue);
        logger.debug("Monitoring {}", springValue);
    }
}
Also used : SpringValue(com.ctrip.framework.apollo.spring.property.SpringValue) Value(org.springframework.beans.factory.annotation.Value) SpringValue(com.ctrip.framework.apollo.spring.property.SpringValue)

Example 5 with SpringValue

use of com.ctrip.framework.apollo.spring.property.SpringValue in project apollo by ctripcorp.

the class SpringValueProcessor method processBeanPropertyValues.

private void processBeanPropertyValues(Object bean, String beanName) {
    Collection<SpringValueDefinition> propertySpringValues = beanName2SpringValueDefinitions.get(beanName);
    if (propertySpringValues == null || propertySpringValues.isEmpty()) {
        return;
    }
    for (SpringValueDefinition definition : propertySpringValues) {
        try {
            PropertyDescriptor pd = BeanUtils.getPropertyDescriptor(bean.getClass(), definition.getPropertyName());
            Method method = pd.getWriteMethod();
            if (method == null) {
                continue;
            }
            SpringValue springValue = new SpringValue(definition.getKey(), definition.getPlaceholder(), bean, beanName, method, false);
            springValueRegistry.register(definition.getKey(), springValue);
            logger.debug("Monitoring {}", springValue);
        } catch (Throwable ex) {
            logger.error("Failed to enable auto update feature for {}.{}", bean.getClass(), definition.getPropertyName());
        }
    }
    // clear
    beanName2SpringValueDefinitions.removeAll(beanName);
}
Also used : SpringValue(com.ctrip.framework.apollo.spring.property.SpringValue) SpringValueDefinition(com.ctrip.framework.apollo.spring.property.SpringValueDefinition) PropertyDescriptor(java.beans.PropertyDescriptor) Method(java.lang.reflect.Method)

Aggregations

SpringValue (com.ctrip.framework.apollo.spring.property.SpringValue)5 Value (org.springframework.beans.factory.annotation.Value)2 SpringValueDefinition (com.ctrip.framework.apollo.spring.property.SpringValueDefinition)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 Method (java.lang.reflect.Method)1 Type (java.lang.reflect.Type)1 Bean (org.springframework.context.annotation.Bean)1