创建XmlBeanFactory报错!!
public static void main(String[] args) throws Exception {
// TODO 自动生成方法存根
BeanFactory factory=new XmlBeanFactory(new InputStream("hello.xml"));
报错:XmlbeanFactory构造函数未定义,这是怎么回事!!
Print p=(Print)factory.getBean("print");
p.sayHello();
改成下面的
BeanFactory factory=new XmlBeanFactory(new InputStreamResource(new InputStream("hello.xml")));
谢谢,不报错了,但是又有新的问题了!!
Exception in thread "main" java.io.FileNotFoundException: hello.xml (系统找不到指定的文件。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.io.FileInputStream.<init>(FileInputStream.java:66)
at linqi.Helloapp.main(Helloapp.java:18)
我是安照spring in action上的例子写的,为什么我的BeanFactory factory=new XmlBeanFactory(new FileInputStream("hello.xml"))报错!
可能是版本的问题
hello.xml文件的位置不对
hello.xml和那些java 文件在同一目录,怎么会不对呀?郁闷!
spring in action 的例子用的是1.1的吗?
我用的是Myeclipse生成的spring,它是什么版本的呀?和这个有关系吗??
我也遇到这个问题,但是改成interpb(曾曾胡,深怕情多累美人!!) 说的
BeanFactory factory=new XmlBeanFactory(new InputStreamResource(new InputStream("hello.xml")));
它还是报错:不能实例化类型 InputStream,这是因为它不是一个具体类
这怎么解决呀
能运行了,但是报以下错误:
log4j:WARN No appenders could be found for logger (org.springframework.beans.factory.xml.XmlBeanDefinitionReader).
log4j:WARN Please initialize the log4j system properly.
这是怎么回事!!
不是错误,只是警告信息。需要导入log4j.properties文件,你可以参考一下apache/jakarta的组件包commong-logging
spring采用Apache common_logging,并结合Apache log4j作为日志输出组件,需要在classpath中新建log4j.properties文件
我现在也在看Spring in Action这本书,去www.manning.com官方下了个源代码,没查到第一章的源代码。现在hello.xml是找到了,可是报这个错:
org.springframework.beans.factory.BeanDefinitionStoreException: Passed-in Resource [resource loaded through InputStream] contains an open stream: cannot determine validation mode automatically. Either pass in a Resource that is able to create fresh streams, or explicitly specify the validationMode on your XmlBeanDefinitionReader instance.
还有,我的main()函数是这样的:
try {
BeanFactory factory = new XmlBeanFactory(new InputStreamResource(new FileInputStream("/hello.xml")));
GreetingService greetingService = (GreetingService) factory.getBean("greetingService");
greetingService.sayGreeting();
} catch (Exception e) {
System.out.println(e);
}
哈哈,这个问题被我完美的解决了,楼主,你需要做两步:
1.hello.xml的位置:
请确保配置hello.xml位于工作路径之下,注意工作路径并不等同于CLASSPATH ,eclipse
的默认工作路径为项目根路径,也就是.project文件所在的目录,而默认输出目录/bin是项目
CLASSPATH的一部分,并非工作路径。
2.把HelloApp.java改成这样:
package com.springinaction.chapter01.hello;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class HelloApp {
public static void main(String[] args) throws Exception {
try {
ApplicationContext ctx = new FileSystemXmlApplicationContext("hello.xml");
GreetingService greetingService = (GreetingService) ctx.getBean("greetingService");
greetingService.sayGreeting();
} catch (Exception e) {
System.out.println(e);
}
}
}
包你可以通过编译和运行,运行结果为:Buenos Dias!
还有1点,虽然是小问题,但是缺它不可,就是hello.xml中的这段声明:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
缺了它就不能运行很奇怪,呵呵:)