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

The following examples show how to use org.springframework.beans.factory.config.BeanExpressionContext. 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: 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 #2
Source File: AbstractBeanFactory.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * 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
 */
@Nullable
protected Object evaluateBeanDefinitionString(@Nullable String value, @Nullable BeanDefinition beanDefinition) {
	if (this.beanExpressionResolver == null) {
		return value;
	}

	Scope scope = null;
	if (beanDefinition != null) {
		String scopeName = beanDefinition.getScope();
		if (scopeName != null) {
			scope = getRegisteredScope(scopeName);
		}
	}
	return this.beanExpressionResolver.evaluate(value, new BeanExpressionContext(this, scope));
}
 
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: AbstractBeanFactory.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * 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
 */
@Nullable
protected Object evaluateBeanDefinitionString(@Nullable String value, @Nullable BeanDefinition beanDefinition) {
	if (this.beanExpressionResolver == null) {
		return value;
	}

	Scope scope = null;
	if (beanDefinition != null) {
		String scopeName = beanDefinition.getScope();
		if (scopeName != null) {
			scope = getRegisteredScope(scopeName);
		}
	}
	return this.beanExpressionResolver.evaluate(value, new BeanExpressionContext(this, scope));
}
 
Example #5
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 #6
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 #7
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 #8
Source File: AmazonEc2InstanceUserTagsFactoryBeanAwsTest.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Test
void testGetUserProperties() throws Exception {

	Assertions.assertEquals("tagv1",
			this.context.getBeanFactory().getBeanExpressionResolver().evaluate(
					"#{instanceData['tag1']}",
					new BeanExpressionContext(this.context.getBeanFactory(), null)));
	Assertions.assertEquals("tagv2",
			this.context.getBeanFactory().getBeanExpressionResolver().evaluate(
					"#{instanceData['tag2']}",
					new BeanExpressionContext(this.context.getBeanFactory(), null)));
	Assertions.assertEquals("tagv3",
			this.context.getBeanFactory().getBeanExpressionResolver().evaluate(
					"#{instanceData['tag3']}",
					new BeanExpressionContext(this.context.getBeanFactory(), null)));
	Assertions.assertEquals("tagv4",
			this.context.getBeanFactory().getBeanExpressionResolver().evaluate(
					"#{instanceData['tag4']}",
					new BeanExpressionContext(this.context.getBeanFactory(), null)));
}
 
Example #9
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 #10
Source File: AbstractNamedValueMethodArgumentResolver.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Constructor with a {@link ConversionService} and a {@link BeanFactory}.
 * @param conversionService conversion service for converting String values
 * to the target method parameter type
 * @param beanFactory a bean factory for resolving {@code ${...}}
 * placeholders and {@code #{...}} SpEL expressions in default values
 */
protected AbstractNamedValueMethodArgumentResolver(ConversionService conversionService,
		@Nullable ConfigurableBeanFactory beanFactory) {

	this.conversionService = conversionService;
	this.configurableBeanFactory = beanFactory;
	this.expressionContext = (beanFactory != null ? new BeanExpressionContext(beanFactory, null) : null);
}
 
Example #11
Source File: AbstractNamedValueMethodArgumentResolver.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Constructor with a {@link ConversionService} and a {@link BeanFactory}.
 * @param conversionService conversion service for converting String values
 * to the target method parameter type
 * @param beanFactory a bean factory for resolving {@code ${...}}
 * placeholders and {@code #{...}} SpEL expressions in default values
 */
protected AbstractNamedValueMethodArgumentResolver(ConversionService conversionService,
		@Nullable ConfigurableBeanFactory beanFactory) {

	this.conversionService = conversionService;
	this.configurableBeanFactory = beanFactory;
	this.expressionContext = (beanFactory != null ? new BeanExpressionContext(beanFactory, null) : null);
}
 
Example #12
Source File: AbstractNamedValueArgumentResolver.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Create a new {@link AbstractNamedValueArgumentResolver} instance.
 * @param factory a bean factory to use for resolving {@code ${...}} placeholder
 * and {@code #{...}} SpEL expressions in default values, or {@code null} if default
 * values are not expected to contain expressions
 * @param registry for checking reactive type wrappers
 */
public AbstractNamedValueArgumentResolver(@Nullable ConfigurableBeanFactory factory,
		ReactiveAdapterRegistry registry) {

	super(registry);
	this.configurableBeanFactory = factory;
	this.expressionContext = (factory != null ? new BeanExpressionContext(factory, null) : null);
}
 
Example #13
Source File: AbstractNamedValueArgumentResolver.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Create a new {@link AbstractNamedValueArgumentResolver} instance.
 * @param factory a bean factory to use for resolving {@code ${...}} placeholder
 * and {@code #{...}} SpEL expressions in default values, or {@code null} if default
 * values are not expected to contain expressions
 * @param registry for checking reactive type wrappers
 */
public AbstractNamedValueArgumentResolver(@Nullable ConfigurableBeanFactory factory,
		ReactiveAdapterRegistry registry) {

	super(registry);
	this.configurableBeanFactory = factory;
	this.expressionContext = (factory != null ? new BeanExpressionContext(factory, null) : null);
}
 
Example #14
Source File: AnnotationMethodHandlerAdapter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void setBeanFactory(BeanFactory beanFactory) {
	if (beanFactory instanceof ConfigurableBeanFactory) {
		this.beanFactory = (ConfigurableBeanFactory) beanFactory;
		this.expressionContext = new BeanExpressionContext(this.beanFactory, new RequestScope());
	}
}
 
Example #15
Source File: ZeebeExpressionResolver.java    From spring-zeebe with Apache License 2.0 5 votes vote down vote up
@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);
  }
}
 
Example #16
Source File: PlaceholderHelper.java    From apollo with Apache License 2.0 5 votes vote down vote up
private Object evaluateBeanDefinitionString(ConfigurableBeanFactory beanFactory, String value,
    BeanDefinition beanDefinition) {
  if (beanFactory.getBeanExpressionResolver() == null) {
    return value;
  }
  Scope scope = (beanDefinition != null ? beanFactory
      .getRegisteredScope(beanDefinition.getScope()) : null);
  return beanFactory.getBeanExpressionResolver()
      .evaluate(value, new BeanExpressionContext(beanFactory, scope));
}
 
Example #17
Source File: AnnotationMethodHandlerAdapter.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void setBeanFactory(BeanFactory beanFactory) {
	if (beanFactory instanceof ConfigurableBeanFactory) {
		this.beanFactory = (ConfigurableBeanFactory) beanFactory;
		this.expressionContext = new BeanExpressionContext(this.beanFactory, new RequestScope());
	}
}
 
Example #18
Source File: AnnotationMethodHandlerAdapter.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void setBeanFactory(BeanFactory beanFactory) {
	if (beanFactory instanceof ConfigurableBeanFactory) {
		this.beanFactory = (ConfigurableBeanFactory) beanFactory;
		this.expressionContext = new BeanExpressionContext(this.beanFactory, new RequestScope());
	}
}
 
Example #19
Source File: RabbitChannelDefinitionProcessor.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
    this.beanFactory = beanFactory;
    if (beanFactory instanceof ConfigurableListableBeanFactory) {
        this.embeddedValueResolver = new EmbeddedValueResolver((ConfigurableBeanFactory) beanFactory);
        this.resolver = ((ConfigurableListableBeanFactory) beanFactory).getBeanExpressionResolver();
        this.expressionContext = new BeanExpressionContext((ConfigurableListableBeanFactory) beanFactory, null);
    }
}
 
