简体   繁体   English

使用IntelliJ IDEA 13社区版创建一个基本的Spring Core应用程序

[英]Creating a basic spring core application with intellij idea 13 community edition

I am just learning spring and my first program is to load a bean xml and instantiate the classes. 我只是在学习spring,而我的第一个程序是加载bean xml并实例化类。

I will be using spring core and I have created a Java project. 我将使用Spring Core,并创建了一个Java项目。 Created a package called: 创建了一个名为:

com.lecture com.lecture

and put the java files there as well as the bean file spring-beans.xml 并将Java文件和bean文件spring-beans.xml放在此处

<beans>
    <bean id="rkdf" class="com.college.RKDFCollege"/>
    <bean id="cseLect" class="com.college.CSELecturer"/>
</beans>

But when I am running the main class which has this code: 但是当我运行具有以下代码的主类时:

ApplicationContext context = new ClassPathXmlApplicationContext("com.lecture.spring-beans.xml");

it is giving me errors 它给我错误

Dec 07, 2014 9:10:37 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [com.lecture.spring-beans.xml] Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [com.lecture.spring-beans.xml]; 2014年12月7日9:10:37 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO:从类路径资源[com.lecture.spring-beans.xml]中加载XML bean定义.springframework.beans.factory.BeanDefinitionStoreException:IOException从类路径资源解析XML文档[com.lecture.spring-beans.xml]; nested exception is java.io.FileNotFoundException: class path resource [com.lecture.spring-beans.xml] cannot be opened because it does not exist 嵌套的异常为java.io.FileNotFoundException:类路径资源[com.lecture.spring-beans.xml]无法打开,因为它不存在

I am new to this I dont have any idea so far! 我是新来的,到目前为止我还没有任何想法! In the project module settings, I have added JDK and Spring jars. 在项目模块设置中,我添加了JDK和Spring jar。 Guidance is appreciated. 指导表示赞赏。

Try using the following path to the XML: "com/lecture/spring-beans.xml" 尝试使用以下XML路径: "com/lecture/spring-beans.xml"

ClassPathXmlApplicationContext treats your class path as a file system. ClassPathXmlApplicationContext将类路径视为文件系统。

ApplicationContext context = new ClassPathXmlApplicationContext("com.lecture.spring-beans.xml"); is searching com.lecture.spring-beans.xml file in the class path which I believe is not the name of the file you have created. 在类路径中搜索com.lecture.spring-beans.xml文件,我相信这不是您创建的文件的名称。 I believe you have created file spring-beans.xml 我相信您已经创建了文件spring-beans.xml

Make sure spring-beans.xml file is in class path. 确保spring-beans.xml文件在类路径中。 Else you can put the spring-beans.xml in resource folder as well. 另外,您也可以将spring-beans.xml放在资源文件夹中。 Then load the file as: 然后将文件加载为:

ApplicationContext context = new ClassPathXmlApplicationContext("spring-beans.xml");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM