Resource接口资源操作

45 篇文章 1 订阅

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.12.RELEASE</version>
        </dependency>

先来看下UrlResource实现类

public class A {

    public static void main(String[] args) throws IOException {
        //根据http前缀访问网络资源
        UrlResource urlResource=new UrlResource("http://www.baidu.com");
        //根据file前缀 访问项目 根目录下的文件
        //UrlResource urlResource=new UrlResource("file:aa.txt");
        System.out.println("文件名称:"+urlResource.getFilename());
        System.out.println("文件描述:"+urlResource.getDescription());
        InputStream inputStream = urlResource.getInputStream();
        byte[]b=new byte[1024];
        while (inputStream.read(b)!=-1){
            System.out.println(new String(b));
        }
    }
}

 aa.txt 是在项目的根目录下,内容如下

你好啊123

在看下FileSystemResource 访问文件系统资源

public class A {

    public static void main(String[] args) throws IOException {
        //访问绝对路径的文件地址
       // FileSystemResource urlResource=new FileSystemResource("E:\\daima\\aa.txt");
        //访问项目 根目录下的文件
        FileSystemResource urlResource=new FileSystemResource("aa.txt");
        System.out.println("文件名称:"+urlResource.getFilename());
        System.out.println("文件描述:"+urlResource.getDescription());
        InputStream inputStream = urlResource.getInputStream();
        byte[]b=new byte[1024];
        while (inputStream.read(b)!=-1){
            System.out.println(new String(b));
        }
    }
}

在看下ClassPathResource 访问resources目录下的文件

public class A {

    public static void main(String[] args) throws IOException {
        //访问resources目录下的文件
        ClassPathResource urlResource=new ClassPathResource("b.txt");
        System.out.println("文件名称:"+urlResource.getFilename());
        System.out.println("文件描述:"+urlResource.getDescription());
        InputStream inputStream = urlResource.getInputStream();
        byte[]b=new byte[1024];
        while (inputStream.read(b)!=-1){
            System.out.println(new String(b));
        }
    }
}

当使用 new FileSystemXmlApplicationContext()的时候

Resource获取到的对象就是FileSystemResource

public class Test {

    public static void main(String[] args) throws IOException {
      ApplicationContext applicationContext=  new FileSystemXmlApplicationContext();
      Resource resource = applicationContext.getResource("aa.txt");
      System.out.println(resource.getFilename());
      System.out.println(resource.getDescription());
        InputStream inputStream = resource.getInputStream();
        byte[]b=new byte[1024];
        while (inputStream.read(b)!=-1){
            System.out.println(new String(b));
        }
    }


}

 当使用new ClassPathXmlApplicationContext的时候

Resource找的都是resources目录下的文件

使用的对象都是ClassPathResource

 

  public static void main(String[] args) throws IOException {
      ApplicationContext applicationContext=  new ClassPathXmlApplicationContext();
      Resource resource = applicationContext.getResource("b.txt");
      System.out.println(resource.getFilename());
      System.out.println(resource.getDescription());
        InputStream inputStream = resource.getInputStream();
        byte[]b=new byte[1024];
        while (inputStream.read(b)!=-1){
            System.out.println(new String(b));
        }
    }

在来看下实现ResourceLoaderAware 资源加载接口

就是在项目启动成功之后,在别的地方可以调用里面的一些功能


import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.ResourceLoader;

//自定义资源加载增强类
public class MyResourceLoaderAware implements ResourceLoaderAware {

    //set注入
    private ResourceLoader resourceLoader;

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.resourceLoader=resourceLoader;
    }

    //获取资源
    public ResourceLoader getResourceLoader() {
        return resourceLoader;
    }


}
public class Test {

    public static void main(String[] args) throws IOException {
      ApplicationContext applicationContext=  new ClassPathXmlApplicationContext("bean.xml");
      MyResourceLoaderAware myResourceLoaderAware=applicationContext.getBean(MyResourceLoaderAware.class);
      System.out.println(myResourceLoaderAware.getResourceLoader());
      System.out.println(applicationContext);
      //上面2个打印的都是一样的,所以这里返回true 也就是在spring加载的时候,就把ResourceLoader给自动注入了
      //我们在别的地方就可以使用ResourceLoader了
      System.out.println(applicationContext==myResourceLoaderAware.getResourceLoader());
        ResourceLoader resourceLoader = myResourceLoaderAware.getResourceLoader();
        Resource resource = resourceLoader.getResource("b.txt");
        System.out.println(resource.getFilename());
        System.out.println(resource.getDescription());
        InputStream inputStream = resource.getInputStream();
        byte[]b=new byte[1024];
        while (inputStream.read(b)!=-1){
            System.out.println(new String(b));
        }
    }
}

bean.xml 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


    <bean id="myResourceLoaderAware" class="com.dmg.st.dao.MyResourceLoaderAware">

    </bean>
</beans>

可以看到,当项目启动成功后 就可以获取到resources目录下的信息

 我们还可以以bean*这个前缀来访问所有beanxxx的.xml文件

在创建一个bean-a.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="user" class="com.dmg.st.dao.User"></bean>
</beans>
public class Test {

    public static void main(String[] args) throws IOException {
      ApplicationContext applicationContext=  new ClassPathXmlApplicationContext("bean*.xml");
      User user=(User)applicationContext.getBean(User.class);
        System.out.println(user);

    }
}

 可以看到访问成功了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值