Example #20
Source File: KafkaChannelDefinitionProcessor.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
    this.beanFactory = beanFactory;
    if (beanFactory instanceof ConfigurableListableBeanFactory) {
        this.embeddedValueResolver = new EmbeddedValueResolver((ConfigurableBeanFactory) beanFactory);
        this.resolver = ((ConfigurableListableBeanFactory) beanFactory).getBeanExpressionResolver();
        this.expressionContext = new BeanExpressionContext((ConfigurableListableBeanFactory) beanFactory, null);
    }
}
 
Example #21
Source File: IgniteRepositoryFactory.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * Creates the factory with initialized {@link Ignite} instance.
 *
 * @param ctx the ctx
 */
public IgniteRepositoryFactory(ApplicationContext ctx) {
    this.ctx = ctx;

    beanFactory = new DefaultListableBeanFactory(ctx.getAutowireCapableBeanFactory());

    beanExpressionContext = new BeanExpressionContext(beanFactory, null);
}
 
Example #22
Source File: IgniteRepositoryFactory.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * Creates the factory with initialized {@link Ignite} instance.
 *
 * @param ctx the ctx
 */
public IgniteRepositoryFactory(ApplicationContext ctx) {
    this.ctx = ctx;

    beanFactory = new DefaultListableBeanFactory(ctx.getAutowireCapableBeanFactory());

    beanExpressionContext = new BeanExpressionContext(beanFactory, null);
}
 
Example #23
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 #24
Source File: StreamListenerAnnotationBeanPostProcessor.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #25
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 #26
Source File: DataDictionary.java    From rice with Educational Community License v2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #27
Source File: BeanExpressionContextAccessor.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public boolean canRead(EvaluationContext context, @Nullable Object target, String name) throws AccessException {
	return (target instanceof BeanExpressionContext && ((BeanExpressionContext) target).containsObject(name));
}
 
Example #28
Source File: BeanExpressionContextAccessor.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public TypedValue read(EvaluationContext context, @Nullable Object target, String name) throws AccessException {
	Assert.state(target instanceof BeanExpressionContext, "Target must be of type BeanExpressionContext");
	return new TypedValue(((BeanExpressionContext) target).getObject(name));
}
 
Example #29
Source File: BeanExpressionContextAccessor.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public Class<?>[] getSpecificTargetClasses() {
	return new Class<?>[] {BeanExpressionContext.class};
}
 
Example #30
Source File: BeanExpressionContextAccessor.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public boolean canRead(EvaluationContext context, @Nullable Object target, String name) throws AccessException {
	return (target instanceof BeanExpressionContext && ((BeanExpressionContext) target).containsObject(name));
}