org.springframework.beans.factory.config.BeanExpressionResolver Java Examples

The following examples show how to use org.springframework.beans.factory.config.BeanExpressionResolver. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SendToHandlerMethodReturnValueHandler.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #2
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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"));
}
 
Example #3
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testExpressionInStringArray() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	BeanExpressionResolver beanExpressionResolver = mock(BeanExpressionResolver.class);
	given(beanExpressionResolver.evaluate(eq("#{foo}"), any(BeanExpressionContext.class)))
			.willReturn("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"));
}
 
Example #4
Source File: ValueResolver.java    From rqueue with Apache License 2.0 6 votes vote down vote up
@NonNull
private static Object resolveExpression(ApplicationContext applicationContext, String name) {
  if (applicationContext instanceof ConfigurableApplicationContext) {
    ConfigurableBeanFactory configurableBeanFactory =
        ((ConfigurableApplicationContext) applicationContext).getBeanFactory();
    String placeholdersResolved = configurableBeanFactory.resolveEmbeddedValue(name);
    BeanExpressionResolver exprResolver = configurableBeanFactory.getBeanExpressionResolver();
    if (exprResolver == null) {
      return name;
    }
    Object result =
        exprResolver.evaluate(
            placeholdersResolved, new BeanExpressionContext(configurableBeanFactory, null));
    if (result != null) {
      return result;
    }
  }
  return name;
}
 
Example #5
Source File: ServerEndpointExporter.java    From netty-websocket-spring-boot-starter with Apache License 2.0 6 votes vote down vote up
private <T> T resolveAnnotationValue(Object value, Class<T> requiredType, String paramName) {
    if (value == null) {
        return null;
    }
    TypeConverter typeConverter = beanFactory.getTypeConverter();

    if (value instanceof String) {
        String strVal = beanFactory.resolveEmbeddedValue((String) value);
        BeanExpressionResolver beanExpressionResolver = beanFactory.getBeanExpressionResolver();
        if (beanExpressionResolver != null) {
            value = beanExpressionResolver.evaluate(strVal, new BeanExpressionContext(beanFactory, null));
        } else {
            value = strVal;
        }
    }
    try {
        return typeConverter.convertIfNecessary(value, requiredType);
    } catch (TypeMismatchException e) {
        throw new IllegalArgumentException("Failed to convert value of parameter '" + paramName + "' to required type '" + requiredType.getName() + "'");
    }
}
 
Example #6
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testExpressionInStringArray() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	BeanExpressionResolver beanExpressionResolver = mock(BeanExpressionResolver.class);
	when(beanExpressionResolver.evaluate(eq("#{foo}"), ArgumentMatchers.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"));
}
 
Example #7
Source File: AnnotationMethodHandlerAdapter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected Object resolveDefaultValue(String value) {
	if (beanFactory == null) {
		return value;
	}
	String placeholdersResolved = beanFactory.resolveEmbeddedValue(value);
	BeanExpressionResolver exprResolver = beanFactory.getBeanExpressionResolver();
	if (exprResolver == null) {
		return value;
	}
	return exprResolver.evaluate(placeholdersResolved, expressionContext);
}
 
Example #8
Source File: QueueMessageHandler.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
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);
	}
}
 
Example #9
Source File: ApplicationMetricsProperties.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
private Map<String, Object> buildExportProperties() {
	Map<String, Object> props = new HashMap<>();
	if (!ObjectUtils.isEmpty(this.properties)) {
		Map<String, String> target = bindProperties();

		BeanExpressionResolver beanExpressionResolver = ((ConfigurableApplicationContext) this.applicationContext)
				.getBeanFactory().getBeanExpressionResolver();
		BeanExpressionContext expressionContext = new BeanExpressionContext(
				((ConfigurableApplicationContext) this.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(
									this.environment.resolvePlaceholders(stringValue),
									expressionContext)
							: this.environment.resolvePlaceholders(stringValue);
				}

				props.put(entry.getKey(), exportedValue);
			}
		}
	}
	return props;
}
 
Example #10
Source File: AbstractNamedValueMethodArgumentResolver.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Resolves the given default value into an argument value.
 */
private Object resolveDefaultValue(String defaultValue) {
	if (this.configurableBeanFactory == null) {
		return defaultValue;
	}
	String placeholdersResolved = this.configurableBeanFactory.resolveEmbeddedValue(defaultValue);
	BeanExpressionResolver exprResolver = this.configurableBeanFactory.getBeanExpressionResolver();
	if (exprResolver == null) {
		return defaultValue;
	}
	return exprResolver.evaluate(placeholdersResolved, this.expressionContext);
}
 
