org.springframework.core.io.ClassPathResource类
测试代码
package cn.edu.hdu.pichen.springexample; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.List; import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.StringUtils; import cn.edu.hdu.pichen.springexample.beans.User; /**
* Hello world!
*
*/
public class App
{
public static void main(String[] args) throws IOException
{
// ClassPathResource resource = new
// ClassPathResource("./../classes/beans.xml");
// System.out.println(resource.getPath());
// System.out.println(resource.getDescription());
// System.out.println(resource.getURL().getPath());
//
// InputStream is = resource.getInputStream();
// BufferedReader br = new BufferedReader(new InputStreamReader(is));
// String str = br.readLine();
// while (str != null)
// {
// System.out.println(str);
// str = br.readLine();
// }
//
System.out.println(StringUtils.cleanPath(".\\beans.xml")); // \\替换为/
System.out.println(StringUtils.cleanPath("file:core/../core/./io/Resource.class")); // System.out.println(StringUtils.cleanPath("./../../beans.xml"));
// System.out.println(StringUtils.cleanPath("./tmp/tmp2/../beans.xml"));
//
//
// BeanFactory factory = new XmlBeanFactory(resource);
// User user = factory.getBean("user", User.class);
// System.out.println("Name:" + user.getName() + "\nAge:" +
// user.getAge());
}
}
pom
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>cn.edu.hdu.pichen</groupId>
<artifactId>springexample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>springexample</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.0.7.RELEASE</version>
</dependency> </dependencies>
</project>
涉及的相关类源码
org.springframework.util.StringUtils工具类的collectionToDelimitedString方法
/**
* Convenience method to return a Collection as a delimited (e.g. CSV)
* String. E.g. useful for <code>toString()</code> implementations.
* @param coll the Collection to display
* @param delim the delimiter to use (probably a ",")
* @param prefix the String to start each element with
* @param suffix the String to end each element with
* @return the delimited String
*/
public static String collectionToDelimitedString(Collection<?> coll, String delim, String prefix, String suffix) {
if (CollectionUtils.isEmpty(coll)) {
return "";
}
StringBuilder sb = new StringBuilder();//局部变量,不需要同步,使用StringBuilder即可
Iterator<?> it = coll.iterator();
while (it.hasNext()) {
sb.append(prefix).append(it.next()).append(suffix);
if (it.hasNext()) {
sb.append(delim);
}
}
return sb.toString();
}
该方法代码很简单,不需要多说明,就是简单的字符串拼接,依次遍历入参coll集合,并取出元素进行前缀、后缀、分隔符拼接。
入参及返回值说明
* @param 需要处理的元素集合(如果是对象,拼接的是toString方法的字符串)
* @param 分隔符(如分号;)
* @param 拼接的元素前缀
* @param 拼接的元素后缀
* @return 处理完后的结果
举个例子:
List<String> pathElements = new LinkedList<String>();
pathElements.add("AAA");
pathElements.add("BBB");
pathElements.add("CCC");
System.out.println(StringUtils.collectionToDelimitedString(pathElements, ";", "#", "$"));
结果打印(分隔符";",前缀"#", 后缀"$"):
#AAA$;#BBB$;#CCC$
org.springframework.util.StringUtils工具类的cleanPath方法
/**
* Normalize the path by suppressing sequences like "path/.." and
* inner simple dots.
* <p>The result is convenient for path comparison. For other uses,
* notice that Windows separators ("\") are replaced by simple slashes.
* @param path the original path
* @return the normalized path
*/
public static String cleanPath(String path) {
if (path == null) {
return null;
}
String pathToUse = replace(path, WINDOWS_FOLDER_SEPARATOR, FOLDER_SEPARATOR); // Strip prefix from path to analyze, to not treat it as part of the
// first path element. This is necessary to correctly parse paths like
// "file:core/../core/io/Resource.class", where the ".." should just
// strip the first "core" directory while keeping the "file:" prefix.
int prefixIndex = pathToUse.indexOf(":");
String prefix = "";
if (prefixIndex != -1) {
prefix = pathToUse.substring(0, prefixIndex + 1);
pathToUse = pathToUse.substring(prefixIndex + 1);
}
if (pathToUse.startsWith(FOLDER_SEPARATOR)) {
prefix = prefix + FOLDER_SEPARATOR;
pathToUse = pathToUse.substring(1);
} String[] pathArray = delimitedListToStringArray(pathToUse, FOLDER_SEPARATOR);
List<String> pathElements = new LinkedList<String>();
int tops = 0; for (int i = pathArray.length - 1; i >= 0; i--) {
String element = pathArray[i];
if (CURRENT_PATH.equals(element)) {
// Points to current directory - drop it.
}
else if (TOP_PATH.equals(element)) {
// Registering top path found.
tops++;
}
else {
if (tops > 0) {
// Merging path element with element corresponding to top path.
tops--;
}
else {
// Normal path element found.
pathElements.add(0, element);
}
}
} // Remaining top paths need to be retained.
for (int i = 0; i < tops; i++) {
pathElements.add(0, TOP_PATH);
} return prefix + collectionToDelimitedString(pathElements, FOLDER_SEPARATOR);
}
该方法根据输入的原始路径转换成一个规格化的路径,如下示例:
.\\beans.xml -----》beans.xml
file:core/../core/./io/Resource.class -----》file:core/io/Resource.class
代码也不复杂简单说明下吧:
org.springframework.core.io.ClassPathResource类的更多相关文章
- org.springframework.core.io包内的源码分析
前些日子看<深入理解javaweb开发>时,看到第一章java的io流,发觉自己对io流真的不是很熟悉.然后看了下JDK1.7中io包的一点点代码,又看了org.springframewo ...
- 无法访问org.springframework.core.NestedRuntimeException 找不到org.springframework.core.NestedRuntimeException的类文件
在学习springAOP时,出现如下异常: 无法访问org.springframework.core.NestedRuntimeException 找不到org.springframework.cor ...
- cannot access org.springframework.core.io.InputStreamSouce
cannot access org.springframework.core.io.InputStreamSouce错误,把mian路径下main.iml文件备份一下,然后删除该文件,报错就会消失,但 ...
- java.lang.NoSuchMethodError: org.springframework.core.io.ResourceEditor
这种情况一般是jar包版本问题,pom导入的jar包存在一个2.5.6的,删掉即可.
- java.lang.NoSuchMethodError: org.springframework.beans.factory.xml.XmlReaderContext.getResourceLoader()Lorg/springframework/core/io/ResourceLoader
问题原因 在整合spring跟struts2是使用Maven,用到struts2-spring-plugin.jar,但是maven不但但加载了这个jar文件还有spring-beans:3.0.5. ...
- The type org.springframework.core.io.support.ResourcePatternResolver cannot be resolved. It is ind
转自:https://blog.csdn.net/evilcry2012/article/details/49208909 缺包 spring-core-.RELEASE.jar
- java.lang.NoSuchMethodError: org.springframework.core.io.ResourceEditor错误
一般是jar包冲突,或者某些jar包版本不同. 如上,spring其他包的版本均为4.2.5,而spring-webmvc的jar包为1.2.6版本,造成版本冲突. 把该包版本改为4.2.5,宣告成功 ...
- spring源码分析-core.io包里面的类
前些日子看<深入理解javaweb开发>时,看到第一章java的io流,发觉自己对io流真的不是很熟悉.然后看了下JDK1.7中io包的一点点代码,又看了org.springframewo ...
- org.springframework.core.Ordered接口
关于Ordered接口,用过的人可能知道,这里我谈下自己的理解.也希望各位大神能给予指点. 源码如下: /** * Interface that can be implemented by obje ...
随机推荐
- java位移运算符 转
https://blog.csdn.net/qq_36134429/article/details/78286416#commentsedit java移位运算符不外乎就这三种:<<(左移 ...
- 代码调用t.cn接口生成短址
新浪短网址接口的稳定性和跳转速度还是很给力的,现给出其API说明. 该接口支持两种返回格式:xml和json 对应的URL请求地址为: xml:http://api.t.sina.com.cn/sho ...
- BZOJ1431 : MLand
考虑任意一棵生成树,它的代价是一个一次函数. 因此所有生成树的最小值随着时间变化呈现出的是一个上凸壳. 三分查找最大值即可. 时间复杂度$O(m\log m\log w)$. #include< ...
- VB读写进程的内存
在窗体部分简单测试了ReadProcessMemory和WriteProcessMemory对另一个程序进程的读写. 由于临时项目变动,又不需要了,所以直接封类,删工程.以下代码没有一个函数经过测试, ...
- python学习笔记(6)
第6章 组合数据类型 组合类型的三种表达形式:集合.序列.字典 集合类型及操作 定义:集合是多个元素的无序组合 集合类型与数学中的集合概念一致 集合元素之间无序,每个元素唯一,不存在相同元素 集合元素 ...
- MongoDB 用Robomong可视化工具操作的 一些简单语句
一.数据更新 db.getCollection('表名').update({ "字段":{$in:["值"]} }, //更新条件 {$set:{ " ...
- python函数用法
一.定义函数 形参:函数完成一项工作所需要的信息,在函数定义时完成 实参:调用函数时传递给函数的信息 二.传递实参 1.位置实参:每个实参都关联到函数定义中的一个形参 示例: def describe ...
- NOIP-火柴棒等式
题目描述 给你n根火柴棍,你可以拼出多少个形如“A+B=C”的等式?等式中的A.B.C是用火柴棍拼出的整数(若该数非零,则最高位不能是0).用火柴棍拼数字0-9的拼法如图所示: 注意: 1. 加号与等 ...
- Spring源码学习-容器BeanFactory(一) BeanDefinition的创建-解析资源文件
写在前面 从大四实习至今已一年有余,作为一个程序员,一直没有用心去记录自己工作中遇到的问题,甚是惭愧,打算从今日起开始养成写博客的习惯.作为一名java开发人员,Spring是永远绕不过的话题,它的设 ...
- Linux 搭建Nginx+uWSGI+Django环境
安装环境 sudo apt-get install nginx sudo apt install python3 sudo apt install python3-pip 使用 sudo pip3 i ...