我在学习弹簧依赖注入。我有两种类型的代码。一个人工作,一个人不工作......但是,他们都为制作教程的人工作。
评论的代码给了我下面显示的错误。当我使用的ApplicationContext
异常螺纹
@SuppressWarnings("deprecation")
public static void main(String[] args) {
//ApplicationContext factory = new ClassPathXmlApplicationContext("Beans.xml");
//The code below works
BeanFactory factory = new XmlBeanFactory(new FileSystemResource("Beans.xml"));
HelloWorld obj = (HelloWorld) factory.getBean("helloworld");
obj.getMessage();
}
的beans.xml
<?xml version="1.0"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<bean id="helloworld" class="com.vulab.hellow.HelloWorld">
<property name="message" value="Hello World" />
</bean>
</beans>
错误消息 “主” org.springframework.beans.factory.BeanDefinitionStoreException:IOException异常从解析XML文档类路径资源[Beans.xml];嵌套的异常是java.io.FileNotFoundException:类路径资源[Beans.xml]因为它不存在而不能打开
文件Beans.xml或文件beans.xml?检查案件? –
它在IDE中也是一样。 @JeromeC。 – AppSensei
你把你的Beans.xml文件放在哪里?它是否在一个源文件夹中(当你编译你的应用程序时它是否被复制到你的classes文件夹中)? – Claudio