java中的xml主程序无法打开Springbean配置文件
我刚开始学习Spring框架,安装了Eclipse并添加了Spring和Maven。我制作了我的第一个程序,当然是Hello World,但使用的是spring beans。 一切都是“按书”进行的,我创建了一些类,比如Person,和主类,它们将通过xml spring beans配置文件中的beans对其进行实例化。我做了豆子。xml文件就在我的项目中,就像书中的示例一样,并编写了以下代码:
package maven.aplikacije.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App
{
public static void main( String[] args )
{
ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
Person helloBean = (Person)ac.getBean("person");
helloBean.sayHello();
}
}
这是一颗豆子。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.xsd">
<bean id = "person" class = "maven.aplikacije.Person.Person"></bean>
</beans>
然而,它不起作用。它列出了以下错误(以及其他错误):
信息:从类路径资源[Person/beans.XML]加载XMLBean定义 线程“main”组织中出现异常。springframework。豆。工厂BeanDefinitionStoreException:IOException解析来自类路径资源[Person/beans.XML]的XML文档;嵌套的例外是java。伊奥。FileNotFoundException:无法打开类路径资源[Person/beans.xml],因为它不存在
我试着写作
ApplicationContext ac = new ClassPathXmlApplicationContext("Person//beans.xml");
还有很多不同的版本,但都不管用。 当然,我还定义了Person class.:)
为什么程序不能“看到”bean。xml文件
提前谢谢
# 1 楼答案
当您从
Person/beans.xml
的类路径引用它时您需要在maven项目中的以下位置放置
beans.xml
如果你试着阅读
然后
或者你可以把它放在任何地方,配置maven来放置bean。类路径中具有指定命名空间的xml文件