Example usage for org.springframework.context.expression BeanFactoryResolver BeanFactoryResolver

List of usage examples for org.springframework.context.expression BeanFactoryResolver BeanFactoryResolver

Introduction

In this page you can find the example usage for org.springframework.context.expression BeanFactoryResolver BeanFactoryResolver.

Prototype

public BeanFactoryResolver(BeanFactory beanFactory) 

Source Link

Document

Create a new BeanFactoryResolver for the given factory.

Usage

From source file:org.springframework.data.document.mongodb.convert.MappingMongoConverter.java

private <S extends Object> S read(final MongoPersistentEntity<S> entity, final DBObject dbo) {

    final StandardEvaluationContext spelCtx = new StandardEvaluationContext();
    if (null != applicationContext) {
        spelCtx.setBeanResolver(new BeanFactoryResolver(applicationContext));
    }/*from w w w  .j a v a2  s .  c  o m*/
    if (!(dbo instanceof BasicDBList)) {
        String[] keySet = dbo.keySet().toArray(new String[] {});
        for (String key : keySet) {
            spelCtx.setVariable(key, dbo.get(key));
        }
    }

    final List<String> ctorParamNames = new ArrayList<String>();
    final MongoPersistentProperty idProperty = entity.getIdProperty();
    final S instance = constructInstance(entity, new PreferredConstructor.ParameterValueProvider() {
        @SuppressWarnings("unchecked")
        public <T> T getParameterValue(PreferredConstructor.Parameter<T> parameter) {
            String name = parameter.getName();
            TypeInformation<T> type = parameter.getType();
            Class<T> rawType = parameter.getRawType();
            String key = idProperty == null ? name
                    : idProperty.getName().equals(name) ? idProperty.getKey() : name;
            Object obj = dbo.get(key);

            ctorParamNames.add(name);
            if (obj instanceof DBRef) {
                return read(type, ((DBRef) obj).fetch());
            } else if (obj instanceof BasicDBList) {
                BasicDBList objAsDbList = (BasicDBList) obj;
                List<?> l = unwrapList(objAsDbList, type);
                return conversionService.convert(l, rawType);
            } else if (obj instanceof DBObject) {
                return read(type, ((DBObject) obj));
            } else if (null != obj && obj.getClass().isAssignableFrom(rawType)) {
                return (T) obj;
            } else if (null != obj) {
                return conversionService.convert(obj, rawType);
            }

            return null;
        }
    }, spelCtx);

    // Set properties not already set in the constructor
    entity.doWithProperties(new PropertyHandler<MongoPersistentProperty>() {
        public void doWithPersistentProperty(MongoPersistentProperty prop) {

            boolean isConstructorProperty = ctorParamNames.contains(prop.getName());
            boolean hasValueForProperty = dbo.containsField(prop.getKey());

            if (!hasValueForProperty || isConstructorProperty) {
                return;
            }

            Object obj = getValueInternal(prop, dbo, spelCtx, prop.getSpelExpression());
            try {
                setProperty(instance, prop, obj, useFieldAccessOnly);
            } catch (IllegalAccessException e) {
                throw new MappingException(e.getMessage(), e);
            } catch (InvocationTargetException e) {
                throw new MappingException(e.getMessage(), e);
            }
        }
    });

    // Handle associations
    entity.doWithAssociations(new AssociationHandler<MongoPersistentProperty>() {
        public void doWithAssociation(Association<MongoPersistentProperty> association) {
            MongoPersistentProperty inverseProp = association.getInverse();
            Object obj = getValueInternal(inverseProp, dbo, spelCtx, inverseProp.getSpelExpression());
            try {
                setProperty(instance, inverseProp, obj);
            } catch (IllegalAccessException e) {
                throw new MappingException(e.getMessage(), e);
            } catch (InvocationTargetException e) {
                throw new MappingException(e.getMessage(), e);
            }
        }
    });

    return instance;
}

From source file:org.springframework.data.mongodb.core.convert.MappingMongoConverter.java