Example #11
Source File: AnnotationMethodHandlerAdapter.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
protected Object resolveDefaultValue(String value) {
	if (beanFactory == null) {
		return value;
	}
	String placeholdersResolved = beanFactory.resolveEmbeddedValue(value);
	BeanExpressionResolver exprResolver = beanFactory.getBeanExpressionResolver();
	if (exprResolver == null) {
		return value;
	}
	return exprResolver.evaluate(placeholdersResolved, expressionContext);
}
 
Example #12
Source File: AnnotationMethodHandlerAdapter.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
protected Object resolveDefaultValue(String value) {
	if (beanFactory == null) {
		return value;
	}
	String placeholdersResolved = beanFactory.resolveEmbeddedValue(value);
	BeanExpressionResolver exprResolver = beanFactory.getBeanExpressionResolver();
	if (exprResolver == null) {
		return value;
	}
	return exprResolver.evaluate(placeholdersResolved, expressionContext);
}
 
Example #13
Source File: AbstractNamedValueMethodArgumentResolver.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Resolves the given default value into an argument value.
 */
private Object resolveDefaultValue(String defaultValue) {
	if (this.configurableBeanFactory == null) {
		return defaultValue;
	}
	String placeholdersResolved = this.configurableBeanFactory.resolveEmbeddedValue(defaultValue);
	BeanExpressionResolver exprResolver = this.configurableBeanFactory.getBeanExpressionResolver();
	if (exprResolver == null) {
		return defaultValue;
	}
	return exprResolver.evaluate(placeholdersResolved, this.expressionContext);
}
 
Example #14
Source File: AbstractNamedValueMethodArgumentResolver.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Resolve the given annotation-specified value,
 * potentially containing placeholders and expressions.
 */
private Object resolveStringValue(String value) {
	if (this.configurableBeanFactory == null) {
		return value;
	}
	String placeholdersResolved = this.configurableBeanFactory.resolveEmbeddedValue(value);
	BeanExpressionResolver exprResolver = this.configurableBeanFactory.getBeanExpressionResolver();
	if (exprResolver == null) {
		return value;
	}
	return exprResolver.evaluate(placeholdersResolved, this.expressionContext);
}
 
Example #15
Source File: AbstractNamedValueMethodArgumentResolver.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Resolve the given annotation-specified value,
 * potentially containing placeholders and expressions.
 */
private Object resolveStringValue(String value) {
	if (this.configurableBeanFactory == null) {
		return value;
	}
	String placeholdersResolved = this.configurableBeanFactory.resolveEmbeddedValue(value);
	BeanExpressionResolver exprResolver = this.configurableBeanFactory.getBeanExpressionResolver();
	if (exprResolver == null) {
		return value;
	}
	return exprResolver.evaluate(placeholdersResolved, this.expressionContext);
}
 
Example #16
Source File: AbstractNamedValueMethodArgumentResolver.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Resolve the given annotation-specified value,
 * potentially containing placeholders and expressions.
 */
@Nullable
private Object resolveEmbeddedValuesAndExpressions(String value) {
	if (this.configurableBeanFactory == null || this.expressionContext == null) {
		return value;
	}
	String placeholdersResolved = this.configurableBeanFactory.resolveEmbeddedValue(value);
	BeanExpressionResolver exprResolver = this.configurableBeanFactory.getBeanExpressionResolver();
	if (exprResolver == null) {
		return value;
	}
	return exprResolver.evaluate(placeholdersResolved, this.expressionContext);
}
 
Example #17
Source File: AbstractNamedValueMethodArgumentResolver.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Resolve the given annotation-specified value,
 * potentially containing placeholders and expressions.
 */
@Nullable
private Object resolveEmbeddedValuesAndExpressions(String value) {
	if (this.configurableBeanFactory == null || this.expressionContext == null) {
		return value;
	}
	String placeholdersResolved = this.configurableBeanFactory.resolveEmbeddedValue(value);
	BeanExpressionResolver exprResolver = this.configurableBeanFactory.getBeanExpressionResolver();
	if (exprResolver == null) {
		return value;
	}
	return exprResolver.evaluate(placeholdersResolved, this.expressionContext);
}
 
