Refine search
private static TemplateLoader mustacheTemplateLoader() { ResourceLoader resourceLoader = new DefaultResourceLoader(); String prefix = "classpath:/templates/"; Charset charset = Charset.forName("UTF-8"); return (name) -> new InputStreamReader( resourceLoader.getResource(prefix + name).getInputStream(), charset); }
/** * Create a PropertySource based on Properties loaded from the given resource * location and use the given class loader to load the resource, assuming it is * prefixed with {@code classpath:}. The name of the PropertySource will be * generated based on the {@link Resource#getDescription() description} of the * resource. */ public ResourcePropertySource(String location, ClassLoader classLoader) throws IOException { this(new DefaultResourceLoader(classLoader).getResource(location)); }
@Override public Resource getResource(String location) { Assert.notNull(location, "Location must not be null"); for (ProtocolResolver protocolResolver : this.protocolResolvers) { Resource resource = protocolResolver.resolve(location, this); if (resource != null) { return resource; } } if (location.startsWith("/")) { return getResourceByPath(location); } else if (location.startsWith(CLASSPATH_URL_PREFIX)) { return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()), getClassLoader()); } else { try { // Try to parse the location as a URL... URL url = new URL(location); return (ResourceUtils.isFileURL(url) ? new FileUrlResource(url) : new UrlResource(url)); } catch (MalformedURLException ex) { // No URL -> resolve as resource path. return getResourceByPath(location); } } }
private void copyResourceFromClasspath(String reportDirectory, Path relativePathSource, Path relativePathDest) throws IOException { File destination = Paths.get(reportDirectory).resolve(relativePathDest).toFile(); InputStream sourceStream = new DefaultResourceLoader().getResource(CLASSPATH + relativePathSource.toString()).getInputStream(); // overwrite if exists FileUtils.copyInputStreamToFile(sourceStream, destination); }
/** * Load and print the Spring banner (if one is configured) to UserConsole. * @param environment the Spring environment */ static void printBanner(final Environment environment) { try { final String bannerLocation = environment.getProperty(BANNER_LOCATION_SPRING_PROPERTY_KEY); if (StringUtils.isNotBlank(bannerLocation)) { final ResourceLoader resourceLoader = new DefaultResourceLoader(); final Resource resource = resourceLoader.getResource(bannerLocation); if (resource.exists()) { final String banner = StreamUtils.copyToString( resource.getInputStream(), environment.getProperty( BANNER_CHARSET_SPRING_PROPERTY_KEY, Charset.class, StandardCharsets.UTF_8 ) ); UserConsole.getLogger().info(banner); } } } catch (final Throwable t) { System.err.println("Failed to print banner: " + t.getMessage()); t.printStackTrace(System.err); } } }
protected InputStream getInputStream(String xmlFile) throws IOException { File file = new File(xmlFile); if (file.exists()) { return new BufferedInputStream(new FileInputStream(file)); } DefaultResourceLoader loader = new DefaultResourceLoader(); Resource resource = loader.getResource(xmlFile); if (resource.exists()) { return resource.getInputStream(); } throw new IOException("Unable to locate " + xmlFile); }
protected Properties getCustomProperties() throws IOException { String location = getCustomPropertiesLocation(); Reader input = null; try { Resource resource = loader.getResource(location); input = new InputStreamReader(resource.getInputStream(), getEncoding()); Properties p = new Properties(); p.load(input); return p; } finally { IOUtils.closeQuietly(input); } }
public void verifyExists(List<String> locations) throws FileNotFoundException { ResourceLoader loader = new DefaultResourceLoader(); for (String location : locations) { Resource resource = loader.getResource(location); if (!resource.exists()) { throw new FileNotFoundException("Unable to locate " + location); } } }
import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.Resource; public static void myFunction() throws IOException { final DefaultResourceLoader loader = new DefaultResourceLoader(); LOGGER.info(loader.getResource("classpath:META-INF/resources/img/copyright.png").exists()); Resource resource = loader.getResource("classpath:META-INF/resources/img/copyright.png"); BufferedImage watermarkImage = ImageIO.read(resource.getFile()); }
/** * Create a new SimpleMetadataReaderFactory for the default class loader. */ public SimpleMetadataReaderFactory() { this.resourceLoader = new DefaultResourceLoader(); }
public void createResource(String resource) { if (resource != null) { DefaultResourceLoader resourceLoader = new DefaultResourceLoader(); Resource tempPropertyResource = resourceLoader.getResource(resource); if (tempPropertyResource != null && tempPropertyResource.exists()) { propertyResource = tempPropertyResource; dirty = true; } } }
@Bean(initMethod = "init", name = "beetlConfig") public BeetlGroupUtilConfiguration getBeetlGroupUtilConfiguration() { BeetlGroupUtilConfiguration beetlGroupUtilConfiguration = new BeetlGroupUtilConfiguration(); ResourcePatternResolver patternResolver = ResourcePatternUtils.getResourcePatternResolver(new DefaultResourceLoader()); try { // WebAppResourceLoader 配置root路径是关键 WebAppResourceLoader webAppResourceLoader = new WebAppResourceLoader(patternResolver.getResource("classpath:/templates").getFile().getPath()); beetlGroupUtilConfiguration.setResourceLoader(webAppResourceLoader); } catch (IOException e) { e.printStackTrace(); } //读取配置文件信息 return beetlGroupUtilConfiguration; }
@Test(expected = IllegalArgumentException.class) public void testStrictSystemPropertyReplacement() { PropertyEditor editor = new ResourceEditor(new DefaultResourceLoader(), new StandardEnvironment(), false); System.setProperty("test.prop", "foo"); try { editor.setAsText("${test.prop}-${bar}"); Resource resolved = (Resource) editor.getValue(); assertEquals("foo-${bar}", resolved.getFilename()); } finally { System.getProperties().remove("test.prop"); } }
/** * TODO: Delete me! * * @param dataInfoProps * @throws IOException */ public CoreResources(Properties dataInfoProps) throws IOException { this.dataInfo = dataInfoProps; if (resourceLoader == null) resourceLoader = new DefaultResourceLoader(); webapp = getWebAppName(resourceLoader.getResource("/").getURI().getPath()); }
ResourcePatternResolver patternResolver = new PathMatchingResourcePatternResolver(new DefaultResourceLoader()); Resource[] resources = patternResolver.getResources("classpath*:META-INF/spring.xml"); for(Resource resource : resources) { System.out.println(resource.getDescription()); }
Resource getRelative(final Resource baseRef, final String relative) { try { if (relative.startsWith("classpath:")) { return RESOURCE_LOADER.getResource(relative); } else { return baseRef.createRelative(relative); } } catch (final IOException e) { throw new ObjectRetrievalFailureException(Resource.class, baseRef, "Resource location has a relative path, however the configResource does not reference a file, so the relative path cannot be resolved. The location is: " + relative, null); } }
/** * Adds a {@link Resource} to this builder. * * @param resource the resource * @return the {@link ResourceConfigurer} for chaining */ @Override public ResourceConfigurer<I> resource(String resource) { resources.add(resourceLoader.getResource(resource)); return this; }
/** * Apply any relevant post processing the {@link ApplicationContext}. Subclasses can * apply additional processing as required. * @param context the application context */ protected void postProcessApplicationContext(ConfigurableApplicationContext context) { if (this.beanNameGenerator != null) { context.getBeanFactory().registerSingleton( AnnotationConfigUtils.CONFIGURATION_BEAN_NAME_GENERATOR, this.beanNameGenerator); } if (this.resourceLoader != null) { if (context instanceof GenericApplicationContext) { ((GenericApplicationContext) context) .setResourceLoader(this.resourceLoader); } if (context instanceof DefaultResourceLoader) { ((DefaultResourceLoader) context) .setClassLoader(this.resourceLoader.getClassLoader()); } } if (this.addConversionService) { context.getBeanFactory().setConversionService( ApplicationConversionService.getSharedInstance()); } }
@Override protected Resource getResourceByPath(String path) { if (this.applicationContext.getServletContext() != null) { return new ServletContextResource( this.applicationContext.getServletContext(), path); } return super.getResourceByPath(path); }
public Document getDesignXmlDocument() throws Exception { DefaultResourceLoader resourceLoader = new DefaultResourceLoader(ClassLoaderUtils.getDefaultClassLoader()); InputStream in = resourceLoader.getResource(DefaultResourceLoader.CLASSPATH_URL_PREFIX + "design.xml").getInputStream(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(in); return doc; }