当我在我的 spring 简单数据库连接代码中定义 applicationContext 时,我收到了这个错误。
但是当我将 applicationContext 放置在 java (DatabaseTestConnection.java) 所在的位置时,它可以毫无问题地连接。
线程“主”org.springframework.beans.factory.BeanDefinitionStoreException 中的异常:IOException 从类路径资源 [applicationContext.xml] 解析 XML 文档;嵌套异常是 java.io.FileNotFoundException: 类路径资源 [applicationContext.xml] 无法打开,因为它不存在
来源
应用程序上下文.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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!--bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.properties" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
p:password="${jdbc.password}" /-->
<bean id="databaseTestConnection" class="DatabaseTestConnection">
<property name="dataSource" ref="externalDataSource"/>
</bean>
<bean id="externalDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" scope="singleton" destroy-method="close">
<property name="driverClassName" value="sun.jdbc.odbc.JdbcOdbcDriver"/>
<property name="url" value="jdbc:odbc:;DRIVER=Microsoft Access Driver (*.mdb, *.accdb);DBQ=C://Users//gopc//Documents//odbc_sql.accdb"/>
<property name="username" value=""/>
<property name="password" value=""/>
</bean>
<!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->
</beans>
数据库测试连接.java
import java.util.*;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
public class DatabaseTestConnection {
private JdbcTemplate jt;
public void setDataSource(DataSource dataSource) {
this.jt = new JdbcTemplate(dataSource);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
XmlBeanFactory bf = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
DatabaseTestConnection bn = (DatabaseTestConnection) bf.getBean("databaseTestConnection");
int count = bn.jt.queryForInt("select count(*) from log_entry");
System.out.println("cgk count:" + count);
List <Map <String,Object> > ob = bn.jt.queryForList("select * from log_entry", args);
System.out.println("cgk size:" + ob.size());
for (Map<String,Object> entry: ob )
{
System.out.println("ID:" + entry.get("ID"));
System.out.println("DateCol:" + entry.get("DateCol"));
System.out.println("Completed:" + entry.get("Completed"));
}
}
}