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

下面列出了怎么用org.springframework.beans.factory.config.BeanExpressionContext的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: rqueue   文件: ValueResolver.java
@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;
}
 
/**
 * 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));
}
 
@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"));
}
 
/**
 * 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));
}
 
@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"));
}
 
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() + "'");
    }
}
 
@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"));
}
 
@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)));
}
 
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;
}
 
/**
 * 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);
}
 
/**
 * 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);
}
 
/**
 * 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);
}
 
/**
 * 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);
}
 
源代码14 项目: lams   文件: AnnotationMethodHandlerAdapter.java
@Override
public void setBeanFactory(BeanFactory beanFactory) {
	if (beanFactory instanceof ConfigurableBeanFactory) {
		this.beanFactory = (ConfigurableBeanFactory) beanFactory;
		this.expressionContext = new BeanExpressionContext(this.beanFactory, new RequestScope());
	}
}
 
源代码15 项目: 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);
  }
}
 
源代码16 项目: apollo   文件: PlaceholderHelper.java
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));
}
 
@Override
public void setBeanFactory(BeanFactory beanFactory) {
	if (beanFactory instanceof ConfigurableBeanFactory) {
		this.beanFactory = (ConfigurableBeanFactory) beanFactory;
		this.expressionContext = new BeanExpressionContext(this.beanFactory, new RequestScope());
	}
}
 
@Override
public void setBeanFactory(BeanFactory beanFactory) {
	if (beanFactory instanceof ConfigurableBeanFactory) {
		this.beanFactory = (ConfigurableBeanFactory) beanFactory;
		this.expressionContext = new BeanExpressionContext(this.beanFactory, new RequestScope());
	}
}
 
@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);
    }
}
 
@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);
    }
}
 
源代码21 项目: ignite   文件: IgniteRepositoryFactory.java
/**
 * 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);
}
 
源代码22 项目: ignite   文件: IgniteRepositoryFactory.java
/**
 * 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);
}
 
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;
}
 
@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);
}
 
源代码25 项目: 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);
	}
}
 
源代码26 项目: 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;
}
 
@Override
public boolean canRead(EvaluationContext context, @Nullable Object target, String name) throws AccessException {
	return (target instanceof BeanExpressionContext && ((BeanExpressionContext) target).containsObject(name));
}
 
@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));
}
 
@Override
public Class<?>[] getSpecificTargetClasses() {
	return new Class<?>[] {BeanExpressionContext.class};
}
 
@Override
public boolean canRead(EvaluationContext context, @Nullable Object target, String name) throws AccessException {
	return (target instanceof BeanExpressionContext && ((BeanExpressionContext) target).containsObject(name));
}
 
 同包方法