【问题标题】:applicationContext could path could not be resolvedapplicationContext 可能无法解析路径
【发布时间】:2012-12-04 04:19:45
【问题描述】:

当我在我的 spring 简单数据库连接代码中定义 applicationContext 时,我收到了这个错误。

但是当我将 applicationContext 放置在 java (DatabaseTestConnection.java) 所在的位置时,它可以毫无问题地连接。

线程“main”中的异常 org.springframework.beans.factory.BeanDefinitionStoreException: IOException 从类路径资源解析 XML 文档 [应用程序上下文.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"));
    }

}
}

【问题讨论】:

  • 您的applicationContext.xml 在哪里?在WEB-INF 里面还是classpath resource
  • 它位于WEB-INF里面。

标签: spring


【解决方案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.

【讨论】:

    【解决方案2】:

    applicationContext.xml 文件应位于WEB-INF/classes 文件夹内。

    您的配置显示applicationContext.xml 文件位于classpath 中。类路径引用了 servlet 应用程序中的 WEB-INF/classes 目录。

    【讨论】:

    • 当我放置在 java 程序所在的 src/java/ 中时,它工作正常。..
    • 如果您将文件放在src/java 并编译项目,那么文件将被复制到classpath
    猜你喜欢
    • 2019-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-15
    • 2014-11-28
    相关资源
    最近更新 更多