在spring下读取文件的绝对路径 <import resource="classes/test/test.xml" />或者<import resource="classpath:test/test.xml" /> 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 好多方法啊,关键在于你已经对路径问题非常清晰了,我一般写自己的类继承spring资源加载类,按我的意思加载。http://blog.csdn.net/sunyujia/archive/2008/09/21/2957481.aspxhttp://blog.csdn.net/sunyujia/archive/2008/01/05/2027087.aspxhttp://blog.csdn.net/sunyujia/archive/2008/06/01/2501720.aspx 一楼的同学请仔细审题test.xml里面怎么写不是怎么import给别人 无语中,需要点学习能力的,没听说过csdn都是保姆吗???想不被javaeye鄙视吗,就要点学习能力,非得给代码才行啊。一个字母都不差就是针对性。重复回答太多遍的问题,做做广告行不行。 package com;import org.springframework.context.support.FileSystemXmlApplicationContext;public class Test { private String name; public static void main(String[] args) throws Exception { java.net.URL url = com.Test.class.getResource("test.xml"); FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext( url.getFile()); System.out.println(((Test)context.getBean("testBean")).getName()); } public String getName() { return name; } public void setName(String name) { this.name = name; }}package com;import java.io.File;import java.io.UnsupportedEncodingException;import java.net.MalformedURLException;import java.net.URL;import org.springframework.core.io.Resource;import org.springframework.core.io.UrlResource;public class Loader extends org.springframework.beans.factory.config.PropertyPlaceholderConfigurer { /* * Description:取得当前类所在的文件 * * @param clazz @return @mail [email protected] @since:Sep 21, 2008 12:32:10 * PM */ public static File getClassFile(Class clazz) { URL path = clazz.getResource(clazz.getName().substring( clazz.getName().lastIndexOf(".") + 1) + ".class"); if (path == null) { String name = clazz.getName().replaceAll("[.]", "/"); path = clazz.getResource("/" + name + ".class"); } return new File(path.getFile()); } /** * * Description:取得当前类所在的ClassPath目录 * * @param clazz * @return * @mail [email protected] * @since:Sep 21, 2008 12:32:27 PM */ public static File getClassPathFile(Class clazz) { File file = getClassFile(clazz); for (int i = 0, count = clazz.getName().split("[.]").length; i < count; i++) file = file.getParentFile(); if (file.getName().toUpperCase().endsWith(".JAR!")) { file = file.getParentFile(); } return file; } /** * * Description: 同getClassPathFile 解决中文编码问题 * * @param clazz * @return * @mail [email protected] * @since:Sep 21, 2008 1:10:37 PM */ public static String getClassPath(Class clazz) { try { return java.net.URLDecoder.decode(getClassPathFile(clazz) .getAbsolutePath(), "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return ""; } } public void setLocation(Resource location) { File file=Loader.getClassPathFile(Loader.class).getParentFile().getParentFile(); System.out.println(file+location.getFilename()); File f=new File(file.getAbsolutePath()+"\\"+location.getFilename()); try { super.setLocation(new UrlResource(f.toURL())); } catch (MalformedURLException e) { e.printStackTrace(); } }} applicationContext.xml<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"><beans> <bean id="testBean" class="com.Test"> <property name="name" ><value>${name}</value></property> </bean></beans>test.xml<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"><beans> <import resource="./../applicationContext.xml" /> <bean id="propertyConfigurer" class="com.Loader"> <property name="location"> <value>config.properties</value> </property> </bean></beans> config.properties内容name=csdn程序输出csdn问答此问题的全部知识点,来自我的3篇博客。楼上还想说什么。 关于自动登录使用AOP记录日志的问题 把jsp页面的内容生成为pdf 上传文件时,后台代码中取字段问题 Spring AOP 拦截指定方法 struts关于生成class文件的疑惑 自己2001写的很小的XML解析器,现在放给大家. ibatis中对SQL server 2000中的text数据类型的处理? 求救:我自定义了一个标签,为什么总是告诉我找不到标签库描述信息? 做java但不愿做界面,这样好找工作吗? 想兼职的注意了!公司项目,找中关村附近JAVA开发高手兼职开发 j2ee面试题 MySQL连接问题,知道了帮下忙,谢谢。
我一般写自己的类继承spring资源加载类,按我的意思加载。
http://blog.csdn.net/sunyujia/archive/2008/09/21/2957481.aspx
http://blog.csdn.net/sunyujia/archive/2008/01/05/2027087.aspx
http://blog.csdn.net/sunyujia/archive/2008/06/01/2501720.aspx
想不被javaeye鄙视吗,就要点学习能力,非得给代码才行啊。一个字母都不差就是针对性。
重复回答太多遍的问题,做做广告行不行。
private String name; public static void main(String[] args) throws Exception {
java.net.URL url = com.Test.class.getResource("test.xml");
FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext(
url.getFile());
System.out.println(((Test)context.getBean("testBean")).getName());
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
}
package com;import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;public class Loader extends
org.springframework.beans.factory.config.PropertyPlaceholderConfigurer {
/*
* Description:取得当前类所在的文件
*
* @param clazz @return @mail [email protected] @since:Sep 21, 2008 12:32:10
* PM
*/
public static File getClassFile(Class clazz) {
URL path = clazz.getResource(clazz.getName().substring(
clazz.getName().lastIndexOf(".") + 1)
+ ".class");
if (path == null) {
String name = clazz.getName().replaceAll("[.]", "/");
path = clazz.getResource("/" + name + ".class");
}
return new File(path.getFile());
} /**
*
* Description:取得当前类所在的ClassPath目录
*
* @param clazz
* @return
* @mail [email protected]
* @since:Sep 21, 2008 12:32:27 PM
*/
public static File getClassPathFile(Class clazz) {
File file = getClassFile(clazz);
for (int i = 0, count = clazz.getName().split("[.]").length; i < count; i++)
file = file.getParentFile();
if (file.getName().toUpperCase().endsWith(".JAR!")) {
file = file.getParentFile();
}
return file;
} /**
*
* Description: 同getClassPathFile 解决中文编码问题
*
* @param clazz
* @return
* @mail [email protected]
* @since:Sep 21, 2008 1:10:37 PM
*/
public static String getClassPath(Class clazz) {
try {
return java.net.URLDecoder.decode(getClassPathFile(clazz)
.getAbsolutePath(), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return "";
}
} public void setLocation(Resource location) {
File file=Loader.getClassPathFile(Loader.class).getParentFile().getParentFile();
System.out.println(file+location.getFilename());
File f=new File(file.getAbsolutePath()+"\\"+location.getFilename());
try {
super.setLocation(new UrlResource(f.toURL()));
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="testBean" class="com.Test">
<property name="name" ><value>${name}</value></property>
</bean>
</beans>
test.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<import resource="./../applicationContext.xml" />
<bean id="propertyConfigurer"
class="com.Loader">
<property name="location">
<value>config.properties</value>
</property>
</bean>
</beans>
name=csdn
程序输出
csdn问答此问题的全部知识点,来自我的3篇博客。楼上还想说什么。