@Test public void propertyPlaceholder() throws Exception { ApplicationContext applicationContext = new ClassPathXmlApplicationContext( "contextNamespaceHandlerTests-replace.xml", getClass()); Map<String, PlaceholderConfigurerSupport> beans = applicationContext .getBeansOfType(PlaceholderConfigurerSupport.class); assertFalse("No PropertyPlaceholderConfigurer found", beans.isEmpty()); assertEquals("bar", applicationContext.getBean("string")); assertEquals("null", applicationContext.getBean("nullString")); }
@Test public void propertyPlaceholderEnvironmentProperties() throws Exception { MockEnvironment env = new MockEnvironment().withProperty("foo", "spam"); GenericXmlApplicationContext applicationContext = new GenericXmlApplicationContext(); applicationContext.setEnvironment(env); applicationContext.load(new ClassPathResource("contextNamespaceHandlerTests-simple.xml", getClass())); applicationContext.refresh(); Map<String, PlaceholderConfigurerSupport> beans = applicationContext .getBeansOfType(PlaceholderConfigurerSupport.class); assertFalse("No PropertyPlaceholderConfigurer found", beans.isEmpty()); assertEquals("spam", applicationContext.getBean("string")); assertEquals("none", applicationContext.getBean("fallback")); }
@Test public void propertyPlaceholderIgnored() throws Exception { ApplicationContext applicationContext = new ClassPathXmlApplicationContext( "contextNamespaceHandlerTests-replace-ignore.xml", getClass()); Map<String, PlaceholderConfigurerSupport> beans = applicationContext .getBeansOfType(PlaceholderConfigurerSupport.class); assertFalse("No PropertyPlaceholderConfigurer found", beans.isEmpty()); assertEquals("${bar}", applicationContext.getBean("string")); assertEquals("null", applicationContext.getBean("nullString")); }
public String resolvePlaceholders(String str, boolean asPropName) { if (asPropName) { String strResolving = PlaceholderConfigurerSupport.DEFAULT_PLACEHOLDER_PREFIX + str + PlaceholderConfigurerSupport.DEFAULT_PLACEHOLDER_SUFFIX, strResolved = this.beanFactory.resolveEmbeddedValue(strResolving); if (!strResolved.equals(strResolving)) { str = strResolved; } } else { str = this.beanFactory.resolveEmbeddedValue(str); } return Objects.toString(this.beanExprResolver.evaluate(str, this.beanExprContext), null); }
@Test public void propertyPlaceholder() throws Exception { ApplicationContext applicationContext = new ClassPathXmlApplicationContext( "contextNamespaceHandlerTests-replace.xml", getClass()); Map<String, PlaceholderConfigurerSupport> beans = applicationContext .getBeansOfType(PlaceholderConfigurerSupport.class); assertFalse("No PropertyPlaceholderConfigurer found", beans.isEmpty()); String s = (String) applicationContext.getBean("string"); assertEquals("No properties replaced", "bar", s); }
@Test public void propertyPlaceholderEnvironmentProperties() throws Exception { MockEnvironment env = new MockEnvironment().withProperty("foo", "spam"); GenericXmlApplicationContext applicationContext = new GenericXmlApplicationContext(); applicationContext.setEnvironment(env); applicationContext.load(new ClassPathResource("contextNamespaceHandlerTests-simple.xml", getClass())); applicationContext.refresh(); Map<String, PlaceholderConfigurerSupport> beans = applicationContext .getBeansOfType(PlaceholderConfigurerSupport.class); assertFalse("No PropertyPlaceholderConfigurer found", beans.isEmpty()); String s = (String) applicationContext.getBean("string"); assertEquals("No properties replaced", "spam", s); }
@Test public void propertyPlaceholderIgnored() throws Exception { ApplicationContext applicationContext = new ClassPathXmlApplicationContext( "contextNamespaceHandlerTests-replace-ignore.xml", getClass()); Map<String, PlaceholderConfigurerSupport> beans = applicationContext .getBeansOfType(PlaceholderConfigurerSupport.class); assertFalse("No PropertyPlaceholderConfigurer found", beans.isEmpty()); String s = (String) applicationContext.getBean("string"); assertEquals("Properties replaced", "${bar}", s); }
/** * Creates a bean factory. * * @param parentContext Parent application context, if any. When specified, any placeholder * configurers found in the parent context will be registered in this bean factory. * @param parentBeanFactory The parent bean factory, if any. */ public FrameworkBeanFactory(ApplicationContext parentContext, BeanFactory parentBeanFactory) { super(parentBeanFactory); int i = 0; if (parentContext != null) { for (PlaceholderConfigurerSupport configurer : parentContext .getBeansOfType(PlaceholderConfigurerSupport.class, false, false).values()) { registerSingleton("_placeholderconfigurer" + ++i, configurer); } } }
/** * Default constructor */ public SpringPropertyReplacer() { phHelper = new PropertyPlaceholderHelper( PlaceholderConfigurerSupport.DEFAULT_PLACEHOLDER_PREFIX, PlaceholderConfigurerSupport.DEFAULT_PLACEHOLDER_SUFFIX, PlaceholderConfigurerSupport.DEFAULT_VALUE_SEPARATOR, true); }
@Override public boolean matches( final ConditionContext context, final AnnotatedTypeMetadata metadata ) { return context.getBeanFactory().getBeansOfType(PlaceholderConfigurerSupport.class).isEmpty(); }
@Bean public static PlaceholderConfigurerSupport placeholderConfigurer() { return new ConfigurationPropertySourcesPlaceholderConfigurer(StaticConfigurationSupplier.getConfiguration()); }
protected void initializeHelper() { helper = new PropertyPlaceholderHelper(PlaceholderConfigurerSupport.DEFAULT_PLACEHOLDER_PREFIX, PlaceholderConfigurerSupport.DEFAULT_PLACEHOLDER_SUFFIX, PlaceholderConfigurerSupport.DEFAULT_VALUE_SEPARATOR, true); }
@Bean public static PlaceholderConfigurerSupport propertyPlaceholderConfigurer() { return new PropertySourcesPlaceholderConfigurer(); }