测试SpringDAO的文件:
package springdao;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringDAOTest { /**
 * @param args
 */
public static void main(String[] args) {
ApplicationContext ctx =
new ClassPathXmlApplicationContext("applicationContext"); HeadofficeDAO dao = (HeadofficeDAO)ctx.getBean("HeadofficeDAO");

Headoffice hoffice =new Headoffice();

hoffice.setId("111");
hoffice.setPassword("111");
hoffice.setUsername("张三");
hoffice.setAge(25);
hoffice.setGender("男");
hoffice.setPart("Manager");

dao.save(hoffice);

}}
错误提示:
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [applicationContext]; nested exception is java.io.FileNotFoundException: class path resource [applicationContext] cannot be opened because it does not exist
Caused by: java.io.FileNotFoundException: class path resource [applicationContext] 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.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:131)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:147)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:173)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:112)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:79)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:101)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:389)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:324)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:122)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:66)
at springdao.SpringDAOTest.main(SpringDAOTest.java:14)
网上查了老多,说applicationContext文件路径的问题,改到WEN-INI下还是错。不明白了,这些都该怎么解决呀?现在对整个这些都没有个清晰的逻辑。

解决方案 »

  1.   

    new ClassPathXmlApplicationContext("applicationContext"); applicationContext.xml
      

  2.   

    new ClassPathXmlApplicationContext("applicationContext.xml"); 
      

  3.   

    改了,还是不行。昨天我以为没人理我,所以就没看回复。大虾们真热情呀!
    以下是我的applicationContext.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="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="configLocation"
    value="classpath:hibernate.cfg.xml">
    </property>
    </bean>
    <bean name="/login" 
         class="com.Toggry.struts.action.LoginAction"></bean>
    <bean id="HeadOfficerDAO" class="springdao.HeadOfficerDAO">
    <property name="sessionFactory">
    <ref bean="sessionFactory" />
    </property>
    </bean></beans>
    我最想知道,这个class path在哪里能配置呢?请高手指点
      

  4.   

    <property name="configLocation" 
    value="classpath:hibernate.cfg.xml"> 
    </property> 
    这里错了
      

  5.   

    在web.xml里配置
    <context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>classpath:applica*.xml</param-value>
      </context-param>
      
      <listener>
         <listener-class>
           org.springframework.web.context.ContextLoaderListener
         </listener-class>
      </listener>
      

  6.   

    public class SpringDAOTest { /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
    ApplicationContext ctx = new ClassPathXmlApplicationContext("src/applicationContext.xml"); HeadofficeDAO dao = (HeadofficeDAO)ctx.getBean("HeadofficeDAO"); Headoffice hoffice =new Headoffice(); hoffice.setId("111"); 
    hoffice.setPassword("111"); 
    hoffice.setUsername("张三"); 
    hoffice.setAge(25); 
    hoffice.setGender("男"); 
    hoffice.setPart("Manager"); dao.save(hoffice); } }
    再不行  你自己加一个 log4j测试