Search in sources :

Example 36 with Resource

use of org.springframework.core.io.Resource in project spring-framework by spring-projects.

the class ResourceEditorRegistrar method registerCustomEditors.

/**
	 * Populate the given {@code registry} with the following resource editors:
	 * ResourceEditor, InputStreamEditor, InputSourceEditor, FileEditor, URLEditor,
	 * URIEditor, ClassEditor, ClassArrayEditor.
	 * <p>If this registrar has been configured with a {@link ResourcePatternResolver},
	 * a ResourceArrayPropertyEditor will be registered as well.
	 * @see org.springframework.core.io.ResourceEditor
	 * @see org.springframework.beans.propertyeditors.InputStreamEditor
	 * @see org.springframework.beans.propertyeditors.InputSourceEditor
	 * @see org.springframework.beans.propertyeditors.FileEditor
	 * @see org.springframework.beans.propertyeditors.URLEditor
	 * @see org.springframework.beans.propertyeditors.URIEditor
	 * @see org.springframework.beans.propertyeditors.ClassEditor
	 * @see org.springframework.beans.propertyeditors.ClassArrayEditor
	 * @see org.springframework.core.io.support.ResourceArrayPropertyEditor
	 */
@Override
public void registerCustomEditors(PropertyEditorRegistry registry) {
    ResourceEditor baseEditor = new ResourceEditor(this.resourceLoader, this.propertyResolver);
    doRegisterEditor(registry, Resource.class, baseEditor);
    doRegisterEditor(registry, ContextResource.class, baseEditor);
    doRegisterEditor(registry, InputStream.class, new InputStreamEditor(baseEditor));
    doRegisterEditor(registry, InputSource.class, new InputSourceEditor(baseEditor));
    doRegisterEditor(registry, File.class, new FileEditor(baseEditor));
    doRegisterEditor(registry, Path.class, new PathEditor(baseEditor));
    doRegisterEditor(registry, Reader.class, new ReaderEditor(baseEditor));
    doRegisterEditor(registry, URL.class, new URLEditor(baseEditor));
    ClassLoader classLoader = this.resourceLoader.getClassLoader();
    doRegisterEditor(registry, URI.class, new URIEditor(classLoader));
    doRegisterEditor(registry, Class.class, new ClassEditor(classLoader));
    doRegisterEditor(registry, Class[].class, new ClassArrayEditor(classLoader));
    if (this.resourceLoader instanceof ResourcePatternResolver) {
        doRegisterEditor(registry, Resource[].class, new ResourceArrayPropertyEditor((ResourcePatternResolver) this.resourceLoader, this.propertyResolver));
    }
}
Also used : FileEditor(org.springframework.beans.propertyeditors.FileEditor) ResourcePatternResolver(org.springframework.core.io.support.ResourcePatternResolver) InputStreamEditor(org.springframework.beans.propertyeditors.InputStreamEditor) URIEditor(org.springframework.beans.propertyeditors.URIEditor) ContextResource(org.springframework.core.io.ContextResource) Resource(org.springframework.core.io.Resource) ClassArrayEditor(org.springframework.beans.propertyeditors.ClassArrayEditor) ResourceEditor(org.springframework.core.io.ResourceEditor) ClassEditor(org.springframework.beans.propertyeditors.ClassEditor) ResourceArrayPropertyEditor(org.springframework.core.io.support.ResourceArrayPropertyEditor) InputSourceEditor(org.springframework.beans.propertyeditors.InputSourceEditor) ReaderEditor(org.springframework.beans.propertyeditors.ReaderEditor) PathEditor(org.springframework.beans.propertyeditors.PathEditor) URLEditor(org.springframework.beans.propertyeditors.URLEditor)

Example 37 with Resource

use of org.springframework.core.io.Resource in project spring-framework by spring-projects.

the class PropertyPlaceholderConfigurerTests method localPropertiesViaResource.

@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);
}
Also used : TestBean(org.springframework.tests.sample.beans.TestBean) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 38 with Resource

use of org.springframework.core.io.Resource in project spring-framework by spring-projects.

the class XmlBeanDefinitionReaderTests method withImport.

@Test
public void withImport() {
    SimpleBeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry();
    Resource resource = new ClassPathResource("import.xml", getClass());
    new XmlBeanDefinitionReader(registry).loadBeanDefinitions(resource);
    testBeanDefinitions(registry);
}
Also used : SimpleBeanDefinitionRegistry(org.springframework.beans.factory.support.SimpleBeanDefinitionRegistry) ClassPathResource(org.springframework.core.io.ClassPathResource) InputStreamResource(org.springframework.core.io.InputStreamResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 39 with Resource

use of org.springframework.core.io.Resource in project spring-framework by spring-projects.

the class XmlBeanDefinitionReaderTests method withFreshInputStream.

@Test
public void withFreshInputStream() {
    SimpleBeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry();
    Resource resource = new ClassPathResource("test.xml", getClass());
    new XmlBeanDefinitionReader(registry).loadBeanDefinitions(resource);
    testBeanDefinitions(registry);
}
Also used : SimpleBeanDefinitionRegistry(org.springframework.beans.factory.support.SimpleBeanDefinitionRegistry) ClassPathResource(org.springframework.core.io.ClassPathResource) InputStreamResource(org.springframework.core.io.InputStreamResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 40 with Resource

use of org.springframework.core.io.Resource in project spring-framework by spring-projects.

the class XmlBeanDefinitionReaderTests method withOpenInputStreamAndExplicitValidationMode.

@Test
public void withOpenInputStreamAndExplicitValidationMode() {
    SimpleBeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry();
    Resource resource = new InputStreamResource(getClass().getResourceAsStream("test.xml"));
    XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(registry);
    reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_DTD);
    reader.loadBeanDefinitions(resource);
    testBeanDefinitions(registry);
}
Also used : SimpleBeanDefinitionRegistry(org.springframework.beans.factory.support.SimpleBeanDefinitionRegistry) ClassPathResource(org.springframework.core.io.ClassPathResource) InputStreamResource(org.springframework.core.io.InputStreamResource) Resource(org.springframework.core.io.Resource) InputStreamResource(org.springframework.core.io.InputStreamResource) Test(org.junit.Test)

Aggregations

Resource (org.springframework.core.io.Resource)610 Test (org.junit.Test)257 ClassPathResource (org.springframework.core.io.ClassPathResource)231 IOException (java.io.IOException)103 FileSystemResource (org.springframework.core.io.FileSystemResource)77 UrlResource (org.springframework.core.io.UrlResource)68 File (java.io.File)64 ArrayList (java.util.ArrayList)58 ByteArrayResource (org.springframework.core.io.ByteArrayResource)49 InputStream (java.io.InputStream)46 InputStreamResource (org.springframework.core.io.InputStreamResource)31 PathMatchingResourcePatternResolver (org.springframework.core.io.support.PathMatchingResourcePatternResolver)30 URL (java.net.URL)25 HashMap (java.util.HashMap)20 MockServerWebExchange (org.springframework.mock.http.server.reactive.test.MockServerWebExchange)18 ServletContextResource (org.springframework.web.context.support.ServletContextResource)18 Map (java.util.Map)17 ResourcePatternResolver (org.springframework.core.io.support.ResourcePatternResolver)17 ResourceLoader (org.springframework.core.io.ResourceLoader)16 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)16