Spring(2)
2019-08-13 本文已影响0人
百炼
date
[TOC]
InputStreamSource 接口的实现层次
public interface InputStreamSource {
InputStream getInputStream() throws IOException;
}
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;
long lastModified() throws IOException;
Resource createRelative(String relativePath) throws IOException;
String getFilename();
String getDescription();
}
Resource接口封闭底层资源,InputStreamSource接口只有一个得到InputStream的方法,所有实现都可以使用InputStream接收。InputStreamSource不管如何实现,具体实现交给了各个子类,给子类充分的自由。而真正处理资源的子类实现的是Resource接口,Resource接口声明了子类的一些约定方法。(接口其实就是一种约定)
InputStreamSource的继承结构 Resource接口的实现类XmlBeanFactory 得到Bean
idea查看调用的时序图 MyTestBean的时序图idea安装SequencePlugin(直到写本篇文章时,SequencePlugin的version为1.3.0)
MyTestBean.java
package edu.cninfo.vo;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
/**
* @author chenxinwei
* create 2019-08-12 15:53
*/
public class MyTestBean {
private String str = "This is my test Bean";
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
@Override
public String toString() {
return super.toString();
}
public static void main(String[] args) {
Resource resource = new ClassPathResource("myTestBean.xml", MyTestBean.class);
BeanFactory bf = new XmlBeanFactory(resource);
final MyTestBean bean = bf.getBean(MyTestBean.class);
System.out.println(bean.getStr());
}
}
myTestBean.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="myTestBean" class="edu.cninfo.vo.MyTestBean">
<property name="str" value="hello"/>
</bean>
</beans>
代码Resource resource = new ClassPathResource("myTestBean.xml", MyTestBean.class);
使用实例时,将第一个参数使用StringUtils.cleanPath(param1)
的结果给path
属性赋值
StringUtils.cleanPath 清理目录操作
说明: 代码调试的时候看到StringUtils.cleanPath,搜索到工程中的单元测试类StringUtilsTest,定位到相应的测试方法下,看cleanPath的功能。
public void testCleanPath() {
assertEquals("mypath/myfile", StringUtils.cleanPath("mypath/myfile"));
assertEquals("mypath/myfile", StringUtils.cleanPath("mypath\\myfile"));
assertEquals("mypath/myfile", StringUtils.cleanPath("mypath/../mypath/myfile"));
assertEquals("mypath/myfile", StringUtils.cleanPath("mypath/myfile/../../mypath/myfile"));
assertEquals("../mypath/myfile", StringUtils.cleanPath("../mypath/myfile"));
assertEquals("../mypath/myfile", StringUtils.cleanPath("../mypath/../mypath/myfile"));
assertEquals("../mypath/myfile", StringUtils.cleanPath("mypath/../../mypath/myfile"));
assertEquals("/../mypath/myfile", StringUtils.cleanPath("/../mypath/myfile"));
assertEquals("/mypath/myfile", StringUtils.cleanPath("/a/:b/../../mypath/myfile"));
assertEquals("file:///c:/path/to/the%20file.txt", StringUtils.cleanPath("file:///c:/some/../path/to/the%20file.txt"));
}
Spring 中使用Schema对xml进行校验