Resource resource=new ClassPathResource("hello.xml");
BeanFactory factory=new XmlBeanFactory(resource);不知道哪本书教你这种误人子弟的写法

解决方案 »

  1.   

    现在改成
        Resource resource=new ClassPathResource("hello.xml");
        BeanFactory factory=new XmlBeanFactory(resource);
        //BeanFactory factory=new XmlBeanFactory(new FileInputStream("hello.xml"));
        GreetingService greetingService=(GreetingService)factory.getBean("greetingService");
        greetingService.sayGreeting();但是还是出错了:
    package com.springinaction.chapter01.hello;import java.io.FileInputStream;
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.beans.factory.xml.XmlBeanFactory;public class HelloApp
    {
      public static void main(String[] arg) throws Exception
      {
        Resource resource=new ClassPathResource("hello.xml");
        BeanFactory factory=new XmlBeanFactory(resource);
        //BeanFactory factory=new XmlBeanFactory(new FileInputStream("hello.xml"));
        GreetingService greetingService=(GreetingService)factory.getBean("greetingService");
        greetingService.sayGreeting();
      }
    }怎麽办~~~~
      

  2.   

    E:\java web work\spring\com\springinaction\chapter01\hello\HelloApp.java:11: cannot resolve symbol
    symbol  : class Resource 
    location: class com.springinaction.chapter01.hello.HelloApp
        Resource resource=new ClassPathResource("hello.xml");
        ^
    E:\java web work\spring\com\springinaction\chapter01\hello\HelloApp.java:11: cannot resolve symbol
    symbol  : class ClassPathResource 
    location: class com.springinaction.chapter01.hello.HelloApp
        Resource resource=new ClassPathResource("hello.xml");
                              ^
    2 errors工具以退出代码 1 完成
      

  3.   

    难道你java是初学的?你不import怎么用?
    如果是初学的,不要去搞什么spring,看看基础为好
      

  4.   

    加上了
    import org.springframework.core.io.ClassPathResource;
    一样出错,请再帮我谢谢!
    E:\java web work\spring\com\springinaction\chapter01\hello\HelloApp.java:12: cannot resolve symbol
    symbol  : class Resource 
    location: class com.springinaction.chapter01.hello.HelloApp
        Resource resource=new ClassPathResource("hello.xml");
        ^
    1 error工具以退出代码 1 完成
      

  5.   

    我估计你放的hello.xml位置不对,你不如用绝对路径解决,(比如c:\\..\\hello.xml)再去看一下关于环境路径的问题,不同的上下文,路径是不一样的.
    Resource resource=new ClassPathResource("hello.xml");也可能无法查找到hello.xml,因为不在不在classPath路径中.
      

  6.   

    ..再加上
    import org.springframework.core.io.Resource;
      

  7.   

    编译通过了,但是运行时出错log4j:WARN No appenders could be found for logger (org.springframework.core.Coll
    ectionFactory).
    log4j:WARN Please initialize the log4j system properly.
    Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStore
    Exception: IOException parsing XML document from class path resource [hello.xml]
    ; nested exception is java.io.FileNotFoundException: class path resource [hello.
    xml] cannot be opened because it does not exist
    java.io.FileNotFoundException: class path resource [hello.xml] cannot be opened
    because it does not exist
            at org.springframework.core.io.ClassPathResource.getInputStream(ClassPat
    hResource.java:137)
            at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBea
    nDefinitions(XmlBeanDefinitionReader.java:167)
            at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBea
    nDefinitions(XmlBeanDefinitionReader.java:148)
            at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFa
    ctory.java:73)
            at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFa
    ctory.java:61)
            at com.springinaction.chapter01.hello.HelloApp.main(HelloApp.java:15)
      

  8.   

    绝对路径怎么可能找不到
    比如C:\\test\\hello.xml
      

  9.   

    分倒不重要,问题解决才是关键,耐心点吧,找个简单例子做做,如果还不行给我发个Email到[email protected]
      

  10.   

    不要用ClassPathResource, 用FileSystemResource, 并使用绝对路径改成 Resource resource=new FileSystemResource("c:/hello.xml");hello.xml放在c盘根目录下面不要忘记 import org.springframework.core.io.FileSystemResource;