Spring中的ResourceLoader接口

来源:互联网 发布:淘宝先锋乒羽商城 编辑:程序博客网 时间:2024/05/17 22:43

先说说Resource接口

Spring框架内的org.springframework.core.io包下的Resource接口是作为所有资源的抽象和访问接口。

Resource接口的定义

public interface Resource extends InputStreamSource {    boolean exists();    boolean isReadable();    boolean isOpen();    URL getURL() throws IOException;    URI getURI() throws IOException;    File getFile() throws IOException;    long contentLength() throws IOException;    Resource createRelative(String relativePath) throws IOException;    String getFilename();    String getDescription();}

该接口定义了7个方法,可以帮助我们查询资源状态、访问资源内容等等。可以根据资源的不同类型,或者资源所处的不同场合,给出相应的具体实现类。资源有了,再来看看如何去查找和定位这些资源,这个时候我们的ResourceLoader就闪亮登场了。

ResourceLoader 统一资源定位器

org.springframework.core.io.ResourceLoader接口是资源查找定位策略的统一抽象。

ResourceLoader接口定义

public interface ResourceLoader {    String CLASSPATH_URL_PREFIX = ResourceUtils.CLASSPATH_URL_PREFIX;    Resource getResource(String location);    ClassLoader getClassLoader();}

这里面最主要的就是getResource()方法,通过它我们就可以根据指定的资源位置,定位到具体的资源实例
上面说过ResourceLoader接口是提供资源查找策略的统一 抽象,具体资源查找策略则由相应的ResourceLoader实现类给出,它默认的实现类是DefaultResourceLoader。下面用具体的代码来演示利用ResourceLoader实现类来读取配置文件的属性。

demo演示

先在项目根目录下的application.properties里面写如下内容

project.name=myproject

然后是java代码

/** * *  * @ClassName PropertyUtil.java * <p>Description: </p> * @author 沉鱼 * @date 2017年11月30日 下午4:15:49 */public class PropertyUtil {    private static final String PROPERTY_LOCATION = "classpath:application.properties";    public static String getProperyies(String key) throws IOException{        DefaultResourceLoader resourceLoader = new DefaultResourceLoader();        Resource resource = resourceLoader.getResource(PROPERTY_LOCATION);        ResourcePropertySource  properties = new ResourcePropertySource(resource);        Map<String,Object> map = properties.getSource();        return map.get(key).toString();    }    public static void main(String[] args) throws IOException {        System.out.println(getProperyies("project.name"));    }}

启动main方法后,控制台会打印myproject

说明:ResourcePropertySource这个类最好看一下源码,它一层一层有继承EnumerablePropertySource<Map<String, Object>这个类,然后EnumerablePropertySource<Map<String, Object>>,又继承PropertySource<T>抽象类

PropertySource抽象类部分定义

public abstract class PropertySource<T> {    protected final String name;    protected final T source;    ...}

其实ResourcePropertySource这个类有直接提供,只需要一个资源路径的构造方法

public ResourcePropertySource(String location) throws IOException {        this(new DefaultResourceLoader().getResource(location));    }

所以上面代码可以直接替换成下面这样的,得到结果是一样,

public class PropertyUtil {    private static final String PROPERTY_LOCATION = "classpath:application.properties";    public static String getProperyies(String key) throws IOException{        ResourcePropertySource  properties = new ResourcePropertySource(resource);        Map<String,Object> map = properties.getSource();        return map.get(key).toString();    }    public static void main(String[] args) throws IOException {        System.out.println(getProperyies("project.name"));    }}

但这么做,跟本文标题ResourceLoader似乎没有什么关系啦(o_o),不过按上面的写法显得对底层了解多一些哈哈。
好了,以后在工作项目中,想获得配置文件中的属性值,可以像上面一样自己写一个工具类,然后就装X去吧。