9.spring之内置resource实现

1.ByteArrayResource

ByteArrayResource代表byte[]数组资源,对于“getInputStream”操作将返回一个ByteArrayInputStream。

public class ResourceTest {
    public static void main(String[] args) {
        Resource resource = new ByteArrayResource("HelloWorld".getBytes());
        if (resource.exists()) {
            dumpStream(resource);
        }
    }

    private static void dumpStream(Resource resource) {
        InputStream is = null;
        try {
            //1.获取文件资源
            is = resource.getInputStream();
            //2.读取资源
            byte[] descBytes = new byte[is.available()];
            is.read(descBytes);
            System.out.println(new String(descBytes));
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            try {
                //3.关闭资源
                is.close();
            } catch (IOException e) {
            }
        }
    }
}

dumpStream方法很抽象定义了访问流的三部曲:打开资源、读取资源、关闭资源,所以dunpStrean可以再进行抽象从而能在自己项目中使用;byteArrayResourceTest测试方法,也定义了基本步骤:定义资源、验证资源存在、访问资源。

ByteArrayResource可多次读取数组资源,即isOpen ()永远返回false。

2.InputStreamResource

InputStreamResource代表java.io.InputStream字节流,对于“getInputStream ”操作将直接返回该字节流,因此只能读取一次该字节流,即“isOpen”永远返回true。

    @Test
    public void testInputStreamResource(){
        ByteArrayInputStream bis = new ByteArrayInputStream("Hello World!".getBytes());
        Resource resource = new InputStreamResource(bis);
        if(resource.exists()) {
            dumpStream(resource);
        }
        Assert.assertEquals(true, resource.isOpen());
    }

3.FileSystemResource

    @Test
    public void testFileSystemResource(){
        Resource resource = new FileSystemResource("D:\\anjunshuang\\dimore.txt");
        if(resource.exists()) {
            dumpStream(resource);
        }
        Assert.assertEquals(false, resource.isOpen());
    }

4.ClassPathResource

ClassPathResource代表classpath路径的资源,将使用ClassLoader进行加载资源。classpath 资源存在于类路径中的文件系统中或jar包里,且“isOpen”永远返回false,表示可多次读取资源。

ClassPathResource加载资源替代了Class类和ClassLoader类的“getResource(String name)”和“getResourceAsStream(String name)”两个加载类路径资源方法,提供一致的访问方式。

ClassPathResource提供了三个构造器:

public ClassPathResource(String path):使用默认的ClassLoader加载“path”类路径资源;

public ClassPathResource(String path, ClassLoader classLoader)使用指定的ClassLoader加载“path”类路径资源;

    @Test
    public void testClassPathResource() {
        Resource resource = new ClassPathResource("application.properties");
        if(resource.exists()) {
            dumpStream(resource);
        }
        Assert.assertEquals(false, resource.isOpen());
    }

加载jar包里的资源,首先在当前类路径下找不到,最后才到Jar包里找,而且在第一个Jar包里找到的将被返回

    @Test
    public void classpathResourceTestFromJar() throws IOException {
        Resource resource = new ClassPathResource("templates/error.ftl");
        if(resource.exists()) {
            dumpStream(resource);
        }
        System.out.println("path:" + resource.getURI().getPath());
        Assert.assertEquals(false, resource.isOpen());
    }

5.UrlResource

UrlResource代表URL资源,用于简化URL资源访问。“isOpen”永远返回false,表示可多次读取资源。

UrlResource一般支持如下资源访问:

http通过标准的http协议访问web资源,如new UrlResource(“http://地址”);

ftp通过ftp协议访问资源,如new UrlResource(“ftp://地址”);

file通过file协议访问本地文件系统资源,如new UrlResource(“file:d:/test.txt”);

6.ServletContextResource

ServletContextResource代表web应用资源,用于简化servlet容器的ServletContext接口的getResource操作和getResourceAsStream操作

7.VfsResource

VfsResource代表Jboss 虚拟文件系统资源。

Jboss VFS(Virtual File System)框架是一个文件系统资源访问的抽象层,它能一致的访问物理文件系统、jar资源、zip资源、war资源等,VFS能把这些资源一致的映射到一个目录上,访问它们就像访问物理文件资源一样,而其实这些资源不存在于物理文件系统。

在示例之前需要准备一些jar包,在此我们使用的是Jboss VFS3版本,可以下载最新的Jboss AS 6x,拷贝lib目录下的“jboss-logging.jar”和“jboss-vfs.jar”两个jar包拷贝到我们项目的lib目录中并添加到“Java Build Path”中的“Libaries”中。

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.example.demo1.service; import com.example.demo1.mapper.*; import com.example.demo1.model.ResponseData; import com.example.demo1.model.ViewEvents; import com.example.demo1.util.ResponseDataUtil; import jakarta.annotation.Resource; import org.springframework.stereotype.Service; import org.springframework.util.ReflectionUtils; import java.lang.reflect.Method; import java.util.HashMap; import java.util.List; @Service public class service { @Resource com.example.demo1.mapper.SolutionAttrMapper SolutionAttrMapper; @Resource com.example.demo1.mapper.ViewAttrMapper ViewAttrMapper; @Resource com.example.demo1.mapper.TableFieldsMapper TableFieldsMapper; @Resource SolutionViewsMapper SolutionViewsMapper; @Resource com.example.demo1.mapper.ViewEventsMapper ViewEventsMapper; @Resource com.example.demo1.method.common Common; public ResponseData solutionCloud(String solution, String view, String event) { switch (event) { case "tech": // 取数 HashMap<String, Object> map = new HashMap<>(1); map.put("SolutionAttr", SolutionAttrMapper.selectByPrimaryKey(solution)); map.put("SolutionViews", SolutionViewsMapper.selectBySolution(solution)); map.put("ViewAttr", ViewAttrMapper.selectByPrimaryKey(view)); map.put("TableFields", TableFieldsMapper.selectByView(view)); // 事件 List<ViewEvents> ViewEvents = ViewEventsMapper.selectbyView(view); for (ViewEvents ViewEvent : ViewEvents) { ResponseData responseData = Common.fields_sort(ViewEvent.getImport1()); if (ViewEvent.getEvent().substring(1) == "_") { //内置事件 String methodName = ViewEvent.getMethod(); String import1 = ViewEvent.getImport1(); Method method1 = ReflectionUtils.findMethod(Common.getClass(), methodName); Object result1 = ReflectionUtils.invokeMethod(method1, Common,import1); } } return ResponseDataUtil.buildSuccess(map); case "data": // return new ResponseEntity(SolutionAttrMapper.selectByPrimaryKey(solution), HttpStatus.OK); } return null; } }
最新发布
06-08

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值