Search in sources :

Example 1 with StringValueResolver

use of org.springframework.util.StringValueResolver in project spring-framework by spring-projects.

the class AbstractApplicationContext method finishBeanFactoryInitialization.

/**
	 * Finish the initialization of this context's bean factory,
	 * initializing all remaining singleton beans.
	 */
protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {
    // Initialize conversion service for this context.
    if (beanFactory.containsBean(CONVERSION_SERVICE_BEAN_NAME) && beanFactory.isTypeMatch(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)) {
        beanFactory.setConversionService(beanFactory.getBean(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class));
    }
    // at this point, primarily for resolution in annotation attribute values.
    if (!beanFactory.hasEmbeddedValueResolver()) {
        beanFactory.addEmbeddedValueResolver(new StringValueResolver() {

            @Override
            public String resolveStringValue(String strVal) {
                return getEnvironment().resolvePlaceholders(strVal);
            }
        });
    }
    // Initialize LoadTimeWeaverAware beans early to allow for registering their transformers early.
    String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false);
    for (String weaverAwareName : weaverAwareNames) {
        getBean(weaverAwareName);
    }
    // Stop using the temporary ClassLoader for type matching.
    beanFactory.setTempClassLoader(null);
    // Allow for caching all bean definition metadata, not expecting further changes.
    beanFactory.freezeConfiguration();
    // Instantiate all remaining (non-lazy-init) singletons.
    beanFactory.preInstantiateSingletons();
}
Also used : StringValueResolver(org.springframework.util.StringValueResolver) ConversionService(org.springframework.core.convert.ConversionService)

Example 2 with StringValueResolver

use of org.springframework.util.StringValueResolver in project hale by halestudio.

the class ConfigurationServicePlaceholderConfigurer method processProperties.

@Override
protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props) throws BeansException {
    StringValueResolver valueResolver = new PlaceholderResolvingStringValueResolver();
    this.doProcessProperties(beanFactory, valueResolver);
}
Also used : StringValueResolver(org.springframework.util.StringValueResolver)

Example 3 with StringValueResolver

use of org.springframework.util.StringValueResolver in project incubator-servicecomb-java-chassis by apache.

the class TestSwaggerGenerator method testBasePathPlaceHolder.

@Test
public void testBasePathPlaceHolder() {
    StringValueResolver stringValueResolver = SpringUtils.createStringValueResolver(Collections.singletonMap("var", "varValue"));
    PojoSwaggerGeneratorContext context = new PojoSwaggerGeneratorContext();
    context.setEmbeddedValueResolver(stringValueResolver);
    SwaggerGenerator swaggerGenerator = new SwaggerGenerator(context, null);
    swaggerGenerator.setBasePath("/a/${var}/b");
    Assert.assertEquals("/a/varValue/b", swaggerGenerator.getSwagger().getBasePath());
}
Also used : StringValueResolver(org.springframework.util.StringValueResolver) PojoSwaggerGeneratorContext(org.apache.servicecomb.swagger.generator.pojo.PojoSwaggerGeneratorContext) Test(org.junit.Test)

Example 4 with StringValueResolver

use of org.springframework.util.StringValueResolver in project disconf by knightliao.

the class ReloadingPropertyPlaceholderConfigurer method processProperties.

/**
 * copy & paste, just so we can insert our own visitor.
 * 启动时 进行配置的解析
 */
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException {
    BeanDefinitionVisitor visitor = new ReloadingPropertyPlaceholderConfigurer.PlaceholderResolvingBeanDefinitionVisitor(props);
    String[] beanNames = beanFactoryToProcess.getBeanDefinitionNames();
    for (int i = 0; i < beanNames.length; i++) {
        // .unicon.iamlabs.spring.properties file locations.
        if (!(beanNames[i].equals(this.beanName) && beanFactoryToProcess.equals(this.beanFactory))) {
            this.currentBeanName = beanNames[i];
            try {
                BeanDefinition bd = beanFactoryToProcess.getBeanDefinition(beanNames[i]);
                try {
                    visitor.visitBeanDefinition(bd);
                } catch (BeanDefinitionStoreException ex) {
                    throw new BeanDefinitionStoreException(bd.getResourceDescription(), beanNames[i], ex.getMessage());
                }
            } finally {
                currentBeanName = null;
            }
        }
    }
    StringValueResolver stringValueResolver = new PlaceholderResolvingStringValueResolver(props);
    // New in Spring 2.5: resolve placeholders in alias target names and aliases as well.
    beanFactoryToProcess.resolveAliases(stringValueResolver);
    // New in Spring 3.0: resolve placeholders in embedded values such as annotation attributes.
    beanFactoryToProcess.addEmbeddedValueResolver(stringValueResolver);
}
Also used : StringValueResolver(org.springframework.util.StringValueResolver) BeanDefinitionVisitor(org.springframework.beans.factory.config.BeanDefinitionVisitor) BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 5 with StringValueResolver

use of org.springframework.util.StringValueResolver in project spring-framework by spring-projects.

the class DefaultListableBeanFactoryTests method resolveEmbeddedValue.

@Test
void resolveEmbeddedValue() {
    StringValueResolver r1 = mock(StringValueResolver.class);
    StringValueResolver r2 = mock(StringValueResolver.class);
    StringValueResolver r3 = mock(StringValueResolver.class);
    lbf.addEmbeddedValueResolver(r1);
    lbf.addEmbeddedValueResolver(r2);
    lbf.addEmbeddedValueResolver(r3);
    given(r1.resolveStringValue("A")).willReturn("B");
    given(r2.resolveStringValue("B")).willReturn(null);
    given(r3.resolveStringValue(isNull())).willThrow(new IllegalArgumentException());
    lbf.resolveEmbeddedValue("A");
    verify(r1).resolveStringValue("A");
    verify(r2).resolveStringValue("B");
    verify(r3, never()).resolveStringValue(isNull());
}
Also used : StringValueResolver(org.springframework.util.StringValueResolver) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) Test(org.junit.jupiter.api.Test)

Aggregations

StringValueResolver (org.springframework.util.StringValueResolver)14 BeanDefinitionVisitor (org.springframework.beans.factory.config.BeanDefinitionVisitor)5 BeanDefinitionStoreException (org.springframework.beans.factory.BeanDefinitionStoreException)3 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)3 PlaceholderResolvingStringValueResolver (com.revolsys.spring.util.PlaceholderResolvingStringValueResolver)2 PojoSwaggerGeneratorContext (org.apache.servicecomb.swagger.generator.pojo.PojoSwaggerGeneratorContext)2 Test (org.junit.Test)2 GenericBeanDefinition (org.springframework.beans.factory.support.GenericBeanDefinition)2 BeanReference (com.revolsys.spring.BeanReference)1 Date (java.util.Date)1 LinkedHashMap (java.util.LinkedHashMap)1 TimeZone (java.util.TimeZone)1 CasEmbeddedValueResolver (org.apereo.cas.CasEmbeddedValueResolver)1 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)1 Test (org.junit.jupiter.api.Test)1 ConversionService (org.springframework.core.convert.ConversionService)1 ScheduledAnnotationBeanPostProcessor (org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor)1 CronSequenceGenerator (org.springframework.scheduling.support.CronSequenceGenerator)1