@Test public void ignoreUnresolvablePlaceholders_true() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.registerBeanDefinition("testBean", genericBeanDefinition(TestBean.class) .addPropertyValue("name", "${my.name}") .getBeanDefinition()); PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer(); ppc.setIgnoreUnresolvablePlaceholders(true); ppc.postProcessBeanFactory(bf); assertThat(bf.getBean(TestBean.class).getName(), equalTo("${my.name}")); }
@Test public void multipleLocationsWithDefaultResolvedValue() throws Exception { // SPR-10619 PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer(); ClassPathResource doesNotHave = new ClassPathResource("test.properties", getClass()); ClassPathResource setToTrue = new ClassPathResource("placeholder.properties", getClass()); ppc.setLocations(doesNotHave, setToTrue); ppc.setIgnoreResourceNotFound(true); ppc.setIgnoreUnresolvablePlaceholders(true); DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.registerBeanDefinition("testBean", genericBeanDefinition(TestBean.class) .addPropertyValue("jedi", "${jedi:false}") .getBeanDefinition()); ppc.postProcessBeanFactory(bf); assertThat(bf.getBean(TestBean.class).isJedi(), equalTo(true)); }
@Test @SuppressWarnings("serial") public void ignoredNestedUnresolvablePlaceholder() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.registerBeanDefinition("testBean", genericBeanDefinition(TestBean.class) .addPropertyValue("name", "${my.name}") .getBeanDefinition()); PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer(); ppc.setProperties(new Properties() {{ put("my.name", "${bogus}"); }}); ppc.setIgnoreUnresolvablePlaceholders(true); ppc.postProcessBeanFactory(bf); assertThat(bf.getBean(TestBean.class).getName(), equalTo("${bogus}")); }
@Test public void optionalPropertyWithValue() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.setConversionService(new DefaultConversionService()); bf.registerBeanDefinition("testBean", genericBeanDefinition(OptionalTestBean.class) .addPropertyValue("name", "${my.name}") .getBeanDefinition()); MockEnvironment env = new MockEnvironment(); env.setProperty("my.name", "myValue"); PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer(); ppc.setEnvironment(env); ppc.setIgnoreUnresolvablePlaceholders(true); ppc.postProcessBeanFactory(bf); assertThat(bf.getBean(OptionalTestBean.class).getName(), equalTo(Optional.of("myValue"))); }
@Test public void optionalPropertyWithoutValue() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.setConversionService(new DefaultConversionService()); bf.registerBeanDefinition("testBean", genericBeanDefinition(OptionalTestBean.class) .addPropertyValue("name", "${my.name}") .getBeanDefinition()); MockEnvironment env = new MockEnvironment(); env.setProperty("my.name", ""); PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer(); ppc.setEnvironment(env); ppc.setIgnoreUnresolvablePlaceholders(true); ppc.setNullValue(""); ppc.postProcessBeanFactory(bf); assertThat(bf.getBean(OptionalTestBean.class).getName(), equalTo(Optional.empty())); }
@Test public void explicitPropertySourcesExcludesEnvironment() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.registerBeanDefinition("testBean", genericBeanDefinition(TestBean.class) .addPropertyValue("name", "${my.name}") .getBeanDefinition()); MutablePropertySources propertySources = new MutablePropertySources(); propertySources.addLast(new MockPropertySource()); PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer(); ppc.setPropertySources(propertySources); ppc.setEnvironment(new MockEnvironment().withProperty("my.name", "env")); ppc.setIgnoreUnresolvablePlaceholders(true); ppc.postProcessBeanFactory(bf); assertThat(bf.getBean(TestBean.class).getName(), equalTo("${my.name}")); assertEquals(ppc.getAppliedPropertySources().iterator().next(), propertySources.iterator().next()); }
@Test @SuppressWarnings("serial") public void explicitPropertySourcesExcludesLocalProperties() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.registerBeanDefinition("testBean", genericBeanDefinition(TestBean.class) .addPropertyValue("name", "${my.name}") .getBeanDefinition()); MutablePropertySources propertySources = new MutablePropertySources(); propertySources.addLast(new MockPropertySource()); PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer(); ppc.setPropertySources(propertySources); ppc.setProperties(new Properties() {{ put("my.name", "local"); }}); ppc.setIgnoreUnresolvablePlaceholders(true); ppc.postProcessBeanFactory(bf); assertThat(bf.getBean(TestBean.class).getName(), equalTo("${my.name}")); }
AbstractApplicationContext createApplicationContext(Api api) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.setParent(gatewayApplicationContext); context.setClassLoader(new ReactorHandlerClassLoader(gatewayApplicationContext.getClassLoader())); context.setEnvironment((ConfigurableEnvironment) gatewayApplicationContext.getEnvironment()); PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer(); configurer.setIgnoreUnresolvablePlaceholders(true); configurer.setEnvironment(gatewayApplicationContext.getEnvironment()); context.addBeanFactoryPostProcessor(configurer); context.getBeanFactory().registerSingleton("api", api); context.register(ApiHandlerConfiguration.class); context.setId("context-api-" + api.getId()); context.refresh(); return context; }
@Bean public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = PropertyLoadHelper.getPropertySourcesPlaceholderConfigurer(); propertySourcesPlaceholderConfigurer.setIgnoreUnresolvablePlaceholders(true); return propertySourcesPlaceholderConfigurer; }
@Bean public static PropertySourcesPlaceholderConfigurer propertyConfigurer() { PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer(); pspc.setIgnoreUnresolvablePlaceholders(true); return pspc; } }
@Configuration @PropertySource(value = "spring/test5.properties") class Config { @Bean PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { PropertySourcesPlaceholderConfigurer c = new PropertySourcesPlaceholderConfigurer(); c.setIgnoreUnresolvablePlaceholders(true); return c; } ...
@Bean public PropertySourcesPlaceholderConfigurer ppc() { PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer(); ppc.setIgnoreUnresolvablePlaceholders(true); return ppc; }
@Bean public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() { PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer(); // this to avoid errors when properties are not resolvable // beans should use default value syntax to verify that the property exists // @Value("${some.key:#{null}}") configurer.setIgnoreUnresolvablePlaceholders(true); return configurer; }
@Bean public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer(); List<Resource> resources = new LinkedList<Resource>(); resources.add(new ClassPathResource("config.properties")); resources.add(new ClassPathResource("config2.properties")); configurer.setLocations(resources.toArray(new Resource[0])); configurer.setIgnoreUnresolvablePlaceholders(true); return configurer; }
@Bean public static PropertySourcesPlaceholderConfigurer properties(@Qualifier("graviteeProperties") Properties graviteeProperties) { PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer(); propertySourcesPlaceholderConfigurer.setProperties(graviteeProperties); propertySourcesPlaceholderConfigurer.setIgnoreUnresolvablePlaceholders(true); return propertySourcesPlaceholderConfigurer; }
@Bean public static PropertySourcesPlaceholderConfigurer properties(@Qualifier("graviteeProperties") Properties graviteeProperties) { PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer(); propertySourcesPlaceholderConfigurer.setProperties(graviteeProperties); propertySourcesPlaceholderConfigurer.setIgnoreUnresolvablePlaceholders(true); return propertySourcesPlaceholderConfigurer; }
@Bean public static PropertySourcesPlaceholderConfigurer properties(@Qualifier("graviteeProperties") Properties graviteeProperties) { PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer(); propertySourcesPlaceholderConfigurer.setProperties(graviteeProperties); propertySourcesPlaceholderConfigurer.setIgnoreUnresolvablePlaceholders(true); return propertySourcesPlaceholderConfigurer; }
public static PropertySourcesPlaceholderConfigurer loadProperties(){ PropertySourcesPlaceholderConfigurer propertySPC = new PropertySourcesPlaceholderConfigurer(); Resource[] resources = new ClassPathResource[ ] { new ClassPathResource( "yourfilename.properties" ) }; propertySPC .setLocations( resources ); propertySPC .setIgnoreUnresolvablePlaceholders( true ); return propertySPC ; }
@Bean public static PropertySourcesPlaceholderConfigurer properties() { final PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer(); Resource[] resources = new Resource[ ] { new ClassPathResource( "default.properties" ), new FileSystemResource("production.properties") }; pspc.setLocations( resources ); pspc.setIgnoreResourceNotFound(true); pspc.setIgnoreUnresolvablePlaceholders(false); return pspc; }
@Bean public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer(ConfigurableApplicationContext ctx, ConfigurableEnvironment env, ConfigurableConversionService conversionService) { env.setConversionService(conversionService); // have to manually set this now, doesn't pick up the bean defined in MangoRuntimeContextConfiguration ctx.getBeanFactory().setConversionService(conversionService); PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer(); configurer.setIgnoreUnresolvablePlaceholders(false); return configurer; }