@Test public void explicitPropertySources() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.registerBeanDefinition("testBean", genericBeanDefinition(TestBean.class) .addPropertyValue("name", "${my.name}") .getBeanDefinition()); MutablePropertySources propertySources = new MutablePropertySources(); propertySources.addLast(new MockPropertySource().withProperty("my.name", "foo")); PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer(); ppc.setPropertySources(propertySources); ppc.postProcessBeanFactory(bf); assertThat(bf.getBean(TestBean.class).getName(), equalTo("foo")); assertEquals(ppc.getAppliedPropertySources().iterator().next(), propertySources.iterator().next()); }
@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}")); }
@Bean public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer(); propertySourcesPlaceholderConfigurer.setPropertySources(new MyPropertySources()); return propertySourcesPlaceholderConfigurer; }
@Bean public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() throws IOException { String profile = System.getProperty("spring.profiles.active"); PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer(); Resource resource = new ClassPathResource(String.format("application-%s.properties", profile)); Properties props = PropertiesLoaderUtils.loadProperties(resource); pspc.setProperties(props); pspc.setPropertySources(); return pspc; }
@Autowired PropertySourcesPlaceholderConfigurer property; @Autowired OctopusPropertiesRepository repo; @PostConstruct public void onInit() { Map<String,Object> props = new ConcurrentHashMap<>(); List<OctopusProperties> loadedSettings = repo.findAll(); loadedSettings.forEach(entry -> props.put(entry.getKey(), entry.getValue())); MutablePropertySources mutablePropertySources = new MutablePropertySources(); mutablePropertySources.addFirst(new MapPropertySource("custom", props)); property.setPropertySources(mutablePropertySources); }
@Configuration public class Appconfig { @Autowired private OctopusPropertiesRepository repo; @Bean public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { PropertySourcesPlaceholderConfigurer property = new PropertySourcesPlaceholderConfigurer(); Map<String,Object> props = new ConcurrentHashMap<>(); List<OctopusProperties> loadedSettings = repo.findAll(); loadedSettings.forEach(entry -> props.put(entry.getKey(), entry.getValue())); MutablePropertySources mutablePropertySources = new MutablePropertySources(); mutablePropertySources.addFirst(new MapPropertySource("custom", props)); property.setPropertySources(mutablePropertySources); return property; } }
MutablePropertySources mutablePropertySources = new MutablePropertySources(); mutablePropertySources.addFirst(new MapPropertySource("custom", props)); property.setPropertySources(mutablePropertySources); return property;