Java 类org.springframework.beans.factory.config.BeanExpressionContext 实例源码

项目:spring4-understanding    文件:DefaultListableBeanFactoryTests.java   
@Test
public void testExpressionInStringArray() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    BeanExpressionResolver beanExpressionResolver = mock(BeanExpressionResolver.class);
    when(beanExpressionResolver.evaluate(eq("#{foo}"), Matchers.any(BeanExpressionContext.class)))
            .thenReturn("classpath:/org/springframework/beans/factory/xml/util.properties");
    bf.setBeanExpressionResolver(beanExpressionResolver);

    RootBeanDefinition rbd = new RootBeanDefinition(PropertiesFactoryBean.class);
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("locations", new String[]{"#{foo}"});
    rbd.setPropertyValues(pvs);
    bf.registerBeanDefinition("myProperties", rbd);
    Properties properties = (Properties) bf.getBean("myProperties");
    assertEquals("bar", properties.getProperty("foo"));
}
项目:spring-jax-rs-client-proxy    文件:JaxRsClientProxyFactoryBean.java   
/**
 * Retrieves the service url.
 *
 * @return the service url
 */
private String getServiceUrl() {

    try {
        if (!serviceUrl.isEmpty()) {

            ConfigurableBeanFactory beanFactory = getBeanFactory();
            if(beanFactory != null) {
                return (String) beanFactory.getBeanExpressionResolver()
                        .evaluate(serviceUrl, new BeanExpressionContext(beanFactory, null));
            }
            return serviceUrl;
        }

        return applicationContext.getBean(serviceUrlProvider)
                .getServiceUrl();
    } catch (BeansException e) {
        throw new IllegalStateException("The service url hasn't been specified and " +
                "no ServiceUrlProvider has been registered in application context.", e);
    }
}
项目:spring-autoproperties    文件:AutoPropertiesFactoryBean.java   
private Object resolveValue(Method method) {
    Value val = AnnotationUtils.findAnnotation(method, Value.class);
    if (val != null) {
        if (LOG.isDebugEnabled()) {
            LOG.debug(String.format("Resolving @Value annotation on %s with key %s",
                    method.getName(), val.value()));
        }
        ConfigurableBeanFactory beanFactory = (ConfigurableBeanFactory) getBeanFactory();
        String strValue = beanFactory.resolveEmbeddedValue(val.value());
        BeanExpressionResolver resolver = beanFactory.getBeanExpressionResolver();

        Object unconvertedResult = resolver.evaluate(strValue, new BeanExpressionContext(beanFactory, null));
        TypeConverter converter = beanFactory.getTypeConverter();
        return converter.convertIfNecessary(unconvertedResult, method.getReturnType());
    }
    String message = "Method %s on interface %s must be annotated with @Value for auto-properties to work!";
    throw new AutoPropertiesException(String.format(message, method.getName(), method.getDeclaringClass().getName()));
}
项目:spring-cloud-stream    文件:ApplicationMetricsProperties.java   
private Map<String, Object> buildExportProperties() {
    Map<String, Object> props = new HashMap<>();
    if (!ObjectUtils.isEmpty(this.properties)) {
        Map<String, String> target = bindProperties();

        BeanExpressionResolver beanExpressionResolver = ((ConfigurableApplicationContext) applicationContext)
                .getBeanFactory().getBeanExpressionResolver();
        BeanExpressionContext expressionContext = new BeanExpressionContext(
                ((ConfigurableApplicationContext) applicationContext).getBeanFactory(), null);
        for (Entry<String, String> entry : target.entrySet()) {
            if (isMatch(entry.getKey(), this.properties, null)) {
                String stringValue = ObjectUtils.nullSafeToString(entry.getValue());
                Object exportedValue = null;
                if (stringValue != null) {
                    exportedValue = stringValue.startsWith("#{")
                            ? beanExpressionResolver.evaluate(
                                    environment.resolvePlaceholders(stringValue), expressionContext)
                            : environment.resolvePlaceholders(stringValue);
                }

                props.put(entry.getKey(), exportedValue);
            }
        }
    }
    return props;
}
项目:spring-cloud-aws    文件:QueueMessageHandler.java   
private String[] resolveName(String name) {
    if (!(getApplicationContext() instanceof ConfigurableApplicationContext)) {
        return wrapInStringArray(name);
    }

    ConfigurableApplicationContext applicationContext = (ConfigurableApplicationContext) getApplicationContext();
    ConfigurableBeanFactory configurableBeanFactory = applicationContext.getBeanFactory();

    String placeholdersResolved = configurableBeanFactory.resolveEmbeddedValue(name);
    BeanExpressionResolver exprResolver = configurableBeanFactory.getBeanExpressionResolver();
    if (exprResolver == null) {
        return wrapInStringArray(name);
    }
    Object result = exprResolver.evaluate(placeholdersResolved, new BeanExpressionContext(configurableBeanFactory, null));
    if (result instanceof String[]) {
        return (String[]) result;
    } else if (result != null) {
        return wrapInStringArray(result);
    } else {
        return wrapInStringArray(name);
    }
}
项目:spring-zeebe    文件:ZeebeExpressionResolver.java   
@Override
public void setBeanFactory(final BeanFactory beanFactory) throws BeansException
{
    this.beanFactory = beanFactory;
    if (beanFactory instanceof ConfigurableListableBeanFactory)
    {
        this.resolver = ((ConfigurableListableBeanFactory) beanFactory).getBeanExpressionResolver();
        this.expressionContext = new BeanExpressionContext((ConfigurableListableBeanFactory) beanFactory, null);
    }
}
项目:spring4-understanding    文件:AnnotationMethodHandlerAdapter.java   
@Override
public void setBeanFactory(BeanFactory beanFactory) {
    if (beanFactory instanceof ConfigurableBeanFactory) {
        this.beanFactory = (ConfigurableBeanFactory) beanFactory;
        this.expressionContext = new BeanExpressionContext(this.beanFactory, new RequestScope());
    }
}
项目:spring4-understanding    文件:AnnotationMethodHandlerAdapter.java   
@Override
public void setBeanFactory(BeanFactory beanFactory) {
    if (beanFactory instanceof ConfigurableBeanFactory) {
        this.beanFactory = (ConfigurableBeanFactory) beanFactory;
        this.expressionContext = new BeanExpressionContext(this.beanFactory, new RequestScope());
    }
}
项目:kc-rice    文件:DataDictionary.java   
/**
 * Returns a property value for the bean with the given name from the dictionary.
 *
 * @param beanName id or name for the bean definition
 * @param propertyName name of the property to retrieve, must be a valid property configured on
 * the bean definition
 * @return Object property value for property
 */
