/** * Return a potentially adapted variant of this {@link ResourcePropertySource}, * overriding the previously given name (if any) with the original resource name * (equivalent to the name generated by the name-less constructor variants). * @since 4.1 */ public ResourcePropertySource withResourceName() { if (this.resourceName == null) { return this; } return new ResourcePropertySource(this.resourceName, null, this.source); }
@Override public PropertySource<?> createPropertySource(@Nullable String name, EncodedResource resource) throws IOException { return (name != null ? new ResourcePropertySource(name, resource) : new ResourcePropertySource(resource)); }
/** * Return a potentially adapted variant of this {@link ResourcePropertySource}, * overriding the previously given name (if any) with the original resource name * (equivalent to the name generated by the name-less constructor variants). * @since 4.1 */ public ResourcePropertySource withResourceName() { if (this.resourceName == null) { return this; } return new ResourcePropertySource(this.resourceName, null, this.source); }
/** * Return a potentially adapted variant of this {@link ResourcePropertySource}, * overriding the previously given (or derived) name with the specified name. * @since 4.0.4 */ public ResourcePropertySource withName(String name) { if (this.name.equals(name)) { return this; } // Store the original resource name if necessary... if (this.resourceName != null) { if (this.resourceName.equals(name)) { return new ResourcePropertySource(this.resourceName, null, this.source); } else { return new ResourcePropertySource(name, this.resourceName, this.source); } } else { // Current name is resource name -> preserve it in the extra field... return new ResourcePropertySource(name, this.name, this.source); } }
@Override public PropertySource<?> createPropertySource(@Nullable String name, EncodedResource resource) throws IOException { return (name != null ? new ResourcePropertySource(name, resource) : new ResourcePropertySource(resource)); }
/** * Return a potentially adapted variant of this {@link ResourcePropertySource}, * overriding the previously given (or derived) name with the specified name. * @since 4.0.4 */ public ResourcePropertySource withName(String name) { if (this.name.equals(name)) { return this; } // Store the original resource name if necessary... if (this.resourceName != null) { if (this.resourceName.equals(name)) { return new ResourcePropertySource(this.resourceName, null, this.source); } else { return new ResourcePropertySource(name, this.resourceName, this.source); } } else { // Current name is resource name -> preserve it in the extra field... return new ResourcePropertySource(name, this.name, this.source); } }
String resolvedLocation = environment.resolveRequiredPlaceholders(location); Resource resource = resourceLoader.getResource(resolvedLocation); environment.getPropertySources().addFirst(new ResourcePropertySource(resource));
public PropertySource getMessageSource() { if(messageSource == null) { String messagesFilename = "messages.properties"; try { messageSource = new ResourcePropertySource(messagesFilename); } catch(IOException ex) { messageSource = new PropertySource.StubPropertySource(messagesFilename); } } return messageSource; }
private void appendPropertySource(CompositePropertySource propertySource, ResourceLoader resourceLoader) throws IOException { ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver(resourceLoader); Resource[] resources = resourcePatternResolver.getResources(RESOURCE_LOCATION_PATTERN); for (Resource resource : resources) { // Add if exists if (resource.exists()) { String internalName = String.valueOf(resource.getURL()); propertySource.addPropertySource(new ResourcePropertySource(internalName, new EncodedResource(resource, FILE_ENCODING))); } } }
@Test public void withLocationAndGeneratedNameAndExplicitClassLoader() throws IOException { PropertySource<?> ps = new ResourcePropertySource(PROPERTIES_LOCATION, getClass().getClassLoader()); assertEquals("bar", ps.getProperty("foo")); assertThat(ps.getName(), is(PROPERTIES_RESOURCE_DESCRIPTION)); }
@Test public void withLocationAndExplicitNameAndExplicitClassLoader() throws IOException { PropertySource<?> ps = new ResourcePropertySource("ps1", PROPERTIES_LOCATION, getClass().getClassLoader()); assertEquals("bar", ps.getProperty("foo")); assertThat(ps.getName(), is("ps1")); }
@Test public void withResourceAndGeneratedName() throws IOException { PropertySource<?> ps = new ResourcePropertySource(new ClassPathResource(PROPERTIES_PATH)); assertEquals("bar", ps.getProperty("foo")); assertThat(ps.getName(), is(PROPERTIES_RESOURCE_DESCRIPTION)); }
@Test public void withResourceAndExplicitName() throws IOException { PropertySource<?> ps = new ResourcePropertySource("ps1", new ClassPathResource(PROPERTIES_PATH)); assertEquals("bar", ps.getProperty("foo")); assertThat(ps.getName(), is("ps1")); }
for (String propertyFile : appPropertiesFiles) { try { sources.addLast(new ResourcePropertySource(propertyFile, AbstractServerApplication.class .getClassLoader())); } catch (IOException ioException) {
@Test public void withLocationAndGeneratedName() throws IOException { PropertySource<?> ps = new ResourcePropertySource(PROPERTIES_LOCATION); assertEquals("bar", ps.getProperty("foo")); assertThat(ps.getName(), is(PROPERTIES_RESOURCE_DESCRIPTION)); }
continue; PropertySource<?> props = new ResourcePropertySource(resource);
@Test public void withLocationAndExplicitName() throws IOException { PropertySource<?> ps = new ResourcePropertySource("ps1", PROPERTIES_LOCATION); assertEquals("bar", ps.getProperty("foo")); assertThat(ps.getName(), is("ps1")); }
@Test public void xmlWithLocationAndGeneratedName() throws IOException { PropertySource<?> ps = new ResourcePropertySource(XML_PROPERTIES_LOCATION); assertEquals("bar", ps.getProperty("foo")); assertThat(ps.getName(), is(XML_PROPERTIES_RESOURCE_DESCRIPTION)); }
@Test public void withResourceHavingNoDescription() throws IOException { PropertySource<?> ps = new ResourcePropertySource(new ByteArrayResource("foo=bar".getBytes(), "")); assertEquals("bar", ps.getProperty("foo")); assertEquals("Byte array resource []", ps.getName()); }
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"your_config.xml"}, false); // all sorts of constructors, many options for finding the resource ResourcePropertySource properties = new ResourcePropertySource("path/to/my.properties"); context.getEnvironment().getPropertySources().addLast(properties); context.refresh();