private <S extends Object> S read(final MongoPersistentEntity<S> entity, final DBObject dbo) {

    final StandardEvaluationContext spelCtx = new StandardEvaluationContext(dbo);
    spelCtx.addPropertyAccessor(DBObjectPropertyAccessor.INSTANCE);

    if (applicationContext != null) {
        spelCtx.setBeanResolver(new BeanFactoryResolver(applicationContext));
    }/*from   w w  w.  ja  v  a  2 s  . co m*/

    final MappedConstructor constructor = new MappedConstructor(entity, mappingContext);

    SpELAwareParameterValueProvider delegate = new SpELAwareParameterValueProvider(spelExpressionParser,
            spelCtx);
    ParameterValueProvider provider = new DelegatingParameterValueProvider(constructor, dbo, delegate);

    final BeanWrapper<MongoPersistentEntity<S>, S> wrapper = BeanWrapper.create(entity, provider,
            conversionService);

    // Set properties not already set in the constructor
    entity.doWithProperties(new PropertyHandler<MongoPersistentProperty>() {
        public void doWithPersistentProperty(MongoPersistentProperty prop) {

            boolean isConstructorProperty = constructor.isConstructorParameter(prop);
            boolean hasValueForProperty = dbo.containsField(prop.getFieldName());

            if (!hasValueForProperty || isConstructorProperty) {
                return;
            }

            Object obj = getValueInternal(prop, dbo, spelCtx, prop.getSpelExpression());
            wrapper.setProperty(prop, obj, useFieldAccessOnly);
        }
    });

    // Handle associations
    entity.doWithAssociations(new AssociationHandler<MongoPersistentProperty>() {
        public void doWithAssociation(Association<MongoPersistentProperty> association) {
            MongoPersistentProperty inverseProp = association.getInverse();
            Object obj = getValueInternal(inverseProp, dbo, spelCtx, inverseProp.getSpelExpression());
            try {
                wrapper.setProperty(inverseProp, obj);
            } catch (IllegalAccessException e) {
                throw new MappingException(e.getMessage(), e);
            } catch (InvocationTargetException e) {
                throw new MappingException(e.getMessage(), e);
            }
        }
    });

    return wrapper.getBean();
}

From source file:org.springframework.integration.expression.ExpressionUtils.java

/**
 * Create a {@link StandardEvaluationContext} with a {@link MapAccessor} in its
 * property accessor property and the supplied {@link ConversionService} in its
 * conversionService property./*from   ww w .ja v  a 2s  .  c om*/
 * @param conversionService the conversion service.
 * @return the evaluation context.
 */
private static StandardEvaluationContext createStandardEvaluationContext(ConversionService conversionService,
        BeanFactory beanFactory) {
    StandardEvaluationContext evaluationContext = new StandardEvaluationContext();
    evaluationContext.addPropertyAccessor(new MapAccessor());
    if (conversionService != null) {
        evaluationContext.setTypeConverter(new StandardTypeConverter(conversionService));
    }
    if (beanFactory != null) {
        evaluationContext.setBeanResolver(new BeanFactoryResolver(beanFactory));
    }
    return evaluationContext;
}

From source file:org.springframework.integration.util.AbstractExpressionEvaluator.java

/**
 * Specify a BeanFactory in order to enable resolution via <code>@beanName</code> in the expression.
 *//*from  ww  w .j  a v  a2s .c o m*/
public void setBeanFactory(final BeanFactory beanFactory) {
    if (beanFactory != null) {
        this.beanFactory = beanFactory;
        this.typeConverter.setBeanFactory(beanFactory);
        if (this.evaluationContext != null && this.evaluationContext.getBeanResolver() == null) {
            this.evaluationContext.setBeanResolver(new BeanFactoryResolver(beanFactory));
        }
    }
}

From source file:org.springframework.kafka.config.AbstractKafkaListenerEndpoint.java

@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
    this.beanFactory = beanFactory;
    if (beanFactory instanceof ConfigurableListableBeanFactory) {
        this.resolver = ((ConfigurableListableBeanFactory) beanFactory).getBeanExpressionResolver();
        this.expressionContext = new BeanExpressionContext((ConfigurableListableBeanFactory) beanFactory, null);
    }//  w  ww. java2s . c  o m
    this.beanResolver = new BeanFactoryResolver(beanFactory);
}