我是Spring的新手,使用JDBC创建一个简单的应用程序,它从数据库读取记录并在屏幕上显示。
package jdbcexample;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"Beans.xml");
StudentJDBCTemplate studentjdbctemplate = (StudentJDBCTemplate) context
.getBean("studentjdbctemplate");
System.out.println("Listing Reocrds : ");
List<Student> students = studentjdbctemplate.listStudents();
for (Student record : students)
System.out.println("id : " + record.getId() + " name : "
+ record.getName() + " password : " + record.getPassword());
}
}
这是我的Bean.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-3.0.xsd ">
<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://locallhost:3306/TEST" />
<property name="username" value="root"/>
<property name="password" value="password" />
</bean>
<bean id="studentjdbctemplate" class="jdbcexample.StudentJDBCTemplate">
<property name="datasource" ref="datasource" />
</bean>
</beans>
所有引用的Java文件和XML文件都在同一个文件夹中,但我仍然收到此错误。能帮我解决这个问题吗?
log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [Beans.xml]; nested exception is java.io.FileNotFoundException: class path resource [Beans.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:212)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:126)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:92)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:465)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:395)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at jdbcexample.MainApp.main(MainApp.java:12)
Caused by: java.io.FileNotFoundException: class path resource [Beans.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:141)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)
... 13 more
答案 0 :(得分:0)
此处有异常追踪class path resource [Beans.xml] cannot be opened because it does not exist
表示类路径中缺少Beans.xml,因此您会看到异常。确保Beans.xml在类路径中,它可以正常工作。
答案 1 :(得分:0)
Have a look at this article. 这将帮助您设置基本的MAVEN Spring MVC HIbernate Sitemesh项目
这里缺少的是file not found
,即Beans.xml。
你可以在web.xml中定义这个类,你可以这样做
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/Beans.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
你需要这样做
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring/application-config.xml,
/WEB-INF/Beans.xml
</param-value>
</context-param>
我希望这对你有用
答案 2 :(得分:0)
我将Beans.xml移动到src文件夹,程序运行成功。