首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

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

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

(关于加载配置文件)偶是Spring新手,做试验的时候遇到了点问题,请大虾们不吝赐教!
配置文件:PropertyEditorBeans.xml 
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-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>  
</beans>  

<?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-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>
</beans>

测试文件:PropertyEditorMain.java 

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();  
  }  
}  

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();
}
}


报错信息: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. 
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 
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.<init>(XmlBeanFactory.java:73) 
at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:61) 
at com.gbsc.propertyeditor.PropertyEditorMain.main(PropertyEditorMain.java:16) 

不知道是哪里出了问题呢?请大虾们不吝赐教! 


[解决办法]
com.gbsc.propertyeditor.PropertyEditorBeans.xml

这个xml文件不存在或者位置不对

热点排行