guobosheng 2008-09-01 22:04
浏览 224
已采纳

偶是Spring新手,做试验的时候遇到了点问题(关于加载配置文件),请大虾们不吝赐教!

配置文件:[b]PropertyEditorBeans.xml[/b]
[code="xml"]<?xml version="1.0" encoding="UTF-8"?>
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-2.0.xsd">

<bean id="customEditorConfigurer"
    class="org.springframework.beans.factory.config.CustomEditorConfigurer">
    <property name="customEditors">
        <map>
            <entry key="com.gbsc.propertyeditor.ExoticType">
                <bean
                    class="com.gbsc.propertyeditor.ExoticTypeEditor">
                    <property name="format" value="upperCase" />
                </bean>
            </entry>
        </map>
    </property>
</bean>

<bean id="dependsOnExoticType"
    class="com.gbsc.propertyeditor.DependsOnExoticType">
    <property name="type" value="aNameForExoticType" />
</bean>

[/code]

测试文件:[b]PropertyEditorMain.java[/b]

[code="java"]package com.gbsc.propertyeditor;

import org.springframework.beans.factory.config.CustomEditorConfigurer;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
//import org.springframework.core.io.FileSystemResource;

public class PropertyEditorMain {

public static void main(String[] args) {

//我猜问题应该是出在这里,不知道对不对
ClassPathResource resource = new ClassPathResource
("com.gbsc.propertyeditor.PropertyEditorBeans.xml");
//用FileSystemResource时没有任何问题。
// FileSystemResource resource = new FileSystemResource
// ("PropertyEditorBeans.xml");
XmlBeanFactory factory = new XmlBeanFactory(resource);

    CustomEditorConfigurer config = (CustomEditorConfigurer)factory.getBean
        ("customEditorConfigurer");
    config.postProcessBeanFactory(factory);

    DependsOnExoticType dependsOnExoticType = (DependsOnExoticType)factory.getBean
        ("dependsOnExoticType");
    dependsOnExoticType.display();
}

}
[/code]

报错信息:Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [com.gbsc.propertyeditor.PropertyEditorBeans.xml]; nested exception is java.io.FileNotFoundException: class path resource [com.gbsc.propertyeditor.PropertyEditorBeans.xml] cannot be opened because it does not exist

详细的报错信息如下:
log4j:WARN No appenders could be found for logger (org.springframework.util.ClassUtils).
log4j:WARN Please initialize the log4j system properly.
[b][i]Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [com.gbsc.propertyeditor.PropertyEditorBeans.xml]; nested exception is java.io.FileNotFoundException: class path resource [com.gbsc.propertyeditor.PropertyEditorBeans.xml] cannot be opened because it does not exist[/i][/b]
Caused by: java.io.FileNotFoundException: class path resource [com.gbsc.propertyeditor.PropertyEditorBeans.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:135)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:307)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:290)
at org.springframework.beans.factory.xml.XmlBeanFactory.(XmlBeanFactory.java:73)
at org.springframework.beans.factory.xml.XmlBeanFactory.(XmlBeanFactory.java:61)
at com.gbsc.propertyeditor.PropertyEditorMain.main(PropertyEditorMain.java:16)

不知道是哪里出了问题呢?请大虾们不吝赐教!
[b]问题补充:[/b]
配置文件是放在src下面的,其它类都在src\com\gbsc\propertyeditor下面。
我的classpath是:.;D:\Program Files\Java\jdk1.6.0_02\lib;
[b]问题补充:[/b]
我这个只是个Java项目,没有Web项目里像WEB-INF/classes这种目录的。

  • 写回答

3条回答 默认 最新

  • iteye_13779 2008-09-01 22:41
    关注

    从你的写法看PropertyEditorBeans.xm是在类路径里,只需写ClassPathResource resource = new ClassPathResource ("PropertyEditorBeans.xml"); 就好了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建