@Test public void testPropertyPlaceholderConfigurerWithSystemPropertyNotUsed() { factory.registerBeanDefinition("tb", genericBeanDefinition(TestBean.class) .addPropertyValue("country", "${os.name}").getBeanDefinition()); PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); Properties props = new Properties(); props.setProperty("os.name", "myos"); ppc.setProperties(props); ppc.postProcessBeanFactory(factory); TestBean tb = (TestBean) factory.getBean("tb"); assertEquals("myos", tb.getCountry()); }
@Override @Nullable public String resolvePlaceholder(String placeholderName) { return PropertyPlaceholderConfigurer.this.resolvePlaceholder(placeholderName, this.props, systemPropertiesMode); } }
@Override protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props) throws BeansException { super.processProperties(beanFactory, props); properties = props; }
/** * Resolve the given placeholder using the given properties, performing * a system properties check according to the given mode. * <p>The default implementation delegates to {@code resolvePlaceholder * (placeholder, props)} before/after the system properties check. * <p>Subclasses can override this for custom resolution strategies, * including customized points for the system properties check. * @param placeholder the placeholder to resolve * @param props the merged properties of this configurer * @param systemPropertiesMode the system properties mode, * according to the constants in this class * @return the resolved value, of null if none * @see #setSystemPropertiesMode * @see System#getProperty * @see #resolvePlaceholder(String, java.util.Properties) */ @Nullable protected String resolvePlaceholder(String placeholder, Properties props, int systemPropertiesMode) { String propVal = null; if (systemPropertiesMode == SYSTEM_PROPERTIES_MODE_OVERRIDE) { propVal = resolveSystemProperty(placeholder); } if (propVal == null) { propVal = resolvePlaceholder(placeholder, props); } if (propVal == null && systemPropertiesMode == SYSTEM_PROPERTIES_MODE_FALLBACK) { propVal = resolveSystemProperty(placeholder); } return propVal; }
@Bean public PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() throws IOException { PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer(); Properties props = propertyFactory(); configurer.setProperties(props); configurer.setSystemPropertiesMode(PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_OVERRIDE); return configurer; }
@Test public void localPropertiesViaResource() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.registerBeanDefinition("testBean", genericBeanDefinition(TestBean.class) .addPropertyValue("name", "${my.name}") .getBeanDefinition()); PropertyPlaceholderConfigurer pc = new PropertyPlaceholderConfigurer(); Resource resource = new ClassPathResource("PropertyPlaceholderConfigurerTests.properties", this.getClass()); pc.setLocation(resource); pc.postProcessBeanFactory(bf); }
@Test public void testPropertyPlaceholderConfigurerWithOverridingSystemProperty() { factory.registerBeanDefinition("tb", genericBeanDefinition(TestBean.class) .addPropertyValue("country", "${os.name}").getBeanDefinition()); PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); Properties props = new Properties(); props.put("os.name", "myos"); ppc.setProperties(props); ppc.setSystemPropertiesMode(PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_OVERRIDE); ppc.postProcessBeanFactory(factory); TestBean tb = (TestBean) factory.getBean("tb"); assertEquals(System.getProperty("os.name"), tb.getCountry()); }
@Bean public static PropertyPlaceholderConfigurer properties(){ PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); Resource[] resources = new ClassPathResource[ ] { new ClassPathResource( "path/to/customer/*.properties" ) }; ppc.setLocations( resources ); ppc.setIgnoreUnresolvablePlaceholders( true ); return ppc; }
@Before public void setup() { p1BeanDef = rootBeanDefinition(TestBean.class) .addPropertyValue("name", "${" + P1 + "}") .getBeanDefinition(); bf = new DefaultListableBeanFactory(); ppcProperties = new Properties(); ppcProperties.setProperty(P1, P1_LOCAL_PROPS_VAL); System.setProperty(P1, P1_SYSTEM_PROPS_VAL); ppc = new PropertyPlaceholderConfigurer(); ppc.setProperties(ppcProperties); }
@Test public void testPropertyPlaceholderConfigurerWithEmptyStringInPlaceholderAsNull() { factory.registerBeanDefinition("tb", genericBeanDefinition(TestBean.class) .addPropertyValue("name", "${ref}").getBeanDefinition()); PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); ppc.setNullValue(""); Properties props = new Properties(); props.put("ref", ""); ppc.setProperties(props); ppc.postProcessBeanFactory(factory); TestBean tb = (TestBean) factory.getBean("tb"); assertNull(tb.getName()); }
private void updateRmiPort(final AbstractApplicationContext applicationContext, final int rmiPort) { final PropertyPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertyPlaceholderConfigurer(); Properties properties = new Properties(); properties.put("rmiPort", Integer.toString(rmiPort)); propertyPlaceholderConfigurer.setProperties(properties); propertyPlaceholderConfigurer.setIgnoreUnresolvablePlaceholders(true); applicationContext.addBeanFactoryPostProcessor(propertyPlaceholderConfigurer); }
/** * Gets the property placeholder configurer. * * @return the property placeholder configurer */ @Bean(name = "propertyPlaceholderConfigurer") public static PropertyPlaceholderConfigurer getPropertyPlaceholderConfigurer() { logger.debug("Instantiated propertyPlaceholderConfigurer"); PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer(); configurer.setLocation(new ClassPathResource("stats.properties")); configurer.setNullValue("NULL"); Properties properties = new Properties(); properties.put("psiprobe.tools.mail.to", "NULL"); properties.put("psiprobe.tools.mail.subjectPrefix", "[PSI Probe]"); configurer.setProperties(properties); configurer.setSystemPropertiesModeName("SYSTEM_PROPERTIES_MODE_OVERRIDE"); return configurer; }
@Bean @Autowired public PropertyPlaceholderConfigurer properties(Properties externalProperties) { PropertyPlaceholderConfigurer propertiesBean = new PropertyPlaceholderConfigurer(); propertiesBean.setIgnoreUnresolvablePlaceholders(true); propertiesBean.setSystemPropertiesModeName("SYSTEM_PROPERTIES_MODE_OVERRIDE"); propertiesBean.setOrder("10"); propertiesBean.setProperties(externalProperties); return propertiesBean; }
@Test public void testPropertyPlaceholderConfigurerWithIgnoreUnresolvablePlaceholder() { factory.registerBeanDefinition("tb", genericBeanDefinition(TestBean.class) .addPropertyValue("name", "${ref}").getBeanDefinition()); PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); ppc.setIgnoreUnresolvablePlaceholders(true); ppc.postProcessBeanFactory(factory); TestBean tb = (TestBean) factory.getBean("tb"); assertEquals("${ref}", tb.getName()); }
@Test public void testPropertyPlaceholderConfigurerWithSystemPropertyFallback() { factory.registerBeanDefinition("tb", genericBeanDefinition(TestBean.class) .addPropertyValue("country", "${os.name}").getBeanDefinition()); PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); ppc.postProcessBeanFactory(factory); TestBean tb = (TestBean) factory.getBean("tb"); assertEquals(System.getProperty("os.name"), tb.getCountry()); }
@Test public void testPropertyPlaceholderConfigurerWithUnresolvableSystemProperty() { factory.registerBeanDefinition("tb", genericBeanDefinition(TestBean.class) .addPropertyValue("touchy", "${user.dir}").getBeanDefinition()); PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); ppc.setSystemPropertiesMode(PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_NEVER); try { ppc.postProcessBeanFactory(factory); fail("Should have thrown BeanDefinitionStoreException"); } catch (BeanDefinitionStoreException ex) { // expected assertTrue(ex.getMessage().contains("user.dir")); } }
@Configuration public class PropertiesConfiguration { @Bean public static PropertyPlaceholderConfigurer propertyPlaceholderConfigurer( @Value("#{servletContext.contextPath}") String contextPath) throws MalformedURLException { final PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer(); configurer.setLocation(new UrlResource("file:" + System.getProperty("user.home") + "/" + contextPath + ".properties")); return configurer; } }
@Test public void testPropertyPlaceholderConfigurerWithEmptyStringAsNull() { factory.registerBeanDefinition("tb", genericBeanDefinition(TestBean.class) .addPropertyValue("name", "").getBeanDefinition()); PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); ppc.setNullValue(""); ppc.postProcessBeanFactory(factory); TestBean tb = (TestBean) factory.getBean("tb"); assertNull(tb.getName()); }
private AbstractApplicationContext readContextFromFile(final String filePath) { final ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[] { filePath }, false); final PropertyPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertyPlaceholderConfigurer(); propertyPlaceholderConfigurer.setSystemPropertiesMode(PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_OVERRIDE); propertyPlaceholderConfigurer.setIgnoreResourceNotFound(true); propertyPlaceholderConfigurer.setLocations(PROPERTIES_LOCATIONS); applicationContext.addBeanFactoryPostProcessor(propertyPlaceholderConfigurer); applicationContext.refresh(); return applicationContext; }
@Test public void trimValuesIsApplied() { PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); ppc.setTrimValues(true); System.setProperty("my.name", " myValue "); DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.registerBeanDefinition("testBean", rootBeanDefinition(TestBean.class) .addPropertyValue("name", "${my.name}") .getBeanDefinition()); ppc.postProcessBeanFactory(bf); assertThat(bf.getBean(TestBean.class).getName(), equalTo("myValue")); System.clearProperty("my.name"); }