public Object getDictionaryBeanProperty(String beanName, String propertyName) {
    Object bean = ddBeans.getSingleton(beanName);
    if (bean != null) {
        return ObjectPropertyUtils.getPropertyValue(bean, propertyName);
    }

    BeanDefinition beanDefinition = ddBeans.getMergedBeanDefinition(beanName);

    if (beanDefinition == null) {
        throw new RuntimeException("Unable to get bean for bean name: " + beanName);
    }

    PropertyValues pvs = beanDefinition.getPropertyValues();
    if (pvs.contains(propertyName)) {
        PropertyValue propertyValue = pvs.getPropertyValue(propertyName);

        Object value;
        if (propertyValue.isConverted()) {
            value = propertyValue.getConvertedValue();
        } else if (propertyValue.getValue() instanceof String) {
            String unconvertedValue = (String) propertyValue.getValue();
            Scope scope = ddBeans.getRegisteredScope(beanDefinition.getScope());
            BeanExpressionContext beanExpressionContext = new BeanExpressionContext(ddBeans, scope);

            value = ddBeans.getBeanExpressionResolver().evaluate(unconvertedValue, beanExpressionContext);
        } else {
            value = propertyValue.getValue();
        }

        return value;
    }

    return null;
}
项目:class-guard    文件:StandardBeanExpressionResolver.java   
public Object evaluate(String value, BeanExpressionContext evalContext) throws BeansException {
    if (!StringUtils.hasLength(value)) {
        return value;
    }
    try {
        Expression expr = this.expressionCache.get(value);
        if (expr == null) {
            expr = this.expressionParser.parseExpression(value, this.beanExpressionParserContext);
            this.expressionCache.put(value, expr);
        }
        StandardEvaluationContext sec = this.evaluationCache.get(evalContext);
        if (sec == null) {
            sec = new StandardEvaluationContext();
            sec.setRootObject(evalContext);
            sec.addPropertyAccessor(new BeanExpressionContextAccessor());
            sec.addPropertyAccessor(new BeanFactoryAccessor());
            sec.addPropertyAccessor(new MapAccessor());
            sec.addPropertyAccessor(new EnvironmentAccessor());
            sec.setBeanResolver(new BeanFactoryResolver(evalContext.getBeanFactory()));
            sec.setTypeLocator(new StandardTypeLocator(evalContext.getBeanFactory().getBeanClassLoader()));
            ConversionService conversionService = evalContext.getBeanFactory().getConversionService();
            if (conversionService != null) {
                sec.setTypeConverter(new StandardTypeConverter(conversionService));
            }
            customizeEvaluationContext(sec);
            this.evaluationCache.put(evalContext, sec);
        }
        return expr.getValue(sec);
    }
    catch (Exception ex) {
        throw new BeanExpressionException("Expression parsing failed", ex);
    }
}
项目:sniffy    文件:SniffySpringConfiguration.java   
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
    this.beanFactory = (ConfigurableBeanFactory) beanFactory;
    resolver = this.beanFactory.getBeanExpressionResolver();
    expressionContext = new BeanExpressionContext(this.beanFactory, null);
    typeConverter = this.beanFactory.getTypeConverter();
}
项目:jresplus    文件:ExtendableAnnotationMethodHandlerAdapter.java   
public void setBeanFactory(BeanFactory beanFactory) {
    if (beanFactory instanceof ConfigurableBeanFactory) {
        this.beanFactory = (ConfigurableBeanFactory) beanFactory;
        this.expressionContext = new BeanExpressionContext(
                this.beanFactory, new RequestScope());
    }
}
项目:spring-cloud-aws    文件:AmazonEc2InstanceUserTagsFactoryBeanAwsTest.java   
@Test
public void testGetUserProperties() throws Exception {

    Assert.assertEquals("tagv1", this.context.getBeanFactory().getBeanExpressionResolver().
            evaluate("#{instanceData['tag1']}", new BeanExpressionContext(this.context.getBeanFactory(), null)));
    Assert.assertEquals("tagv2", this.context.getBeanFactory().getBeanExpressionResolver().
            evaluate("#{instanceData['tag2']}", new BeanExpressionContext(this.context.getBeanFactory(), null)));
    Assert.assertEquals("tagv3", this.context.getBeanFactory().getBeanExpressionResolver().
            evaluate("#{instanceData['tag3']}", new BeanExpressionContext(this.context.getBeanFactory(), null)));
    Assert.assertEquals("tagv4", this.context.getBeanFactory().getBeanExpressionResolver().
            evaluate("#{instanceData['tag4']}", new BeanExpressionContext(this.context.getBeanFactory(), null)));
}
项目:spring-cloud-aws    文件:SendToHandlerMethodReturnValueHandler.java   
private String resolveName(String name) {
    if (!(this.beanFactory instanceof ConfigurableBeanFactory)) {
        return name;
    }

    ConfigurableBeanFactory configurableBeanFactory = (ConfigurableBeanFactory) this.beanFactory;

    String placeholdersResolved = configurableBeanFactory.resolveEmbeddedValue(name);
    BeanExpressionResolver exprResolver = configurableBeanFactory.getBeanExpressionResolver();
    if (exprResolver == null) {
        return name;
    }
    Object result = exprResolver.evaluate(placeholdersResolved, new BeanExpressionContext(configurableBeanFactory, null));
    return result != null ? result.toString() : name;
}
项目:rice    文件:DataDictionary.java   
/**
 * Returns a property value for the bean with the given name from the dictionary.
 *
 * @param beanName id or name for the bean definition
 * @param propertyName name of the property to retrieve, must be a valid property configured on
 * the bean definition
 * @return Object property value for property
 */