Example #18
Source File: AbstractNamedValueArgumentResolver.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Resolve the given annotation-specified value,
 * potentially containing placeholders and expressions.
 */
@Nullable
private Object resolveStringValue(String value) {
	if (this.configurableBeanFactory == null || this.expressionContext == null) {
		return value;
	}
	String placeholdersResolved = this.configurableBeanFactory.resolveEmbeddedValue(value);
	BeanExpressionResolver exprResolver = this.configurableBeanFactory.getBeanExpressionResolver();
	if (exprResolver == null) {
		return value;
	}
	return exprResolver.evaluate(placeholdersResolved, this.expressionContext);
}
 
Example #19
Source File: AbstractNamedValueMethodArgumentResolver.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Resolve the given annotation-specified value,
 * potentially containing placeholders and expressions.
 */
@Nullable
private Object resolveStringValue(String value) {
	if (this.configurableBeanFactory == null) {
		return value;
	}
	String placeholdersResolved = this.configurableBeanFactory.resolveEmbeddedValue(value);
	BeanExpressionResolver exprResolver = this.configurableBeanFactory.getBeanExpressionResolver();
	if (exprResolver == null || this.expressionContext == null) {
		return value;
	}
	return exprResolver.evaluate(placeholdersResolved, this.expressionContext);
}
 
Example #20
Source File: AbstractNamedValueArgumentResolver.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Resolve the given annotation-specified value,
 * potentially containing placeholders and expressions.
 */
@Nullable
private Object resolveStringValue(String value) {
	if (this.configurableBeanFactory == null || this.expressionContext == null) {
		return value;
	}
	String placeholdersResolved = this.configurableBeanFactory.resolveEmbeddedValue(value);
	BeanExpressionResolver exprResolver = this.configurableBeanFactory.getBeanExpressionResolver();
	if (exprResolver == null) {
		return value;
	}
	return exprResolver.evaluate(placeholdersResolved, this.expressionContext);
}
 
Example #21
Source File: AbstractNamedValueMethodArgumentResolver.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Resolve the given annotation-specified value,
 * potentially containing placeholders and expressions.
 */
@Nullable
private Object resolveStringValue(String value) {
	if (this.configurableBeanFactory == null) {
		return value;
	}
	String placeholdersResolved = this.configurableBeanFactory.resolveEmbeddedValue(value);
	BeanExpressionResolver exprResolver = this.configurableBeanFactory.getBeanExpressionResolver();
	if (exprResolver == null || this.expressionContext == null) {
		return value;
	}
	return exprResolver.evaluate(placeholdersResolved, this.expressionContext);
}
 
Example #22
Source File: AbstractBeanFactory.java    From blog_demos with Apache License 2.0 4 votes vote down vote up
@Override
public BeanExpressionResolver getBeanExpressionResolver() {
	return this.beanExpressionResolver;
}
 
Example #23
Source File: AbstractBeanFactory.java    From blog_demos with Apache License 2.0 4 votes vote down vote up
@Override
public void setBeanExpressionResolver(BeanExpressionResolver resolver) {
	this.beanExpressionResolver = resolver;
}
 
Example #24
Source File: AbstractBeanFactory.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
@Nullable
public BeanExpressionResolver getBeanExpressionResolver() {
	return this.beanExpressionResolver;
}
 
Example #25
Source File: AbstractBeanFactory.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
@Nullable
public BeanExpressionResolver getBeanExpressionResolver() {
	return this.beanExpressionResolver;
}
 
Example #26
Source File: AbstractBeanFactory.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public void setBeanExpressionResolver(@Nullable BeanExpressionResolver resolver) {
	this.beanExpressionResolver = resolver;
}
 
Example #27
Source File: AbstractBeanFactory.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public void setBeanExpressionResolver(BeanExpressionResolver resolver) {
	this.beanExpressionResolver = resolver;
}
 
Example #28
Source File: AbstractBeanFactory.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public BeanExpressionResolver getBeanExpressionResolver() {
	return this.beanExpressionResolver;
}
 
Example #29
Source File: AbstractBeanFactory.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public BeanExpressionResolver getBeanExpressionResolver() {
	return this.beanExpressionResolver;
}
 
Example #30
Source File: AbstractBeanFactory.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void setBeanExpressionResolver(BeanExpressionResolver resolver) {
	this.beanExpressionResolver = resolver;
}