/** * Return a {@link PropertySource} of this instance. * @return a {@link PropertySource} */ public PropertySource<?> toPropertySource() { return new PropertiesPropertySource(getClass().getSimpleName(), copy(this.entries)); }
@Override public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { MutablePropertySources sources = environment.getPropertySources(); Properties properties = new Properties(); Integer configuredPort = environment.getProperty("grpc.port", Integer.class); properties.put("grpc.port", 0); if (null == configuredPort) { properties.put(LocalRunningGrpcPort.propertyName, DEFAULT_GRPC_PORT); } else if (0 == configuredPort) { properties.put(LocalRunningGrpcPort.propertyName, SocketUtils.findAvailableTcpPort()); } else { properties.put("grpc.port", configuredPort); properties.put(LocalRunningGrpcPort.propertyName, configuredPort); } sources.addLast(new PropertiesPropertySource("grpc", properties)); } }
@Override public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { SpringApplication springApplication = event.getSpringApplication(); Class clazz = springApplication.getMainApplicationClass(); if (!clazz.isAnnotationPresent(ImportExtraConfig.class)) { return; } ImportExtraConfig annotation = (ImportExtraConfig) clazz .getAnnotation(ImportExtraConfig.class); String[] extraConfig = annotation.name(); if (extraConfig == null || extraConfig.length == 0) { return; } for (String config : extraConfig) { try { Properties properties = new Properties(); properties.load(new FileInputStream(config)); event.getEnvironment().getPropertySources() .addFirst(new PropertiesPropertySource(config, properties)); } catch (IOException e) { e.printStackTrace(); } } } }
/** * Use Spring 3.1. environment support to set properties for the * customer-specific application context. * * @param ctx * @param customer */ private void setEnvironmentForCustomer(ConfigurableApplicationContext ctx, String customer) { StandardEnvironment env = new StandardEnvironment(); Properties props = new Properties(); // populate properties for customer props.setProperty("host", "host.for." + customer); props.setProperty("user", "user"); props.setProperty("password", "password"); props.setProperty("remote.directory", "/tmp"); PropertiesPropertySource pps = new PropertiesPropertySource("ftpprops", props); env.getPropertySources().addLast(pps); ctx.setEnvironment(env); } }
@Override protected ApplicationContext initApplicationContext() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.register(WebConfig.class); Properties props = new Properties(); props.setProperty("myOrigin", "http://site1.com"); context.getEnvironment().getPropertySources().addFirst(new PropertiesPropertySource("ps", props)); context.register(PropertySourcesPlaceholderConfigurer.class); context.refresh(); return context; }
@Before public void setUp() { propertySources = new MutablePropertySources(); propertyResolver = new PropertySourcesPropertyResolver(propertySources); testProperties = new Properties(); propertySources.addFirst(new PropertiesPropertySource("testProperties", testProperties)); }
@Override public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { if (CloudPlatform.CLOUD_FOUNDRY.isActive(environment)) { Properties properties = new Properties(); JsonParser jsonParser = JsonParserFactory.getJsonParser(); addWithPrefix(properties, getPropertiesFromApplication(environment, jsonParser), "vcap.application."); addWithPrefix(properties, getPropertiesFromServices(environment, jsonParser), "vcap.services."); MutablePropertySources propertySources = environment.getPropertySources(); if (propertySources.contains( CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME)) { propertySources.addAfter( CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME, new PropertiesPropertySource("vcap", properties)); } else { propertySources .addFirst(new PropertiesPropertySource("vcap", properties)); } } }
@Before @SuppressWarnings("resource") public void setup() { StaticWebApplicationContext wac = new StaticWebApplicationContext(); Properties props = new Properties(); props.setProperty("myOrigin", "http://example.com"); wac.getEnvironment().getPropertySources().addFirst(new PropertiesPropertySource("ps", props)); wac.registerSingleton("ppc", PropertySourcesPlaceholderConfigurer.class); wac.refresh(); this.handlerMapping.setRemoveSemicolonContent(false); wac.getAutowireCapableBeanFactory().initializeBean(this.handlerMapping, "hm"); this.request.setMethod("GET"); this.request.addHeader(HttpHeaders.ORIGIN, "http://domain.com/"); }
@Test @SuppressWarnings("serial") public void equals() { Map<String, Object> map1 = new HashMap<String, Object>() {{ put("a", "b"); }}; Map<String, Object> map2 = new HashMap<String, Object>() {{ put("c", "d"); }}; Properties props1 = new Properties() {{ setProperty("a", "b"); }}; Properties props2 = new Properties() {{ setProperty("c", "d"); }}; MapPropertySource mps = new MapPropertySource("mps", map1); assertThat(mps, equalTo(mps)); assertThat(new MapPropertySource("x", map1).equals(new MapPropertySource("x", map1)), is(true)); assertThat(new MapPropertySource("x", map1).equals(new MapPropertySource("x", map2)), is(true)); assertThat(new MapPropertySource("x", map1).equals(new PropertiesPropertySource("x", props1)), is(true)); assertThat(new MapPropertySource("x", map1).equals(new PropertiesPropertySource("x", props2)), is(true)); assertThat(new MapPropertySource("x", map1).equals(new Object()), is(false)); assertThat(new MapPropertySource("x", map1).equals("x"), is(false)); assertThat(new MapPropertySource("x", map1).equals(new MapPropertySource("y", map1)), is(false)); assertThat(new MapPropertySource("x", map1).equals(new MapPropertySource("y", map2)), is(false)); assertThat(new MapPropertySource("x", map1).equals(new PropertiesPropertySource("y", props1)), is(false)); assertThat(new MapPropertySource("x", map1).equals(new PropertiesPropertySource("y", props2)), is(false)); }
new PropertiesPropertySource(LOCAL_PROPERTIES_PROPERTY_SOURCE_NAME, mergeProperties()); if (this.localOverride) { this.propertySources.addFirst(localPropertySource);
new PropertiesPropertySource(LOCAL_PROPERTIES_PROPERTY_SOURCE_NAME, mergeProperties()); if (this.localOverride) { this.propertySources.addFirst(localPropertySource);
public class AppContextInitializer implements ApplicationContextInitializer<ConfigurableWebApplicationContext> { @Override public void initialize(ConfigurableWebApplicationContext applicationContext) { Properties props; try { props = PropertiesLoaderUtils.loadAllProperties("/some/path"); PropertiesPropertySource ps = new PropertiesPropertySource("profile", props); applicationContext.getEnvironment().getPropertySources().addFirst(ps); } catch (IOException e) { // handle error } } }
public void afterPropertiesSet() throws Exception { PropertiesPropertySource propertySource = new PropertiesPropertySource(name, this.properties); Environment environment = applicationContext.getEnvironment(); if (environment instanceof ConfigurableEnvironment) { ConfigurableEnvironment configurableEnvironment = (ConfigurableEnvironment) applicationContext .getEnvironment(); configurableEnvironment.getPropertySources().addLast(propertySource); } }
@Override public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { File home = getHomeFolder(); File propertyFile = (home != null) ? new File(home, FILE_NAME) : null; if (propertyFile != null && propertyFile.exists() && propertyFile.isFile()) { FileSystemResource resource = new FileSystemResource(propertyFile); Properties properties; try { properties = PropertiesLoaderUtils.loadProperties(resource); environment.getPropertySources().addFirst( new PropertiesPropertySource("devtools-local", properties)); } catch (IOException ex) { throw new IllegalStateException("Unable to load " + FILE_NAME, ex); } } }
@Override protected PropertySource<?> toSimplePropertySource() { Properties props = new Properties(); copyIfSet(props, "branch"); String commitId = getProperties().getShortCommitId(); if (commitId != null) { props.put("commit.id", commitId); } copyIfSet(props, "commit.time"); return new PropertiesPropertySource("git", props); }
@Override protected PropertySource<?> toSimplePropertySource() { Properties props = new Properties(); copyIfSet(props, "group"); copyIfSet(props, "artifact"); copyIfSet(props, "name"); copyIfSet(props, "version"); copyIfSet(props, "time"); return new PropertiesPropertySource("build", props); }
private ClassPathXmlApplicationContext createContext(UdpMulticastEndToEndTests launcher, String location) { ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(); applicationContext.setConfigLocation(location); StandardEnvironment env = new StandardEnvironment(); Properties props = new Properties(); props.setProperty("port", Integer.toString(launcher.getReceiverPort())); PropertiesPropertySource pps = new PropertiesPropertySource("ftpprops", props); env.getPropertySources().addLast(pps); applicationContext.setEnvironment(env); applicationContext.refresh(); return applicationContext; }
private ClassPathXmlApplicationContext createContext(UdpUnicastEndToEndTests launcher, String location) { ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(); applicationContext.setConfigLocation(location); StandardEnvironment env = new StandardEnvironment(); Properties props = new Properties(); props.setProperty("port", Integer.toString(launcher.getReceiverPort())); PropertiesPropertySource pps = new PropertiesPropertySource("ftpprops", props); env.getPropertySources().addLast(pps); applicationContext.setEnvironment(env); applicationContext.refresh(); return applicationContext; }
private PropertySource<?> newPropertySource(String name, Properties properties) { return new PropertiesPropertySource(name, properties); } }
private PropertySource<?> newPropertySource(String name, Properties properties) { return new PropertiesPropertySource(name, properties); } }