public Object getDictionaryBeanProperty(String beanName, String propertyName) {
    Object bean = ddBeans.getSingleton(beanName);
    if (bean != null) {
        return ObjectPropertyUtils.getPropertyValue(bean, propertyName);
    }

    BeanDefinition beanDefinition = ddBeans.getMergedBeanDefinition(beanName);

    if (beanDefinition == null) {
        throw new RuntimeException("Unable to get bean for bean name: " + beanName);
    }

    PropertyValues pvs = beanDefinition.getPropertyValues();
    if (pvs.contains(propertyName)) {
        PropertyValue propertyValue = pvs.getPropertyValue(propertyName);

        Object value;
        if (propertyValue.isConverted()) {
            value = propertyValue.getConvertedValue();
        } else if (propertyValue.getValue() instanceof String) {
            String unconvertedValue = (String) propertyValue.getValue();
            Scope scope = ddBeans.getRegisteredScope(beanDefinition.getScope());
            BeanExpressionContext beanExpressionContext = new BeanExpressionContext(ddBeans, scope);

            value = ddBeans.getBeanExpressionResolver().evaluate(unconvertedValue, beanExpressionContext);
        } else {
            value = propertyValue.getValue();
        }

        return value;
    }

    return null;
}
项目:lams    文件:BeanExpressionContextAccessor.java   
@Override
public boolean canRead(EvaluationContext context, Object target, String name) throws AccessException {
    return ((BeanExpressionContext) target).containsObject(name);
}
项目:lams    文件:BeanExpressionContextAccessor.java   
@Override
public TypedValue read(EvaluationContext context, Object target, String name) throws AccessException {
    return new TypedValue(((BeanExpressionContext) target).getObject(name));
}
项目:lams    文件:BeanExpressionContextAccessor.java   
@Override
public Class<?>[] getSpecificTargetClasses() {
    return new Class<?>[] {BeanExpressionContext.class};
}
项目:FastBootWeixin    文件:WxContextUtils.java   
/**
 * 本来想用Prepared的,但是发现prepared没有地方发布这个事件,可恶
 *
 * @param applicationReadyEvent
 */
