一文让你读懂JAVA.IO、字符编码、URL和Spring.Resource( 四 )

  • 2 ByteArrayResource:获取byte数组表示的资源 基于ByteArrayInputStream和字节数组实现,应用场景类似ByteArrayInputStream,缓存byte[]资源
  • 3 ClassPathResource:获取类路径下的资源
//ClassPathResource.java 的三个属性private final String path;//使用Class或ClassLoader加载资源private ClassLoader classLoader;private Class<?> clazz;---使用方式----Resource resource = new ClassPathResource("test.txt");
  • 4 InputStreamResource:接收一个InputStream对象,获取输入流封装的资源
  • 5 ServletContextResourse:加载ServletContext环境下(相对于Web应用根目录的)路径资源,获取的资源
  • 6 UrlResource:通过URL访问http资源和FTP资源等
8 ResourceLoader 获取资源
一文让你读懂JAVA.IO、字符编码、URL和Spring.Resource

文章插图
 
resource.png
  • ResourceLoader是为了屏蔽了Resource的具体实现,统一资源的获取方式 。你即能从ResourceLoader加载ClassPathResource,也能加载FileSystemResource等
public interface ResourceLoader {// 默认从类路径加载的资源 前缀: "classpath:",获取ClassPathResourceString CLASSPATH_URL_PREFIX = ResourceUtils.CLASSPATH_URL_PREFIX;Resource getResource(String location);
  • ResourceLoader接口默认对classpath路径下面的资源进行加载
public interface ResourcePatternResolver extends ResourceLoader {// 默认加载所有路径(包括jar包)下面的文件,"classpath*:", 获取ClassPathResourceString CLASSPATH_ALL_URL_PREFIX = "classpath*:";
  • ResourcePatternResolver默认会加载所有路径下面的文件,获得ClassPathResource;classpath:只会在class类路径下查找;而classpath*:会扫描所有JAR包及class类路径下出现的文件
//Ant风格表达式com/smart/**/*.xml ResourcePatternResoler resolver = new PathMatchingResourcePatternResolver();Resource resources[] = resolver.getResources("com/smart/**/*.xml");// ApplicationContext ctx //FileSystemResource资源Resource template = ctx.getResource("file:///res.txt");//UrlResource资源Resource template = ctx.getResource("https://my.cn/res.txt");
  • ResourceLoader方法getResource的locationPattern可设置资源模式前缀来获取非ClassPathResource资源,locationPattern支持Ant风格
前缀 示例 描述 classpath: classpath:config.xml 从类路径加载 file: file:///res.txt 从文件系统加载FileSystemResource http: http://my.cn/res.txt 加载UrlResource
9 JAVA.Properties了解一下
  • Properties是java自带的配置处理类;Properties加载资源的两种方式
public class Properties extends Hashtable<Object,Object>{.... //可根据Reader或者InputStream加载properties文件内容public synchronized void load(Reader reader) throws IOExceptionpublic synchronized void load(InputStream inStream) throws IOException
  • Properties读取配置示例代码
//res.propertiesusername = rootpassword = password-------代码示例-------------InputStream input = ClassLoader.getSystemResourceAsStream("res.properties");Properties prop = new Properties();prop.load(inputStream); //根据inputStream载入资源String username = prop.getProperty("username");10 yml配置资源的读取
  • 普通java项目如果需要读取yml可引入jackson-dataformat-yaml,而springboot默认配置支持yml的读取
<dependency><groupId>com.fasterxml.jackson.dataformat</groupId><artifactId>jackson-dataformat-yaml</artifactId><version>2.9.5</version>
  • 基于jackson-dataformat-yaml对yml配置资源的读取
//res.yml 配置name: chenparams:url:http://www.my.com----------代码示例---------------InputStream input = ClassLoader.getSystemResourceAsStream("res.yml");Yaml yml = new Yaml();Map map = new Yaml().loadAs(input, LinkedHashMap.class);; //根据inputStream载入资源String name = MapUtils.getString(map,"name"); // chen//url:http://www.my.com11 优雅地关闭资源,try-with-resource语法和lombok@Cleanup
  • 资源的打开就需要对应的关闭,但我们常会忘记关闭资源,或在多处代码关闭资源感到杂乱,有没有简洁的关闭方法呢?
  • 自动关闭资源类需实现AutoCloseable接口和配合try-with-resource语法糖使用
public class YSOAPConnection implements AutoCloseable {private SOAPConnection connection;public static YSOAPConnection open(SOAPConnectionFactory soapConnectionFactory) throws SOAPException{YSOAPConnection ySoapConnection = new YSOAPConnection();SOAPConnection connection = soapConnectionFactory.createConnection();ySoapConnection.setConnection(connection);return ySoapConnection;}public SOAPMessage call(SOAPMessage request, Object to) throws SOAPException {return connection.call(request, to);}@Overridepublic void close() throws SOAPException {if (connection != null) {connection.close(); }}}


推荐阅读