@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); }
@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; }
@Bean public static PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() { // Typically externalize this as a properties file Properties properties = new Properties(); properties.setProperty("permitAllPattern", "/public/**"); PropertyPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertyPlaceholderConfigurer(); propertyPlaceholderConfigurer.setProperties(properties); return propertyPlaceholderConfigurer; } }
@Bean public static PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() { // Typically externalize this as a properties file Properties properties = new Properties(); properties.setProperty("permitAllPattern", "/public/**"); PropertyPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertyPlaceholderConfigurer(); propertyPlaceholderConfigurer.setProperties(properties); return propertyPlaceholderConfigurer; } }
@Test public void testPropertyPlaceholderConfigurerWithCircularReference() { factory.registerBeanDefinition("tb", genericBeanDefinition(TestBean.class) .addPropertyValue("age", "${age}") .addPropertyValue("name", "name${var}") .getBeanDefinition()); PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); Properties props = new Properties(); props.setProperty("age", "99"); props.setProperty("var", "${m}"); props.setProperty("m", "${var}"); ppc.setProperties(props); try { ppc.postProcessBeanFactory(factory); fail("Should have thrown BeanDefinitionStoreException"); } catch (BeanDefinitionStoreException ex) { // expected } }
@Test public void testPropertyPlaceholderConfigurerWithNestedPlaceholderInKey() { factory.registerBeanDefinition("tb", genericBeanDefinition(TestBean.class) .addPropertyValue("name", "${my${key}key}").getBeanDefinition()); PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); Properties props = new Properties(); props.put("key", "new"); props.put("mynewkey", "myname"); ppc.setProperties(props); ppc.postProcessBeanFactory(factory); TestBean tb = (TestBean) factory.getBean("tb"); assertEquals("myname", tb.getName()); }
@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()); }
@Test public void testPropertyPlaceholderConfigurerWithDefaultProperties() { factory.registerBeanDefinition("tb", genericBeanDefinition(TestBean.class) .addPropertyValue("touchy", "${test}").getBeanDefinition()); PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); Properties props = new Properties(); props.put("test", "mytest"); ppc.setProperties(props); ppc.postProcessBeanFactory(factory); TestBean tb = (TestBean) factory.getBean("tb"); assertEquals("mytest", tb.getTouchy()); }
@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()); }
@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()); }
@Test public void testPropertyPlaceholderConfigurerWithPlaceholderInAlias() { factory.registerBeanDefinition("tb", genericBeanDefinition(TestBean.class).getBeanDefinition()); factory.registerAlias("tb", "${alias}"); PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); Properties props = new Properties(); props.put("alias", "tb2"); ppc.setProperties(props); ppc.postProcessBeanFactory(factory); TestBean tb = (TestBean) factory.getBean("tb"); TestBean tb2 = (TestBean) factory.getBean("tb2"); assertSame(tb, tb2); }
@Test @SuppressWarnings("resource") public void formatFieldForAnnotationWithPlaceholdersAndFactoryBean() throws Exception { GenericApplicationContext context = new GenericApplicationContext(); PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); Properties props = new Properties(); props.setProperty("dateStyle", "S-"); props.setProperty("datePattern", "M-d-yy"); ppc.setProperties(props); context.registerBeanDefinition("formattingService", new RootBeanDefinition(FormattingConversionServiceFactoryBean.class)); context.getBeanFactory().registerSingleton("ppc", ppc); context.refresh(); formattingService = context.getBean("formattingService", FormattingConversionService.class); doTestFormatFieldForAnnotation(ModelWithPlaceholders.class, false); }
@Test public void testPropertyPlaceholderConfigurerWithSelfReferencingPlaceholderInAlias() { factory.registerBeanDefinition("tb", genericBeanDefinition(TestBean.class).getBeanDefinition()); factory.registerAlias("tb", "${alias}"); PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); Properties props = new Properties(); props.put("alias", "tb"); ppc.setProperties(props); ppc.postProcessBeanFactory(factory); TestBean tb = (TestBean) factory.getBean("tb"); assertNotNull(tb); assertEquals(0, factory.getAliases("tb").length); }
@Test @SuppressWarnings("resource") public void formatFieldForAnnotationWithPlaceholders() throws Exception { GenericApplicationContext context = new GenericApplicationContext(); PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); Properties props = new Properties(); props.setProperty("dateStyle", "S-"); props.setProperty("datePattern", "M-d-yy"); ppc.setProperties(props); context.getBeanFactory().registerSingleton("ppc", ppc); context.refresh(); context.getBeanFactory().initializeBean(formattingService, "formattingService"); formattingService.addFormatterForFieldAnnotation(new JodaDateTimeFormatAnnotationFormatterFactory()); doTestFormatFieldForAnnotation(ModelWithPlaceholders.class, false); }
ppc2.setProperties(ppc2Properties);
@Test public void testPropertyPlaceholderConfigurerWithAliases() { factory.registerBeanDefinition("tb", genericBeanDefinition(TestBean.class) .addPropertyValue("touchy", "${test}").getBeanDefinition()); factory.registerAlias("tb", "${myAlias}"); factory.registerAlias("${myTarget}", "alias2"); PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); Properties props = new Properties(); props.put("test", "mytest"); props.put("myAlias", "alias"); props.put("myTarget", "tb"); ppc.setProperties(props); ppc.postProcessBeanFactory(factory); TestBean tb = (TestBean) factory.getBean("tb"); assertEquals("mytest", tb.getTouchy()); tb = (TestBean) factory.getBean("alias"); assertEquals("mytest", tb.getTouchy()); tb = (TestBean) factory.getBean("alias2"); assertEquals("mytest", tb.getTouchy()); }
props.setProperty("key4", "mykey4"); props.setProperty("parent", "parent1"); ppc.setProperties(props); ppc.postProcessBeanFactory(factory);
Properties props = new Properties(); props.setProperty("tb", "testBean4"); ppc.setProperties(props); ppc.postProcessBeanFactory(bf);
Properties props = new Properties(); props.setProperty("tb", "testBean3"); ppc.setProperties(props); ppc.postProcessBeanFactory(bf);
Properties props = new Properties(); props.setProperty("tb", "testBean3"); ppc.setProperties(props); ppc.postProcessBeanFactory(bf);