当我在spring spring数据库连接代码中定义applicationContext时,我收到了这个错误。
但是当我将applicationContext放在java(DatabaseTestConnection.java)所在的位置时,它会毫无问题地连接。
线程“main”中的异常 org.springframework.beans.factory.BeanDefinitionStoreException: IOException从类路径资源解析XML文档 [applicationContext.xml中];嵌套异常是 java.io.FileNotFoundException:类路径资源 [applicationContext.xml]无法打开,因为它不存在
的applicationContext.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>
DatabaseTestConnection.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"));
}
}
}
答案 0 :(得分:1)
无论如何,你只是在本地测试,所以你可以这样做从上下文路径加载
ApplicationContext ctx = new FileSystemXmlApplicationContext("C:\\workspace\\src\\main\\webapp\\WEB-INF\\applicationContext.xml"); // in your case path to your applicationContext.xml file in your local.
答案 1 :(得分:0)
applicationContext.xml
文件应位于WEB-INF/classes
文件夹中。
您的配置显示applicationContext.xml
文件位于classpath
。类路径引用servlet应用程序中的WEB-INF/classes
目录。