@Override
public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
    configurableBeanFactory = applicationReadyEvent.getApplicationContext().getBeanFactory();
    expressionContext = new BeanExpressionContext(configurableBeanFactory, null);
}
项目:spring4-understanding    文件:BeanExpressionContextAccessor.java   
@Override
public boolean canRead(EvaluationContext context, Object target, String name) throws AccessException {
    return ((BeanExpressionContext) target).containsObject(name);
}
项目:spring4-understanding    文件:BeanExpressionContextAccessor.java   
@Override
public TypedValue read(EvaluationContext context, Object target, String name) throws AccessException {
    return new TypedValue(((BeanExpressionContext) target).getObject(name));
}
项目:spring4-understanding    文件:BeanExpressionContextAccessor.java   
@Override
public Class<?>[] getSpecificTargetClasses() {
    return new Class<?>[] {BeanExpressionContext.class};
}
项目:my-spring-cache-redis    文件:BeanExpressionContextAccessor.java   
@Override
public boolean canRead(EvaluationContext context, Object target, String name) throws AccessException {
    return ((BeanExpressionContext) target).containsObject(name);
}
项目:my-spring-cache-redis    文件:BeanExpressionContextAccessor.java   
@Override
public TypedValue read(EvaluationContext context, Object target, String name) throws AccessException {
    return new TypedValue(((BeanExpressionContext) target).getObject(name));
}
项目:my-spring-cache-redis    文件:BeanExpressionContextAccessor.java   
@Override
public Class<?>[] getSpecificTargetClasses() {
    return new Class<?>[] {BeanExpressionContext.class};
}
项目:java-restify    文件:SpelDynamicParameterExpressionResolver.java   
public SpelDynamicParameterExpressionResolver(ConfigurableBeanFactory beanFactory) {
    this.beanFactory = beanFactory;
    this.resolver = beanFactory.getBeanExpressionResolver();
    this.context = new BeanExpressionContext(beanFactory, null);
}
项目:spring    文件:BeanExpressionContextAccessor.java   
@Override
public boolean canRead(EvaluationContext context, Object target, String name) throws AccessException {
    return ((BeanExpressionContext) target).containsObject(name);
}
项目:spring    文件:BeanExpressionContextAccessor.java   
@Override
public TypedValue read(EvaluationContext context, Object target, String name) throws AccessException {
    return new TypedValue(((BeanExpressionContext) target).getObject(name));
}
项目:spring    文件:BeanExpressionContextAccessor.java   
@Override
public Class<?>[] getSpecificTargetClasses() {
    return new Class<?>[] {BeanExpressionContext.class};
}
项目:sdcct    文件:EmbeddedPlaceholderResolver.java   
@Override
public void afterPropertiesSet() throws Exception {
    this.beanExprResolver = this.beanFactory.getBeanExpressionResolver();
    this.beanExprContext = new BeanExpressionContext(this.beanFactory, null);
}
项目:spring-cloud-stream    文件:StreamListenerAnnotationBeanPostProcessor.java   
@Override
public final void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    this.applicationContext = (ConfigurableApplicationContext) applicationContext;
    this.resolver = this.applicationContext.getBeanFactory().getBeanExpressionResolver();
    this.expressionContext = new BeanExpressionContext(this.applicationContext.getBeanFactory(), null);
}
项目:JDeSurvey    文件:DateTimeFormatAnnotationFormatterFactory.java   
private String evaluateExpression(String expression) {
    Object value = beanFactory.getBeanExpressionResolver().evaluate(
            expression, new BeanExpressionContext(beanFactory, null));
    return value != null ? value.toString() : null;
}
项目:class-guard    文件:AnnotationMethodHandlerAdapter.java   
public void setBeanFactory(BeanFactory beanFactory) {
    if (beanFactory instanceof ConfigurableBeanFactory) {
        this.beanFactory = (ConfigurableBeanFactory) beanFactory;
        this.expressionContext = new BeanExpressionContext(this.beanFactory, new RequestScope());
    }
}
项目:class-guard    文件:BeanExpressionContextAccessor.java   
public boolean canRead(EvaluationContext context, Object target, String name) throws AccessException {
    return ((BeanExpressionContext) target).containsObject(name);
}
项目:class-guard    文件:BeanExpressionContextAccessor.java   
public TypedValue read(EvaluationContext context, Object target, String name) throws AccessException {
    return new TypedValue(((BeanExpressionContext) target).getObject(name));
}
项目:class-guard    文件:BeanExpressionContextAccessor.java   
public Class[] getSpecificTargetClasses() {
    return new Class[] {BeanExpressionContext.class};
}
项目:class-guard    文件:AnnotationMethodHandlerAdapter.java   
public void setBeanFactory(BeanFactory beanFactory) {
    if (beanFactory instanceof ConfigurableBeanFactory) {
        this.beanFactory = (ConfigurableBeanFactory) beanFactory;
        this.expressionContext = new BeanExpressionContext(this.beanFactory, new RequestScope());
    }
}
项目:lams    文件:AbstractBeanFactory.java   
/**
 * Evaluate the given String as contained in a bean definition,
 * potentially resolving it as an expression.
 * @param value the value to check
 * @param beanDefinition the bean definition that the value comes from
 * @return the resolved value
 * @see #setBeanExpressionResolver
 */
