001/* 002 * Copyright 2002-2010 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.web.context.support; 018 019import javax.servlet.ServletContext; 020 021import org.springframework.core.io.DefaultResourceLoader; 022import org.springframework.core.io.Resource; 023 024/** 025 * ResourceLoader implementation that resolves paths as ServletContext 026 * resources, for use outside a WebApplicationContext (for example, 027 * in an HttpServletBean or GenericFilterBean subclass). 028 * 029 * <p>Within a WebApplicationContext, resource paths are automatically 030 * resolved as ServletContext resources by the context implementation. 031 * 032 * @author Juergen Hoeller 033 * @since 1.0.2 034 * @see #getResourceByPath 035 * @see ServletContextResource 036 * @see org.springframework.web.context.WebApplicationContext 037 * @see org.springframework.web.servlet.HttpServletBean 038 * @see org.springframework.web.filter.GenericFilterBean 039 */ 040public class ServletContextResourceLoader extends DefaultResourceLoader { 041 042 private final ServletContext servletContext; 043 044 045 /** 046 * Create a new ServletContextResourceLoader. 047 * @param servletContext the ServletContext to load resources with 048 */ 049 public ServletContextResourceLoader(ServletContext servletContext) { 050 this.servletContext = servletContext; 051 } 052 053 /** 054 * This implementation supports file paths beneath the root of the web application. 055 * @see ServletContextResource 056 */ 057 @Override 058 protected Resource getResourceByPath(String path) { 059 return new ServletContextResource(this.servletContext, path); 060 } 061 062}