protected Object evaluateBeanDefinitionString(String value, BeanDefinition beanDefinition) {
    if (this.beanExpressionResolver == null) {
        return value;
    }
    Scope scope = (beanDefinition != null ? getRegisteredScope(beanDefinition.getScope()) : null);
    return this.beanExpressionResolver.evaluate(value, new BeanExpressionContext(this, scope));
}
项目:spring4-understanding    文件:AbstractBeanFactory.java   
/**
 * Evaluate the given String as contained in a bean definition,
 * potentially resolving it as an expression.
 * @param value the value to check
 * @param beanDefinition the bean definition that the value comes from
 * @return the resolved value
 * @see #setBeanExpressionResolver
 */
protected Object evaluateBeanDefinitionString(String value, BeanDefinition beanDefinition) {
    if (this.beanExpressionResolver == null) {
        return value;
    }
    Scope scope = (beanDefinition != null ? getRegisteredScope(beanDefinition.getScope()) : null);
    return this.beanExpressionResolver.evaluate(value, new BeanExpressionContext(this, scope));
}
项目:spring    文件:AbstractBeanFactory.java   
/**
 * Evaluate the given String as contained in a bean definition,
 * potentially resolving it as an expression.
 * @param value the value to check
 * @param beanDefinition the bean definition that the value comes from
 * @return the resolved value
 * @see #setBeanExpressionResolver
 */
protected Object evaluateBeanDefinitionString(String value, BeanDefinition beanDefinition) {
    if (this.beanExpressionResolver == null) {
        return value;
    }
    Scope scope = (beanDefinition != null ? getRegisteredScope(beanDefinition.getScope()) : null);
    return this.beanExpressionResolver.evaluate(value, new BeanExpressionContext(this, scope));
}